# Replacing placeholder text in LaTeX \input file

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? Jan 5, 2015 at 9:50
• Can you add a simple example of a file that you want to import and of a typical replacement? Jan 5, 2015 at 10:34
• @UlrikeFischer: \mytext becomes \\mytext. Same with all "special" characters... Jan 5, 2015 at 15:54
• And what about |? If this is stable you could use it instead of the backslash. Jan 5, 2015 at 16:01
• @egreg: I posted a sample file with three text labels, including one with symbols. Jan 6, 2015 at 16:48

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.)

• Excellent! This does indeed work very nicely... Jan 7, 2015 at 16:26