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:

\renewcommand{\lstlistingname}{Code} %change 'listing' to 'code'
%Adjusted the font of the list of listings title
\renewcommand{\lstlistlistingname}{\centering\bfseries\normalsize{LIST OF CODE}}
%test listings
test code
test code 2

The result looks like this: compilation result

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.

Thank you in advance!

  • 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?
    – TeXnician
    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.
    – jkmey
    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.
    – jkmey
    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.


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

%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.

%Format the title of the list. Center with an \hfill on either side

%%%%%%%%%%%%%%%%% Answer to Question #3 %%%%%%%%%%%%%%%
%Adjust the vskip before each new list entry with the following:

%%%%%%%%%%%%%%%%% Answer to Question #4 %%%%%%%%%%%%%%%
%set the distance between TOC dots (default 4.5) with the following:

%%%%%%%%%%%%%%%%% Answer to Question #5 %%%%%%%%%%%%%%%
% a quick patch so a space is added between chapters (like is done in toc, lof, lot)
\patchcmd{\@chapter}% <cmd>
  {\addtocontents}% <search>
  {\addtocontents{loc}{\protect\addvspace{10\p@}}% Add per-chapter space in LoL
   \addtocontents}% <replace>
  {}{}% <success><failure>
%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
\addcontentsline{loc}{code} %add entry to the list of code
{} %Nothing special needs to happen in closing the environment.

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

\usepackage{listings} % minted is also an option, but I wanted to stick with 
% listings because it seems to be updated more regularly.
%New colors defined below
%Code listing style named "mystyle"
\lstdefinestyle{mystyle}{% define my own style. Lots of options to choose from.
  backgroundcolor=\color{backcolour},   commentstyle=\color{codegreen},
  captionpos=t, %caption position t or b
\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.

\usepackage{hyperref} %for referencing the code



\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:
function TestFunction()
%%% This is a sample function.

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



\begin{coding}{The second coding section}
function TestFunction()
%%% This is another sample function.

disp('Hello World')


\chapter{Even more great content}

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

j = i+1;



The List of Code (Page 1): List of Code (Page 1)

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

Last page for completeness (Page 3) Last page for completeness (Page 3)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.