I would like to use a variable so I can define which file to include at compile-time (argument passed to LaTeX - see below).

Problem here is that I cannot insert document name with "_", without crashing everything. Is thez a simple way to perform such task?

Command line used

 pdflatex "\def\testpath{my\_document}\input{my_tex_document}"

Below is a minimal example of my LaTex document


 \title{Librairie \testpath}


 \section{Insertion of \testpath}

  • 5
    The simple way would be to not have underscores in your filenames? – Seamus Jul 20 '11 at 15:16

I would use the following command:

pdflatex --jobname 'my_document' my_tex_document

This will produce a my_document.pdf which makes more sense to me. You can then also use \jobname to access the file name without any issues with underscores. The content of that macro is already detokenized.


 \title{Librairie \texttt{\jobname}}


 \section{Insertion of \texttt{\jobname}}


You should then always make sure that you don't use \input{\jobname} and compile the document without the --jobname argument. Otherwise you create an endless loop.

If you need this to only compile certain chapters of a document (which are all included using \include) use the following command instead. Here an example of compiling the introduction of a thesis only:

pdflatex --jobname introduction '\includeonly{introduction}\input{thesis}'

You can try

pdflatex "\edef\testpath{\detokenize{my_document}}\input{my_tex_document}"

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.