如何为列表指定简短的标题?

如何为列表指定简短的标题?

我的一个列表名称很长。在\lstlistoflistings目录中,我只想显示它的缩写版本。如何为我的列表指定缩写名称?

\documentclass[11pt]{book}
\usepackage[svgnames]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{avant}
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}

\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\newfontfamily{\lstconsolas}{Consolas}

\newcommand\ph\mlplaceholder

\renewcommand{\lstlistingname}{Matlab Algorithm}
\renewcommand{\lstlistlistingname}{List of \lstlistingname s}

\begin{document}

\lstlistoflistings

\lstinputlisting[style=Matlab-editor,basicstyle=\lstconsolas,caption=Sample code from Matlab with a very very very very very loooooooooooooooong name]{sample.m}

\begin{lstlisting}[
  style=Matlab-editor,
  basicstyle=\lstconsolas\small,
  escapechar=`,
  caption={For educational purposes},
]
% example of while loop using placeholders
while x2 = 1 + 100 `\ph{condition}`
  if `\ph{something-bad-happens}`
    break
  else
    % do something useful
  end
  % do more things
end
\end{lstlisting}

\end{document} 

答案1

listings'caption键允许你使用与

\caption[short title]{long title}

但是您必须用括号将传递给键的值括起来caption,以便“隐藏”可选短标题参数后面的右括号:

\lstinputlisting[
    % ...
    caption = {[Short and sweet!]Sample code from Matlab with a very very very very very loooooooooooooooong name},
]{sample.m}

在此处输入图片描述

相关内容