页边距列表的标题

页边距列表的标题

floatrow包允许人们在图形和表格中添加边距标题,但似乎缺少对包lstlisting中的环境执行相同操作的可能性listings

一种解决方法是将 放在lstlisting图中(见示例)。但这不允许我们为长代码列表保留分页符。有没有办法将lstlisting标题放在页边距中?我查看了floatrow包内部,看看是否可以扩展它,但我不知道如何扩展。

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

caption包有一种方法可以将其放入Fig X:边缘内(见下文),但将第三个参数放入其中llap似乎不起作用。

\DeclareCaptionFormat{llap}{\llap{#1#2}#3\par}
\captionsetup{lstlisting}{format=llap,labelsep=quad,singlelinecheck=no}

答案1

memoir并且floatrow配合得不好。如果你想要表格和图片的侧面标题,你可以使用memoir命令\sidecap(参见10.10 侧面字幕手册)。对于使用 生成的列表lstlisting,还必须做一些额外的工作。

下面的代码(可以改进)展示了一种方法;将标题存储在一个宽度为的框中\sidecapwidth,然后使用 放置在边距中\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}

在此处输入图片描述

相关内容