用于目录的 lstset 的自定义标题

用于目录的 lstset 的自定义标题

我想在我的自定义环境中使用图形标题的功能(可以有两个标题:一个用于目录,一个用于图形下方/上方的标题)filecode。 MWE:

\documentclass[11pt,a4paper]{article}
\usepackage{listings}
\usepackage{color}
\usepackage{courier}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{frame=tb,
  language=C++,
  showstringspaces=false,
  columns=fullflexible,
  basicstyle={\scriptsize\ttfamily},
  numbers=left,
  numbersep=4pt,
  numberstyle=\scriptsize\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{gray}\upshape,
  stringstyle=\color{mauve},
  moredelim=[s][\color{gray}]{@}{\ },
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\newenvironment{filecode}[1][]
  {\vspace{1ex} \minipage[0.5\textheight]{\linewidth}% \begin{filecode}[#1]
   \lstset{basicstyle=\fontfamily{pcr}\footnotesize,#1}}
  {\endminipage \vspace{1em}}% \end{filecode}

\begin{document}

\begin{filecode}[label=lst:code,caption=Very long and frustrating caption that noone want to see before this page]
  \lstinputlisting{main.cpp}
\end{filecode}

\end{document}

答案1

使用

caption={[Short caption]Very long and frustrating caption that noone want to see before this page}

完整代码:

\documentclass[11pt,a4paper]{article}
\usepackage{listings}
\usepackage{color}
\usepackage{courier}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{frame=tb,
  language=C++,
  showstringspaces=false,
  columns=fullflexible,
  basicstyle={\scriptsize\ttfamily},
  numbers=left,
  numbersep=4pt,
  numberstyle=\scriptsize\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{gray}\upshape,
  stringstyle=\color{mauve},
  moredelim=[s][\color{gray}]{@}{\ },
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\newenvironment{filecode}[1][]
  {\vspace{1ex} \minipage[0.5\textheight]{\linewidth}% \begin{filecode}[#1]
   \lstset{basicstyle=\fontfamily{pcr}\footnotesize,#1}}
  {\endminipage \vspace{1em}}% \end{filecode}

\begin{document}
\lstlistoflistings
\begin{filecode}[label=lst:code,caption={[Short caption]Very long and frustrating caption that noone want to see before this page}]
  \lstinputlisting{main.cpp}
\end{filecode}

\end{document}

在此处输入图片描述

相关内容