2

The floatrow package enables one to put in the margin captions for figures and tables, but it seems there is missing the possibility of doing the same for the lstlisting environment from listings package.

A workaround is to put the lstlisting in a figure (see sample). But this doesn't allow us to keep the page breaking for long code listings. Is there a way to put lstlisting captions in the margin? I've looked inside the floatrow package to see if I can extend it, but I don't know how.

\documentclass{memoir}
\usepackage{listings}
\usepackage{floatrow}
\floatsetup[figure]{margins=hangright,capposition=beside,capbesideposition={top,left},floatwidth=\textwidth}

\begin{document}
\begin{figure}
  \begin{lstlisting}

    This is a test
  \end{lstlisting}
  \caption{This is a test.}
\end{figure}
\end{document}

The caption package has a way to put Fig X: inside the margin (see below), but putting the third argument within llap doesn't seem to work.

\DeclareCaptionFormat{llap}{\llap{#1#2}#3\par}
\captionsetup{lstlisting}{format=llap,labelsep=quad,singlelinecheck=no}
1
  • memoir and floatrow don't cooperate well. What exactly are you trying to achieve? Jul 13 '13 at 15:32
1

memoir and floatrow don't cooperate well. If you want side captions for table and figure, you can use memoir's \sidecap command (See section 10.10 SIDE CAPTIONS of the manual). In the case of listings produced with lstlisting, one has to do some additional work.

The following code (that admits improvements) shows one approach; the caption is stored in a box of width \sidecapwidth and then placed in the margin using \sidepar:

\documentclass{memoir}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{marginnote}
\usepackage{lipsum}

\newsavebox\mycap
\newlength\mycapHt

\setlength\sidecapwidth{77pt}

\DeclareCaptionFormat{llap}{%
  \begin{lrbox}{\mycap}
  \begin{minipage}{\sidecapwidth}
  \raggedright#1:#2#3
  \end{minipage}%
  \end{lrbox}%
  \settoheight\mycapHt{\usebox\mycap}%
  \addtolength\mycapHt{.85\baselineskip}%
  \setlength\sideparvshift{\mycapHt}%
  \sidepar{\usebox\mycap}}
\captionsetup[lstlisting]{format=llap,labelsep=space,singlelinecheck=no}

\begin{document}

\begin{figure}
\begin{sidecaption}{An illustration}[fig:ill]
\centering
\includegraphics[height=4cm]{example-image-a}
\end{sidecaption}
\end{figure}
\lipsum[4]
\begin{lstlisting}[caption={test caption for a listing}]
This is a test
This is a test
This is a test
This is a test
This is a test
\end{lstlisting}
\lipsum[4]
\begin{lstlisting}[caption={test caption for a listing that will span several lines}]
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
\end{lstlisting}
\lipsum[4]

\end{document}

enter image description here

2
  • Thanks. That's what I was aiming at. BTW, where would I look to find the information that would enable me to solve this on my own? I have the LaTeX Companion and I can do some simple macros, but things this customized seem to require knowing very well LaTeX internals.
    – user33612
    Jul 13 '13 at 16:43
  • @user33612 the description of everything I used here can be found in the LaTeX Companion and in the manual for memoir (or in the file memoir.cls). By the way, don't forget that (after a sensible wait for other possible answers) you can accept the answer that you consider best solved your problem by clicking on the checkmark to its left. In case of doubt, please see How do you accept an answer?. Jul 13 '13 at 17:23

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.