In my tufte-book document I would like to show source code listings using the listings package but with their captions in the margin. Modifying the code for typesetting the listing caption is not straightforward, and so the easiest solution is to wrap the listing in a floating environment (see the second answer to the question "combining listings and tufte-book, listings caption on tufte's margin"). However, this does not work:

  • If you specify a label for the floating environment, labels for line number references (see the second answer to the question "Lstlistings reference to line number") will no longer be output to the auxiliary file, and the references will show up as "??".
  • If you omit the label for the floating environment, line number references are wrong (they always reference the first line).

Here is a minimal working example illustrating the different variants for the second option:

% textwidth Tuftian float for listings
% fullwidth Tuftian float for listings
% enable re-use of \listoflistings facility
% show listing number in caption even though \lst@@caption is empty


% regular Tuftian figure float, wrong caption and writes to lof
Dummy line
Labeled line |\label{line:test1}|
\caption{First test}
Reference to line \ref{line:test1}

% custom Tuftian listing float, correct caption and writes to lol
Dummy line
Labeled line |\label{line:test2}|
\caption{Second test}
Reference to line \ref{line:test2}

% lstlisting float, wrong caption but writes to lol
\begin{lstlisting}[escapechar=|,float=h,caption=Third test]
Dummy line
Labeled line |\label{line:test3}|
Reference to line \ref{line:test3}

The result looks as follows:


Now I would like to go with the second approach (since I would like my listings to appear under a "List of Listings" instead of a "List of Figures"), because it provides a caption in the margin and therefore nicely integrates with the remaining floating environments. However, line number references do not work, and I have no clue why. The relevant contents of the auxiliary file follows:

\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces First test}}{1}{lstnumber.-1.2}}
\@writefile{lol}{\contentsline {lstlisting}{\numberline {1}{\ignorespaces Second test}}{1}{lstnumber.-2.2}}
\@writefile{lol}{\contentsline {lstlisting}{\numberline {2}Third test}{1}{lstlisting.0.2}}
  • Rather than renaming the list of figures, I would like to have both a list of figures and a list of listings in my document. By writing \contentsline commands to the lol file rather than the lof file, I can re-use the \listoflistings command provided by the listings package. However, regardless of this, line number references do not work correctly... – Michael Rudolf Dec 11 '15 at 6:33

It seems that the issue is a redefinition of the \label command in the floating environment used by the tufte-latex package for figures and tables (and in the second example of the question also for the listing). Below is the corresponding code, which already contains a hint to the solution:

% Handle subfigure package compatibility
  % don't move the label while inside a \subfigure or \subtable command
}{}% subfigure package is not loaded

The tufte-latex document class detects whether the subfigure package has been loaded and sets the value of the Boolean accordingly. I came across this when using the subcaption package, which supercedes the subfig and subfigure packages - I could not reference the subfigures, because their labels were not output to the auxiliary file, just like the lables from the listings were missing. In order to retain the original behavior of the \label command also for listings and subcaption floats, I simply set the Boolean to true with the following lines in my document preamble:


This solved my problems with both listings and subfigures.

  • This unfortunately breaks the labels of regular figures and tables, so this Boolean switch should probably be used on a per-float basis. – Michael Rudolf Dec 15 '15 at 14:22

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.