Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\usepackage{listings}
\newcommand{\mylisting}[2][]{%
\lstinputlisting[caption={\texttt{\detokenize{#2}}},#1]{#2}%}

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

\lstlistoflistings

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
add comment

1 Answer

up vote 3 down vote accepted

You want to add \protect in front of \detokenize

\newcommand{\mylisting}[2][]{%
  \lstinputlisting[caption={\texttt{\protect\detokenize{#2}}},#1]{#2}}

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
add comment

Your Answer

 
discard

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.