TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to have a command to insert listings that contain the filename in the caption. This works well as long as there is no "_" etc. in the filename.

I also tried the suggestion from a previous Thread (input filename as caption in listing):


This works quite well, but fails if I want to insert a listoflistings and have included a file that contains an underscore.


Looking at the .lol file shows that the listoflistings command included the following line:

\contentsline {lstlisting}{\numberline {5.1}\texttt {test_file.cpp}}{36}{lstlisting.5.1}

I think the problem is that it does not contain the \detokenize command.

Any suggestions?

share|improve this question
up vote 4 down vote accepted

You want to add \protect in front of \detokenize


The \protect command will do nothing when the caption is typeset in the text, but will make LaTeX write it unchanged in the .lol file.

share|improve this answer
Thank you! That solved it. – Baertierchen Feb 24 '13 at 10:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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