Skip to content

Instantly share code, notes, and snippets.

@jpablo
Created April 29, 2011 06:42
Show Gist options
  • Save jpablo/947951 to your computer and use it in GitHub Desktop.
Save jpablo/947951 to your computer and use it in GitHub Desktop.
Convert inkscape files into eps converting the embeded strings into latex formulas
#! /usr/bin/env ruby
# has problems with spaces
# depends on:
# pstotext
# http://pages.cs.wisc.edu/~ghost/doc/pstotext.htm
def procesa(file)
p file
nombre = file.split("/")[-1]
base = nombre.split(".").first
letras = `pstotext #{file}`.split
macros = letras.grep(/\\newcommand/)
#caption = "\\caption{.}\n\\label{#{base}}"
label = base
File.open(base + ".tex","w") do |f|
macros.each{|m| f.puts(m + "\n")}
if not $main then
f.puts <<'TEXTO'
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\mb}[1]{\mathbf{#1}}
\newcommand{\mr}[1]{\mathrm{#1}}
\newcommand{\bs}[1]{\boldsymbol{#1}}
TEXTO
end
f.puts <<'TEXTO'
\begin{figure}[!htbp]
\centering
%\colorbox{white}{$R$}
TEXTO
letras.uniq.sort.each{ |l|
l2 = l.gsub("\\","\\\\\\")
if macros.include? l then
f.puts "\\psfrag{#{l2}}{}"
else
f.puts "\\psfrag{#{l2}}{$ #{l} $}"
end
}
f.puts <<TEXTO
\\includegraphics{#{base}}
%\\caption{}
\\label{#{label}} #{"\n" + label if $label}
\\end{figure}
TEXTO
end
end
# 13.upto(30) { |x|
# procesa "3-#{x}.eps"
# }
$label = false
$main = false
if ARGV[0] == "-l" then
$label = true
ARGV.delete_at 0
end
if ARGV[0] == "-m" then
$main = true
ARGV.delete_at 0
end
ARGV.each {|f| procesa f}
#! /usr/bin/env ruby
# -----------------
# for inkscape
# -----------------
# depends on:
# inkscape, eps2frag.rb, latex, dvips, psbbox
def file2eps(tmpdir, base)
`cd #{tmpdir}; inkscape -E #{base}.eps ../#{base}.svg`
end
def principal(file)
<<CADENA
\\documentclass[12pt]{article}
\\usepackage{graphics, amsmath, amsfonts, amssymb, color, psfrag, bbm}
\\begin{document}
\\pagestyle{empty}
#{IO.readlines(file).join}
\\end{document}
CADENA
end
def procesa(file)
print file + "\n"
base = file.split(".")[0..-2].join(".")
ext = file.split(".")[-1]
tmpdir = base + ".tmpdir"
`mkdir #{tmpdir}`
# file.svg -> file.eps
file2eps(tmpdir, base)
# file.eps -> file.tex
`cd #{tmpdir}; eps2frag.rb #{base}.eps`
File.open(tmpdir+"/main.tex", "w") do |file|
file.print principal(tmpdir + "/" + base + ".tex")
end
`cd #{tmpdir}; latex --interaction batchmode main`
`cd #{tmpdir}; dvips -E -q -o main.ps main.dvi`
`cp -f #{tmpdir}/main.ps #{base}.psf.eps`
# arregla la BoundingBox (hay un error en dvips)
`psbbox #{base}.psf.eps`
`rm -rf #{tmpdir}` if $deltmpdir
end
if ARGV[0] == "-d" then
$deltmpdir = false
ARGV.delete_at 0
else
$deltmpdir = true
end
ARGV.each {|f| procesa f}
#!/bin/csh
#
# NAME
# psbbox - Replace BoundingBox line in PostScript files by "real" BoundingBox
#
# SYNOPSIS
# psbbox file ...
#
# DESCRIPTION
# This program replaces the BoundingBox line in all PostScript files
# specified on the command line by a BoundingBox determined by the bbox
# modules of Ghostscript.
#
# KNOWN LIMITATIONS
# Works only for single-page PostScript files. Other limitations are
# the limitations of the Ghostscript bbox module.
#
# AUTHOR
# psbbox was created by Remko Scharroo on 7 October 2003.
#
foreach file ( $* )
set tmpfile=`mktemp /tmp/psbbox.XXXXXX`
set bbox=`gs -dNOPAUSE -dBATCH -sDEVICE=bbox $file |& grep '%%BoundingBox'`
cp $file $tmpfile
sed 's/%%BoundingBox: .*$/'"$bbox/" $tmpfile >! $file
rm -f $tmpfile
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment