如何向 "list of listings" 中的每个条目添加文本?
How to add text to each entry in the "list of listings"?
我需要在一个巨大的 XeLaTeX 文档中使用包 "listings" 来修改 "list of listings" 中的每个条目。我设法显示了章节编号、章节内的编号、标题、几个点以及每个条目的页码。但是,我无法在每一行的开头为每个条目包含单词 "CODE"。
我已经阅读了几个关于这个主题的论坛,但答案令人困惑而且不是决定性的。似乎最好的方法是使用 \thelstlisting
但我收到一条错误消息说它没有定义(顺便说一句,我对 Latex 有点陌生)。
现在是 MWE(最小工作示例):
\NeedsTeXFormat{LaTeX2e}
\documentclass{report}
\RequirePackage[spanish,english]{babel}
\RequirePackage{inputenc}
\RequirePackage{hyperref}
\RequirePackage[all]{hypcap}
\RequirePackage{caption}
\RequirePackage{listings}
\renewcommand{\lstlistlistingname}{\centerline{My own title for codes}}
\renewcommand{\lstlistingname}{Source code example}
\begin{document}
\chapter{Introduction}
\section{History}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vel hendrerit massa, vitae aliquam est. Nullam mauris lacus, scelerisque non risus sed, sagittis rutrum mi. Quisque malesuada sagittis gravida. Duis placerat interdum dui. Etiam eu nibh vel sapien laoreet posuere. Cras vel auctor arcu.
\lstinputlisting[language=Python, numberbychapter=true, frame=single, caption={How to do NNN}]{code.py}
Metus aliquam rutrum gravida. Nullam dapibus accumsan odio ut maximus. Aliquam bibendum felis nisl, eu faucibus ante placerat vitae. Suspendisse turpis est, ultricies a posuere non, pellentesque nec nunc. Sed pharetra quis sem nec tincidunt. Donec ultricies felis id risus faucibus.
%% FOLLOWING LINE DOESN'T GET ANY ERROR, IN FACT IT DOES NOTHING!!
\renewcommand{\thelstlisting}{CODE~\arabic{lstlisting}}
\lstlistoflistings
\end{document}
(文件 code.py 有几行 Python 代码)
清单中的第一行是:
1.1 如何做NNN ..................... 1
但是,单词 "CODE" 丢失了。它应该是:
CODE 1.1 如何做 NNN ..................... 1
命令 \renewcommand*{\thelstlisting}{CODE~\arabic{lstlisting}}
在文档中不起作用,在文档之前收到错误消息:未定义列表列表。
我该怎么做才能使每个条目的前面出现 CODE 一词?
你对 \thelstlisting
的重新定义来得太迟了。您需要将它放在列表之前。但是,它会弄乱标题和交叉引用等其他内容。相反,我建议修改列表中条目的格式:
\NeedsTeXFormat{LaTeX2e}
\documentclass{report}
\RequirePackage[spanish,english]{babel}
\RequirePackage{inputenc}
\RequirePackage{hyperref}
\RequirePackage[all]{hypcap}
\RequirePackage{caption}
\RequirePackage{listings}
\renewcommand{\lstlistlistingname}{\centerline{My own title for codes}}
\renewcommand{\lstlistingname}{Source code example}
\begin{filecontents*}{code.py}
zzz
\end{filecontents*}
\makeatletter
\def\l@lstlisting#1#2{\@dottedtocline{1}{3.4em}{2.3em}{Code #1}{#2}}
\makeatother
\begin{document}
\chapter{Introduction}
\section{History}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vel hendrerit massa, vitae aliquam est. Nullam mauris lacus, scelerisque non risus sed, sagittis rutrum mi. Quisque malesuada sagittis gravida. Duis placerat interdum dui. Etiam eu nibh vel sapien laoreet posuere. Cras vel auctor arcu.
\lstinputlisting[language=Python, numberbychapter=true, frame=single, caption={How to do NNN}]{code.py}
Metus aliquam rutrum gravida. Nullam dapibus accumsan odio ut maximus. Aliquam bibendum felis nisl, eu faucibus ante placerat vitae. Suspendisse turpis est, ultricies a posuere non, pellentesque nec nunc. Sed pharetra quis sem nec tincidunt. Donec ultricies felis id risus faucibus.
%% FOLLOWING LINE DOESN'T GET ANY ERROR, IN FACT IT DOES NOTHING!!
%\renewcommand{\thelstlisting}{CODE~\arabic{lstlisting}}
\lstlistoflistings
\end{document}
我需要在一个巨大的 XeLaTeX 文档中使用包 "listings" 来修改 "list of listings" 中的每个条目。我设法显示了章节编号、章节内的编号、标题、几个点以及每个条目的页码。但是,我无法在每一行的开头为每个条目包含单词 "CODE"。
我已经阅读了几个关于这个主题的论坛,但答案令人困惑而且不是决定性的。似乎最好的方法是使用 \thelstlisting
但我收到一条错误消息说它没有定义(顺便说一句,我对 Latex 有点陌生)。
现在是 MWE(最小工作示例):
\NeedsTeXFormat{LaTeX2e}
\documentclass{report}
\RequirePackage[spanish,english]{babel}
\RequirePackage{inputenc}
\RequirePackage{hyperref}
\RequirePackage[all]{hypcap}
\RequirePackage{caption}
\RequirePackage{listings}
\renewcommand{\lstlistlistingname}{\centerline{My own title for codes}}
\renewcommand{\lstlistingname}{Source code example}
\begin{document}
\chapter{Introduction}
\section{History}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vel hendrerit massa, vitae aliquam est. Nullam mauris lacus, scelerisque non risus sed, sagittis rutrum mi. Quisque malesuada sagittis gravida. Duis placerat interdum dui. Etiam eu nibh vel sapien laoreet posuere. Cras vel auctor arcu.
\lstinputlisting[language=Python, numberbychapter=true, frame=single, caption={How to do NNN}]{code.py}
Metus aliquam rutrum gravida. Nullam dapibus accumsan odio ut maximus. Aliquam bibendum felis nisl, eu faucibus ante placerat vitae. Suspendisse turpis est, ultricies a posuere non, pellentesque nec nunc. Sed pharetra quis sem nec tincidunt. Donec ultricies felis id risus faucibus.
%% FOLLOWING LINE DOESN'T GET ANY ERROR, IN FACT IT DOES NOTHING!!
\renewcommand{\thelstlisting}{CODE~\arabic{lstlisting}}
\lstlistoflistings
\end{document}
(文件 code.py 有几行 Python 代码)
清单中的第一行是:
1.1 如何做NNN ..................... 1
但是,单词 "CODE" 丢失了。它应该是:
CODE 1.1 如何做 NNN ..................... 1
命令 \renewcommand*{\thelstlisting}{CODE~\arabic{lstlisting}}
在文档中不起作用,在文档之前收到错误消息:未定义列表列表。
我该怎么做才能使每个条目的前面出现 CODE 一词?
你对 \thelstlisting
的重新定义来得太迟了。您需要将它放在列表之前。但是,它会弄乱标题和交叉引用等其他内容。相反,我建议修改列表中条目的格式:
\NeedsTeXFormat{LaTeX2e}
\documentclass{report}
\RequirePackage[spanish,english]{babel}
\RequirePackage{inputenc}
\RequirePackage{hyperref}
\RequirePackage[all]{hypcap}
\RequirePackage{caption}
\RequirePackage{listings}
\renewcommand{\lstlistlistingname}{\centerline{My own title for codes}}
\renewcommand{\lstlistingname}{Source code example}
\begin{filecontents*}{code.py}
zzz
\end{filecontents*}
\makeatletter
\def\l@lstlisting#1#2{\@dottedtocline{1}{3.4em}{2.3em}{Code #1}{#2}}
\makeatother
\begin{document}
\chapter{Introduction}
\section{History}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vel hendrerit massa, vitae aliquam est. Nullam mauris lacus, scelerisque non risus sed, sagittis rutrum mi. Quisque malesuada sagittis gravida. Duis placerat interdum dui. Etiam eu nibh vel sapien laoreet posuere. Cras vel auctor arcu.
\lstinputlisting[language=Python, numberbychapter=true, frame=single, caption={How to do NNN}]{code.py}
Metus aliquam rutrum gravida. Nullam dapibus accumsan odio ut maximus. Aliquam bibendum felis nisl, eu faucibus ante placerat vitae. Suspendisse turpis est, ultricies a posuere non, pellentesque nec nunc. Sed pharetra quis sem nec tincidunt. Donec ultricies felis id risus faucibus.
%% FOLLOWING LINE DOESN'T GET ANY ERROR, IN FACT IT DOES NOTHING!!
%\renewcommand{\thelstlisting}{CODE~\arabic{lstlisting}}
\lstlistoflistings
\end{document}