Text Formattinglistings | Customize List of Listings

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

When I tried the code, I noticed there may be a problem for hyperref with the space in the redefinition of the listings counter presentation, since hyperref uses this for hyperlinking.

In that case, you could use \space instead:

Code: Select all

\renewcommand*{\thelstlisting}{Code\space\arabic{lstlisting}}
Stefan
LaTeX.org admin

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

Well, it's working, but now my ToC is messed :
ToC.jpg
ToC.jpg (11.08 KiB) Viewed 6939 times
It was "Chapter <number>" with its title below it. Now the Chapter number is superposed with the title.
Last edited by Cham on Thu Feb 23, 2012 12:53 pm, edited 3 times in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: listings | Customize List of Listings

Post by Stefan Kottwitz »

My ToC is ok. Perhaps show it with code (as always). Seems like overprinting.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

I simply added this to the preamble, to modify the ToC appearance :

Code: Select all

\usepackage{tocloft}
	\setlength{\cftchapnumwidth}{0pt}  % Pour ajouter un espace devant le titre des chapitres.
	\setlength{\cftsecnumwidth}{35pt}  % Pour ajouter un espace devant le titre des sections.
	\renewcommand{\cftchappresnum}{\chaptername\ }
	\setlength{\cftsubsecindent}{53pt}  % Pour ajouter un espace devant le No. des sous-sections.
	%\renewcommand{\cftchapaftersnum}{~:}  % Pour écrire « Chapitre : »
	\renewcommand{\cftchapaftersnumb}{\newline}
	\renewcommand{\cftchapdotsep}{\cftdotsep}  % Ajoute les points . . . devant les chapitres.
	\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}} 
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

When I do this, with your code, I get:
toc.png
toc.png (3.76 KiB) Viewed 6937 times
So, not the problem.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

And it is worst than that : The list of figures is losing "Figure". It's now just a list of chapter numbers with the figure's caption.

Ok, I have enough. I give up.

I return to the default list of listings with just <number> <caption>.
It is way too much work and frustration just to add "Code" and ":" in the list of listings. :evil:

To me, it is now clear that the listings package is uncomplete. It's not even consistent with the default list of figures and tables.
The Prophet
Posts: 3
Joined: Sat Mar 16, 2013 1:04 am

listings | Customize List of Listings

Post by The Prophet »

Hello,

I know it is an old thread, but I have the exact same problem like Cham. I want to customize the look of "List of Listings" like "List of Figures" or "List of Tables". The suggested snippets working partly to me. What i get is just a list of numbers.

For "List of Figures" and "List of Tables" this Code works like a charm.

Code: Select all

\counterwithout{figure}{chapter}                           
\counterwithout{table}{chapter}
\renewcommand{\thefigure}{\bfseries\arabic{figure}}
\renewcommand{\thetable}{\bfseries\arabic{table}}
\renewcommand{\cfttabpresnum}{\textbf{Tabelle }}
\renewcommand{\cftfigpresnum}{\textbf{Abbildung }}
\renewcommand{\cftfigaftersnum}{\textbf{:}}
\renewcommand{\cfttabaftersnum}{\textbf{:}}
\setlength{\cftfignumwidth}{3.1cm}
\setlength{\cfttabnumwidth}{2.5cm}
\setlength{\cftfigindent}{0cm}
\setlength{\cfttabindent}{0cm}
But there seems nothing for "List of listings"?

EDIT:

Code: Select all

\makeatletter
%\renewcommand{\numberline}[1]{\hb@xt@\@tempdima{#1: }}
\renewcommand\l@lstlisting[2]{\@dottedtocline{1}{0cm}{4em}{#1}{#2}}
\makeatother
\begin{document}
\counterwithout{lstlisting}{chapter}
\renewcommand*{\thelstlisting}{Code~\arabic{lstlisting}}
Screen_1
Screen_1
screen_1.png (36.11 KiB) Viewed 6549 times

Code: Select all

\makeatletter
\renewcommand{\numberline}[1]{\hb@xt@\@tempdima{#1: }}
\renewcommand\l@lstlisting[2]{\@dottedtocline{1}{0cm}{4em}{#1}{#2}}
\makeatother
\begin{document}
\counterwithout{lstlisting}{chapter}
\renewcommand*{\thelstlisting}{Code~\arabic{lstlisting}}
Screen_2
Screen_2
screen_2.png (34.28 KiB) Viewed 6549 times

EDIT SOLUTION:

If you use minted to format listings like i do then minted uses internally package "listing" do not confuse this with "listings" package!

Override "list of listings (lol)" commandos

Code: Select all

\makeatletter
\renewcommand\l@listing[2]{\@dottedtocline{1}{0em}{2.5em}{\textbf{Quellcode}~#1}{#2}} %Bezeichner im Verzeichnis
\renewcommand\thelisting{\textbf{\arabic{listing}:}} 
\makeatother
add to toc and rename the points

Code: Select all

\renewcommand\listoflistingscaption{Quellcodeverzeichnis} %Bezeichner des Verzeichnisses
\renewcommand\listingname{Quellcode}%Bezeichner des Listings
\clearpage % notwendig sonst stimmt seitenzahl nicht 
\addcontentsline{toc}{chapter}{Quellcodeverzeichnis} % Eintrag im Inhaltsverzeichnis
\listoflistings % Listing ausführen
Format "list of figures (lot)" and "list of tables (lot)"

Code: Select all

\counterwithout{figure}{chapter}                                                 
\counterwithout{table}{chapter} 
\renewcommand{\thefigure}{\bfseries\arabic{figure}}
\renewcommand{\thetable}{\bfseries\arabic{table}} 
\renewcommand{\cfttabpresnum}{\textbf{Tabelle }} 
\renewcommand{\cftfigpresnum}{\textbf{Abbildung }} 
\renewcommand{\cftfigaftersnum}{\textbf{:}} 
\renewcommand{\cfttabaftersnum}{\textbf{:}} 
\setlength{\cftfignumwidth}{3,1cm}                     
\setlength{\cfttabnumwidth}{2,5cm}                                           
\setlength{\cftfigindent}{0cm}                                                     
\setlength{\cfttabindent}{0cm}
and finally customize the captions

Code: Select all

\usepackage[labelsep=space,labelformat=simple,labelfont=bf]{caption}
\captionsetup[figure]{labelsep=colon,labelformat=simple,labelfont=bf}
\captionsetup[table]{labelsep=colon,labelformat=simple,labelfont=bf}
Post Reply