[Tex/LaTex] Footnotemark in caption of listing causing problem


I have the following listing:

   \begin{lstlisting}[float=h,caption={Description\footnotemark.}, label={listing2},
        keywordstyle=\color{blue}\bfseries, ndkeywordstyle=\color{black}\bfseries, commentstyle=\color{red}\ttfamily,
        stringstyle=\color{green}\ttfamily, identifierstyle=\color{black},backgroundcolor=`\color{white},frame=single, frameround=ffff,captionpos=b,basicstyle=\scriptsize]`




When I compile, there is a fatal error: latex Text Capacity exceeded, sorry [input stack size=5000]

Problem is at \footnotemark, how I can solve this problem?

Best Answer

The \footnotemark command is a fragile so it needs to be protected with \protect when used as a "moving argument":

\begin{lstlisting}[float=h,caption={Description\protect\footnotemark.}, label={listing2}, ...]



In essence, "moving arguments" are macros that are passed to some other macro that eventually write them to an external file (like the ToC or list of listings). This typically involves complete expansion of their content. The \protect basically postpones this expansion until content is read back by LaTeX.

The complete topic is quite LaTeXnical: When and if \protect is needed depends not only on the implementation of the macro passed as argument (\footnotemark in this case), but also the macro that processes it in its argument (which is \lst@MakeCaption in this case). Hence, it might well be the case that for some specific caption-like commands it is not necessary. The rule of thumb is: If it breaks with a weird error message, try \protect.

The glory details of fragile and robust commands are very well explained in What is the difference between Fragile and Robust commands?, the protection mechanism itself in Minimal \protected@edef example.

Related Question