Reputation: 23
I need to use the package "listings" in a huge XeLaTeX document to modify each entry in the "list of listings". I managed to show the chapter number, the number within chapter, the caption, several dots, and the page number for each entry. However, I have been unable to include the word "CODE" for each entry at the beginning of each line.
I have read several forums regarding this topic, but the answers are confusing and not conclusive. It seems the best way is employing \thelstlisting
but I got an error saying it is not defined (btw, I'm kinda new to Latex).
Now a MWE (minimal working example):
\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}
(file code.py has few lines of Python code)
The first line in the listings is:
1.1 How to do NNN .............. 1
However, the word "CODE" is missing. It should read:
CODE 1.1 How to do NNN .............. 1
The command \renewcommand*{\thelstlisting}{CODE~\arabic{lstlisting}}
is not working within the document, and before the document it gets an error that says: thelstlisting is undefined.
What could I do to make the word CODE appear at front of each entry?
Upvotes: 2
Views: 1231
Reputation: 38748
Your redefinition of \thelstlisting
comes too late. You would need to place it before the listsings. However there it would mess up the captions and other things like cross references. Instead I suggest to modify the format of the entries in the list of listings:
\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}
Upvotes: 1