![要在目錄中使用的 lstset 的自訂標題](https://rvso.com/image/298809/%E8%A6%81%E5%9C%A8%E7%9B%AE%E9%8C%84%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%9A%84%20lstset%20%E7%9A%84%E8%87%AA%E8%A8%82%E6%A8%99%E9%A1%8C.png)
我想在我的自訂環境中使用圖形標題的功能(可以有兩個標題:一個用於目錄,一個用於圖下方/上方的標題)filecode
。微量元素:
\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}