# [cairo] Inkscape eps export for Latex

Bernd Bandemer bandemer at gmail.com
Tue Sep 1 10:55:03 PDT 2009

Hi Adrian and Behdad,

>>> Any ideas on a robust solution for this? I also noticed that the
>>> roadmap for Cairo has enabling/disabling certain subsetters scheduled
>>> for 2.12, but this is very far in the future... It would be great if a
>>> solution can be found sooner.
>>
>> Currently as long as cairo is subsetting the font any hack based around
>> substituting placeholder text will not work since the substituted text
>> may use glyphs that are not in the subset.
>
> I think psfrag substitutes the entire show operation with other Postscript
> code.  So subsetting in itself is not an issue.

Honestly, I don't know how the internals of psfrag work. It's true
that it can handle Tj as long as characters are not reindexed. So,
"(abc) Tj"  will work, "<010203> Tj" won't. I am not sure "<979899>
Tj" works or not.

>> Possible solutions are:
>> 1) Fix Inkscape
>> The purpose of psfrag is to embed LaTex equations in the drawings.
>> However Inkscape already provides this functionality with
>> Extensions->Render->LaTeX formula. In a comment 12 on the Inkscape bug
>> report [1] you pointed out that after embedding an equation in Inkscape,
>> changing the font or font size requires requires reentering the
>> equation. This appears to be because Inkscape converts the rendered
>> equation to paths and embeds the paths in the SVG file.
>>
>> This could be fixed if Inkscape also embedded the LaTex equation in the
>> SVG file and automatically rerendered it when the font is changed. I'm
>> not an SVG expert but reading the SVG 1.1 specification there is a
>> "Foreign namespaces and private data" feature that can be used for
>> "Application-specific information so that authoring applications can
>> include model-level data in the SVG content to serve their
>> "roundtripping" purposes".
>
> I think I agree with Bernd that such solutions break the batch-processing
> nature of TeX systems and are suboptimal.  If you want to change the formula
> in the drawing, instead of typing in vim and running make, you now need to
> open Inkscape, change something, export to EPS, run make.

Adrian, you're right, this is another way to do it. In fact, there is
an extension for inkscape called textext which does it relatively well
( http://www.elisanet.fi/ptvirtan/software/textext/ ). However, like
Behdad said, it's much cleaner to have all latex source code in actual
.tex files, not scattered across .tex, .svg, ...  For example, if you
want to change the latex font or font size, with psfrag you only need
to recompile the tex source without touching the figure files. With
textext, every figure file has to be updated one by one.

> Another option, one I like to see instead, is a solution similar to TeX's
> \special.  That is, have a way in cairo PS backend to inject arbitrary
> Postscript code.  The Inkscape can have a kind of external object
> placeholder that it would inject into cairo's output, and psfrag changed to
> understand that.  Similar features for PDF is also desired, but should be
> flexible enough to allow PDF forms, etc, for example.

I think this would be the cleanest solution. There is no reason the
placeholders in the eps file should be actual postscript text. The
only reason psfrag uses this way is for practicality. It would be
cleaner to have some sort of "coordinate tag" which in itself does not
render anything, but allows applications of psfrag to position its
latex code properly within the postscript canvas. I don't know whether
such a "coordinate tag" exists in postscript standard and/or pdf. If
yes, then that would be the natural solution. Inkscape and psfrag
should be updated to support it, and cairo should have support for it
via some \special-like facility, like Behdad said.

>> 2) Changes to cairo subsetting
>>
>> One of the items on my TODO list is to look into providing the option to
>> allow embedding non-subsetted fonts in PDF. This is so the PDF output
>> can be roundtripped though applications that modify the PDF file.
>
> Right.  This would be a useful feature indeed.

I agree, this is useful for other applications too. I guess the main
work here would be defining a proper framework of managing the
subsetters. The case we need here is relatively simple (we only need
to deactivate any subsetting), but I guess you have a more fine-grain
control of the subsetting process in mind.

As for the implementation details, I can't comment much. I don't
understand the fine points in font managament, and I am not aware
enough of the cross-dependencies to other backends of cairo and other
users of cairo other than inkscape, so I can't really comment on this.

For experimentation purposes, here's an example of psfrag use:

-------------- prepare inkscape figure file -------------------------
-make an inkscape document with a text object "abcdef", say drawing.svg
-run "inkscape --export-eps drawing.eps drawing.svg"

(I would attach the files here but I am not sure about the policies
for this list)

-------------- tex source code: testpsfrag.tex --------------------
\documentclass[12pt]{article}
\usepackage{graphicx,psfrag}

\begin{document}

\begin{figure}
\psfrag{abcdef}{$x=\frac 3 2 x_0$}
\includegraphics[width=6cm]{drawing}
\caption{Illustration of psfrag}
\end{figure}

\end{document}

-------------- compile it using: --------------------
latex testpsfrag        # (makes dvi file)
dvips testpsfrag             # (makes ps file)
ps2pdf -dEmbedAllFonts=true -dPDFSETTINGS=/prepress testpsfrag.ps
# (make pdf file)
... then open testpsfrag.pdf

Note we need to go .tex -> .dvi -> .ps -> .pdf  for psfrag to work.
It's possible to compile tex directly to pdf with pdflatex, but psfrag
doesn't work with them.
----------------------------------------------------------

Let me know how I can help further,
Bernd