11

I'm calling a script where I give a command line input to LaTeX:

pdflatex "\def\myvar{given_test} \input{figures.tex}"

And then in the script, I try to use this "given_test" input and print it:

\textbf{filename: \myvar}

This breaks LaTeX, then I try to use detokenize

\textbf{filename: \detokenize{\myvar}}

And that just prints the text "\myvar"

The underscore is a necessity, since we are reading from a more complex pipeline that uses them for the file identifiers.

So how do I read what is inside \myvar and not break LaTeX.

Werner
  • 603,163

2 Answers2

10

Together with guidelines in Handling of special LaTeX characters in text, you can expand \myvar before \detokenize-ing it:

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\def\myvar{given_test}
\begin{document}
\textbf{filename: \detokenize\expandafter{\myvar}}
\end{document} 
Werner
  • 603,163
  • 1
    Sorry for nitpicking but I think that the first \expandafter is not mandatory (as with \unexpanded). Sorry if I'm mistaken. – cjorssen Nov 15 '13 at 08:33
  • @cjorssen You're right: \detokenize looks for the opening brace expanding tokens as it goes, so the first \expandafter is not necessary. – egreg Nov 15 '13 at 10:22
6

It shouldn't be difficult to change your pipeline to do

pdflatex "\edef\myvar{\detokenize{given_test}}\input{figures.tex}"

Example with figures.tex like this:

\documentclass{article}

\begin{document}

Here's the file name: \texttt{\myvar}

\input{\myvar}

\end{document}

and with given_test.tex containing

Hello World!

Compiling with the above command line gives

enter image description here

so you see that the variable points to the correct file name.

Note that if you need \myvar in other fonts than typewriter type, loading

\usepackage[T1]{fontenc}

in the document becomes necessary.

egreg
  • 1,121,712