# How do I change the list of listings title font and list of listings entry spacing?

I've set some formatting in my table of contents, list of tables, and list of figures that I want to continue in my list of listings (using tocloft package). However, I can't find the commands to do so. Consider the following example:

\documentclass[12pt,letterpaper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{listings}
\renewcommand{\lstlistingname}{Code} %change 'listing' to 'code'
%
%
\begin{document}
%
%Adjusted the font of the list of listings title
\renewcommand{\lstlistlistingname}{\centering\bfseries\normalsize{LIST OF CODE}}
%
\lstlistoflistings
\vspace{1in}
%
%test listings
\begin{lstlisting}[caption=exlist]
test code
\end{lstlisting}
\begin{lstlisting}[caption=exlist2]
test code 2
\end{lstlisting}
%
\end{document}


The result looks like this:

I want to do a few things.

1) I want to control the distance from the top margin to the LIST OF CODE title similar to \setlength{\cftbeforetoctitleskip}{1in} in tocloft. Is there a way to do this with the listings package? Update: This distance is controlled by tocloft. Thus, use \setlength{\cftbeforetoctitleskip}{\spaceBeforeChapterTitles}.

2) I want to control the vskip between the LIST OF CODE title and the first line similar to \setlength{\cftaftertoctitleskip}{12pt} in tocloft. Update: This distance is controlled by tocloft as well. Thus, use \setlength{\cftaftertoctitleskip}{\spaceBetweenTitleAndText}.

3) I want to control the vskip values in between the list of listings entries (i.e. 12pt skip between exlist and exlist2 lines) like I do with \setlength{\cftbeforechapskip}{12pt} in tocloft.

4) I want to control the space between dots, similar to \renewcommand{\cftdotsep}{1.5} in tocloft.

5) I want there to be a space between listings that come from different chapters, just as is done automatically in a list of figures or list of tables.

I think I may be able to create a new list of code using tocloft, but I really like how listings color codes my scripts.

• The \listoflistings settings can't be controlled with tocloft that way.
– user31729
Sep 22 '17 at 19:26
• Any news here? Did you try to adjust it in another way? Jan 1 '18 at 9:26
• No, I gave up for now. I'll have to come back to it eventually though and I'll see if I remember to give an update here. Mar 11 '18 at 19:10
• update: I discovered that #1 and #2 are controlled by the tocloft settings. Thus, the following commands will also change the list of listings: \setlength{\cftbeforetoctitleskip}{\spaceBeforeChapterTitles}. However, I'm still looking for solutions for #3, #4, and I'm adding #5. Apr 8 '20 at 20:45

I've combed various forums and come up with a solution that works to address all five questions. Please see the code below for comments and explanations.

\documentclass[12pt,letterpaper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}

\usepackage{tocloft} %allow us to change some of the table contents, list of (whatever)
\usepackage{caption}

%Found a nice reference here: https://texblog.org/2008/07/13/define-your-own-list-of/
%Let's define a new list called List of Code
\newlistof{code}{loc}{\MakeUppercase{List of Code}}
%{id of list}{abbreviation for the list}{name of list}

%%%%%%%%%%%%% Answer to Question #1 and #2. %%%%%%%%%%%%%
%Control the spacing before and after the title with the following commands,
%analogous to how we control the spacing in toc.
\setlength{\cftbeforeloctitleskip}{1in}
\setlength{\cftafterloctitleskip}{1in}

%Format the title of the list. Center with an \hfill on either side
\renewcommand{\cftloctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftafterloctitle}{\hfill\space}

%%%%%%%%%%%%%%%%% Answer to Question #3 %%%%%%%%%%%%%%%
%Adjust the vskip before each new list entry with the following:
\setlength{\cftbeforecodeskip}{.5\normalbaselineskip}

%%%%%%%%%%%%%%%%% Answer to Question #4 %%%%%%%%%%%%%%%
%set the distance between TOC dots (default 4.5) with the following:
\renewcommand{\cftcodedotsep}{1.5}

%%%%%%%%%%%%%%%%% Answer to Question #5 %%%%%%%%%%%%%%%
% a quick patch so a space is added between chapters (like is done in toc, lof, lot)
%https://tex.stackexchange.com/questions/461521/how-to-create-space-between-chapter-in-list-of-listings
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@chapter}% <cmd>
{}{}% <success><failure>
\makeatother
%end of patch

% Note that all the changes we've made have been separate from the listings package.
% We will be using our own caption system based on a new environment instead of
% using a caption system for lstlistings.
% Let's now make the new environment.
\newenvironment{coding}[1]{% require one input (the caption)
\refstepcounter{code} %increase the counter. Note that we reset it later (per chapter)
%
%
% now format the environment
\par %make sure it starts on a new line
\vspace{\baselineskip} %this was previously taken care of with \lstset, but we need
% to manually add it here to have the space be above our custom caption.
\noindent\textbf{Code \thechapter.\thecode: #1} %format the caption
{\protect\numberline{\thechapter.\thecode}#1}
\par}%
{} %Nothing special needs to happen in closing the environment.

\makeatletter
\@addtoreset{code}{chapter} %reset the list of code counter at each new chapter
\renewcommand{\p@code}{\thechapter.} % Want \autoref to include the chapter number.
\makeatother

\usepackage{listings} % minted is also an option, but I wanted to stick with
% listings because it seems to be updated more regularly.
\usepackage{color}
\usepackage{xcolor}
%New colors defined below
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%Code listing style named "mystyle"
\lstdefinestyle{mystyle}{% define my own style. Lots of options to choose from.
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=t, %caption position t or b
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle} %set the new style
\lstset{%aboveskip=\baselineskip, %don't want this aboveskip because it would put a
% space between our custom caption and the code.
belowskip=\baselineskip}

%https://tex.stackexchange.com/questions/207744/no-autoref-name-for-appendix
\usepackage{hyperref} %for referencing the code
\newcommand*{\codeautorefname}{Code}

\begin{document}

\listofcode

\chapter{The First Chapter}
I've written some sample code in \autoref{code1}. I hope you enjoy!

\begin{coding}{This is my new environment caption}
\label{code1} %Include the label for cross referencing here.

%This example would insert an entire matlab script.
%I'm commenting it out for simplicity of posting here.
% \lstinputlisting[language=Matlab,%
% % caption=SigmaExtract.m,%      Don't include the caption here.
% % label={code:SigmaExtract},%   Don't include label here.
% ]{SigmaExtract.m}

%Instead, I'll show a simple portion of script here:
\begin{lstlisting}[language=matlab]
function TestFunction()
%%% This is a sample function.

for i = 1:10
fprintf('i = %0.0f\n',i)
end; clear i

end
\end{lstlisting}

\end{coding}

\begin{coding}{The second coding section}
\begin{lstlisting}[language=matlab]
function TestFunction()
%%% This is another sample function.

disp('Hello World')

end
\end{lstlisting}
\end{coding}

\chapter{Even more great content}

\begin{coding}{The third great function}
\begin{lstlisting}[language=matlab]
function j = TestFunction(i)
%%% This is another sample function.

j = i+1;

end
\end{lstlisting}
\end{coding}

\end{document}


The List of Code (Page 1):

Some sample code with demonstration of \autoref (Page 2)

Last page for completeness (Page 3)

• Note that solution for #5 doesn't work if you're using the newfloat package in your document. Apr 10 '20 at 14:39