8

I am not very familiar with all latex details, but I'd like to use the listings package in a tufte-book document. However, the listings package places its captions above or below the listing and I couldn't find a way to adjust that in a way so that the caption is put to the page margin as with tufte-book's \marginnote etc.

Any idea how to solve that?

3

It is best neither to mess with the listings or the tufte-book commands, as they are both difficult to modify. I would simply load the listing in a \begin{figure}...\end{figure} environment. If you want to have a different float for listings, you can use the float package.

\documentclass{tufte-book}
\usepackage{listings}
\begin{document}
\begin{figure}
\begin{lstlisting}
This is a test
\end{lstlisting}
\caption{This is a test.}
\end{figure}
\end{document}
3

Thanks to the above answer, this is what I use now:

\newcommand{\cpplistinc}[1]{
    \renewcommand{\figurename}{Listing}
    \begin{figure}
    \lstinputlisting[style=C++11,caption={}]{../src/ch\thechapter/#1}
    \caption{#1}
    \label{ch\thechapter/#1}
    \index{#1 in chapter \thechapter } % might use a separate index for that.
    \end{figure}
    \renewcommand{\figurename}{Figure}
    }

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.