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.

  • 2
    Try \textbf{filename: \expandafter\detokenize\expandafter{\myvar}}
    – Werner
    Nov 15, 2013 at 3:25
  • It works, but it gives a dot instead of the underscore, is understandable, but if it gave the underscores it would be great, care to write it as an answer and then you can get the sweet points? Nov 15, 2013 at 3:28
  • @Leonpalafox Changing \textbf onto \texttt gives underscore in the desired place. Nov 15, 2013 at 4:29
  • You can use \edef\myvar{\detokenize{given_test}} in the command line.
    – egreg
    Nov 15, 2013 at 17:58

2 Answers 2


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

enter image description here

\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\textbf{filename: \detokenize\expandafter{\myvar}}
  • 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, 2013 at 8: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, 2013 at 10:22

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:



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



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


in the document becomes necessary.

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.