余白のリストのキャプション

余白のリストのキャプション

この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配置する方法がありますが(下記参照)、3 番目の引数を内部に配置すると機能しないようです。Fig X:llap

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

答え1

memoirうまく連携しません。表や図のサイドキャプションが必要な場合は、のコマンドfloatrowを使用します(セクションmemoir\sidecap10.10 サイドキャプション(マニュアルの)を使用して作成されたリストの場合はlstlisting、追加の作業を行う必要があります。

次のコード (改善の余地あり) は 1 つのアプローチを示しています。キャプションは幅のボックスに格納され\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}

ここに画像の説明を入力してください

関連情報