如何在 lstlisting 环境中设置标题的可选参数

如何在 lstlisting 环境中设置标题的可选参数

通常,如果我设置标题,我习惯\caption[short]{long}在图片列表中只添加简短描述。但是我该如何使用 来实现呢listings

这里是列表列表中具有非常长标题的 MWE:

\documentclass{article}
    \usepackage{listings}

\begin{document}

    \lstlistoflistings

    \clearpage
    \section{A section}
    \begin{lstlisting}[frame=ltrb,caption={This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption.}]
    1=1
    \end{lstlisting}
\end{document}

答案1

lstlisting可以用几乎相同的方式给出环境的简短标题,\caption={[short caption]long caption}请参阅手册第 4.9 节listings

请注意{}对争论的东西进行保护[],因为它们很脆弱。

{...}用于长标题也许会更好。

\documentclass{article}
    \usepackage{listings}

\begin{document}
\lstlistoflistings

\clearpage
\section{A section}
\begin{lstlisting}[frame=ltrb,caption={[short caption]{This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption. This is a very long caption.}}]
1=1
\end{lstlisting}
\end{document}

在此处输入图片描述

在此处输入图片描述

相关内容