# combining listings and tufte-book, listings caption on tufte's margin

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?

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}


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}
}