Search code examples
latexlistings

Issue with Spacing in List of Listings with tcolorbox


I am experiencing an issue with the spacing between the title and page number in the list of listings in my LaTeX document. I am using the tcolorbox package for colored frames, and listings to manage the listings.

Here is a minimal example illustrating the problem:

\documentclass{article}
\usepackage{tcolorbox} % Package for colored boxes
\usepackage{listings} % Package for listings (we use our own commands for lists)
\usepackage{tocloft} % Package for managing lists of contents

% Definition of the myColorFrame environment
\newcounter{listing}
\newenvironment{myColorFrame}[2][blue]{%
    \refstepcounter{listing}%
    \addcontentsline{lol}{listing}{Code~\thelisting:~#2}% Adds entry to the list of listings
    \begin{tcolorbox}[colframe=#1,title={Listing~\thelisting: #2}]%
}{%
    \end{tcolorbox}
}

\begin{document}
\section{Intro}
\begin{myColorFrame}{red}{Example listing}
  This is a simulated listing.
\end{myColorFrame}

% Adding the list of listings
\renewcommand{\lstlistingname}{Listing}
\renewcommand{\lstlistlistingname}{List of Listings}
\lstlistoflistings
\end{document}

In the generated list of listings, the entry appears as:

List of Listings Code 1: Example listing1

There is no space between the title "Code 1: Example listing" and the page number "1". I need to add some space between the title and the page number.

Thank you in advance for your help!

Expected Outcome:

There should be some space between the title "Code 1: Example listing" and the page number in the list of listings.

Additional Information:

The issue persists even when using different options or modifications to the tcolorbox and listings packages. I am looking for a solution that allows adding a fixed amount of space between the title and the page number.


Solution

  • \documentclass{article}
    \usepackage{tcolorbox} % Package for colored boxes
    \usepackage{listings} % Package for listings (we use our own commands for lists)
    \usepackage{tocloft} % Package for managing lists of contents
    
    % Definition of the myColorFrame environment
    \newcounter{listing}
    \newenvironment{myColorFrame}[2][blue]{%
        \refstepcounter{listing}%
        \addcontentsline{lol}{listing}{Code~\thelisting:~#2\hfill}% Adds entry to the list of listings
        \begin{tcolorbox}[colframe=#1,title={Listing~\thelisting: #2}]%
    }{%
        \end{tcolorbox}
    }
    
    \begin{document}
    \section{Intro}
    \begin{myColorFrame}{red}{Example listing}
      This is a simulated listing.
    \end{myColorFrame}
    
    % Adding the list of listings
    \renewcommand{\lstlistingname}{Listing}
    \renewcommand{\lstlistlistingname}{List of Listings}
    \lstlistoflistings
    \end{document}
    

    enter image description here