0

I am using the Dia graphics editor and exporting my diagrams to TikZ code. I am trying to address Dia's inability to easily typeset formulas e.t.c. by using LaTeX for this part. Unfortunately that editor does not (and will not) allow raw text to be exported directly - any LaTeX code inserted in e.g. a text label is escaped in the output, which makes it (mostly) useless:

\ifx\du\undefined
  \newlength{\du}
\fi
\setlength{\du}{15\unitlength}
\begin{tikzpicture}
\pgftransformxscale{1.000000}
\pgftransformyscale{-1.000000}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (4.000000\du,6.000000\du){PlaceHolder0};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (4.000000\du,8.000000\du){PlaceHolder1};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (4.000000\du,10.000000\du){!@\#\$\%\^{}\&*()|\{\}\ensuremath{[}\ensuremath{]}<>};
\end{tikzpicture}

The code above corresponds to a diagram with three text labels. The line before the last one is a label with a bunch of symbols: !@#$%^&*()|{}[]<> becomes !@\#\$\%\^{}\&*()|\{\}\ensuremath{[}\ensuremath{]}<>. Not every symbol is escaped, but quite a few of them are.

I was hoping to get around this limitation, using the catchfile package to "import" the TikZ code and then StrSubstitute from the xstring package to replace specific placeholder text with raw TeX code. My current code looks like this:

\newcommand{\inputfigure}[1]{{
    \CatchFileDef{\tmp}{#1}{}%
    \noexpandarg%
    \expandafter\StrSubstitute\expandafter{\tmp}{PlaceHolder0}{Text0}[\tmp]%
    \expandafter\StrSubstitute\expandafter{\tmp}{PlaceHolder1}{Text1}[\tmp]%
    \tmp%
}}

Unfortunately it does not seem to work - LaTeX runs for an inordinate amount of time at 100% CPU and then the output still has the original text.

How can I make this work? Alternatively, is there another way to do this that does not involve external programs?

  • How is latex code escaped in the output? E.g. what happens when you add \mytext in a label? And what would happen with |mytext or §mytext? – Ulrike Fischer Jan 5 '15 at 9:50
  • 1
    Can you add a simple example of a file that you want to import and of a typical replacement? – egreg Jan 5 '15 at 10:34
  • @UlrikeFischer: \mytext becomes \\mytext. Same with all "special" characters... – thkala Jan 5 '15 at 15:54
  • And what about |? If this is stable you could use it instead of the backslash. – Ulrike Fischer Jan 5 '15 at 16:01
  • @egreg: I posted a sample file with three text labels, including one with symbols. – thkala Jan 6 '15 at 16:48
3

A bit long for a comment. You could write in the label a command as |mytext and in the tex document locally change the catcode of | so that it can be use instead of the backslash:

\documentclass{scrreprt}
\def\mytext{abc}
\begin{document}
\begingroup
\catcode`\|=0
|mytext
\endgroup
\end{document}

(This naturally will only work if | is not used somewhere else in your tikz code.)

|improve this answer|||||
  • Excellent! This does indeed work very nicely... – thkala Jan 7 '15 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.