# How to fix minted and listings hyphen alignment on my list of codes?

I managed to put both listings and minted codes in a single list of codes called LISTINGS. However, the entries of the minted package on the list of codes are shorted on the distance between the listing name and the listing title.

This causes two problems.

The first problem is that when I have more than 10 items, they are eaten by the hyphen of the minted code environment, instead of expanding to hold the hyphen. On my thesis, this problems happens right after the 10th item, as showed on the next figure:

However, when I tried to create a minimal example, it only happened after 1000th item, as showed on the next figure:

First problem minimal example

\documentclass[english,12pt,a4paper,twoside,chapter=TITLE,section=TITLE,]{abntex2}
\usepackage{caption,listings}
\usepackage[newfloat,chapter,outputdir=F:/SublimeText/Data/Cache/LaTeXTools/b98d701d6340232fe19ea13f16f5a620]{minted}

% https://tex.stackexchange.com/questions/320185/adjust-vertical-spacing-between-caption-and-listing-in-koma-scrartcl
\captionsetup[listing]{position=top,skip=0pt}
\newenvironment{code}{\captionsetup{type=listing}}{}

% https://tex.stackexchange.com/questions/26220/control-spacing-around-table-caption
\AtBeginEnvironment{code}{\setcounter{listing}{\value{lstlisting}}
\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\parskip}{0pt}
\captionsetup{skip=0pt}
}
\AtEndEnvironment{code}{\stepcounter{lstlisting}}

% https://tex.stackexchange.com/questions/269491/mixing-minted-with-lstlisting
\AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
\AtEndEnvironment{listing}{\stepcounter{lstlisting}}

\makeatletter
\newlength\mylen

\begingroup
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\endgroup

% Why the empty space size is increasing each call to my calculate listing header command?
% https://tex.stackexchange.com/questions/388411/why-the-empty-space-size-is-increasing-each-call
\newlength\cftlstlistingoldnumwidth
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}

% Calculate the size of the header
%
% What is the use of percent signs (%) at the end of lines?
% https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}%
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}%
\setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\mylen}%
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}

% Ensure it is called at least one time
\calculatelisteningsheader

% https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
\AtBeginDocument{\calculatelisteningsheader}
\makeatother

\begin{document}

% https://tex.stackexchange.com/questions/511519/latex-keeps-showing-minted-environment-as-figures-instead-of-listening
{\renewcommand{\cftfigurename}{\listingname\space}
\lstlistoflistings
}
% \newpage

\begin{code}
\caption{Code}
\begin{minted}{text}
$python3 unit_tests.py OK \end{minted} \end{code} % \newpage % ... more 1892 items equal \begin{code} \caption{Code} \begin{minted}{text}$ python3 unit_tests.py
OK
\end{minted}
\end{code}
% \newpage

\end{document}


The second problem is that when I use both listings and minted codes, they became unaligned on list of codes (See figure).

Second problem minimal example

\documentclass[english,12pt,a4paper,twoside,chapter=TITLE,section=TITLE,]{abntex2}
\usepackage{caption,listings}
\usepackage[newfloat,chapter,outputdir=F:/SublimeText/Data/Cache/LaTeXTools/b98d701d6340232fe19ea13f16f5a620]{minted}

% https://tex.stackexchange.com/questions/320185/adjust-vertical-spacing-between-caption-and-listing-in-koma-scrartcl
\captionsetup[listing]{position=top,skip=0pt}
\newenvironment{code}{\captionsetup{type=listing}}{}

% https://tex.stackexchange.com/questions/26220/control-spacing-around-table-caption
\AtBeginEnvironment{code}{\setcounter{listing}{\value{lstlisting}}
\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\parskip}{0pt}
\captionsetup{skip=0pt}
}
\AtEndEnvironment{code}{\stepcounter{lstlisting}}

% https://tex.stackexchange.com/questions/269491/mixing-minted-with-lstlisting
\AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
\AtEndEnvironment{listing}{\stepcounter{lstlisting}}

\makeatletter
\newlength\mylen

\begingroup
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\endgroup

% Why the empty space size is increasing each call to my calculate listing header command?
% https://tex.stackexchange.com/questions/388411/why-the-empty-space-size-is-increasing-each-call
\newlength\cftlstlistingoldnumwidth
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}

% Calculate the size of the header
%
% What is the use of percent signs (%) at the end of lines?
% https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}%
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}%
\setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\mylen}%
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}

% Ensure it is called at least one time
\calculatelisteningsheader

% https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
\AtBeginDocument{\calculatelisteningsheader}
\makeatother

\begin{document}

% https://tex.stackexchange.com/questions/511519/latex-keeps-showing-minted-environment-as-figures-instead-of-listening
{\renewcommand{\cftfigurename}{\listingname\space}
\lstlistoflistings
}
% \newpage

\begin{lstlisting}[caption={Listing}]
# If the body of the namespace is longer than this
# number, it won't be indented. Requires
\end{lstlisting}
% \newpage

\begin{code}
\caption{Code}
\label{unitTestsResults}
\begin{minted}{text}
$python3 unit_tests.py OK \end{minted} \end{code} % \newpage \begin{lstlisting}[caption={Listing}] # If the body of the namespace is longer than this # number, it won't be indented. Requires \end{lstlisting} % \newpage \begin{code} \caption{Code} \label{unitTestsResults} \begin{minted}{text}$ python3 unit_tests.py
OK
\end{minted}
\end{code}
% \newpage

\end{document}


Related questions:

## 2 Answers

I managed to fix the second problem:

With this:

\newenvironment{code}{
\captionsetup{type=listing}

% https://tex.stackexchange.com/questions/514847/xpatch-fails-if
\def\makesafecoloncatcoderestore##1{%
\edef##1{\catcode:=\the\catcode:\relax}%
}

% https://tex.stackexchange.com/questions/335104/save-and-restore-the
\makesafecoloncatcoderestore\restorecodecoloncatcode
\catcode\:=12

\xpatchcmd{\addcontentsline}
{\contentsline}
{\specialcodelistingcontentsline}{
% \message{^^J^^JCould YES patch the contentsline environment!^^J^^J^^J^^J^^J}
}
{\FAILEDPATCHSPECIALCONTENTSLINE}

% \xshowcmd\addcontentsline
\restorecodecoloncatcode
}{}


When the example was originally generated, the contents of main.lol were this:

\contentsline {lstlisting}{\numberline {1}Listing}{1}{lstlisting.0.1}
\contentsline {listing}{\numberline {2}{\ignorespaces Code\relax }}{1}{listing.caption.2}
\contentsline {lstlisting}{\numberline {3}Listing}{1}{lstlisting.0.3}
\contentsline {listing}{\numberline {4}{\ignorespaces Code\relax }}{1}{listing.caption.3}


Then, my fix, changes it to this:

\contentsline {lstlisting}{\numberline {1}Listing}{1}{lstlisting.0.1}
\specialcodelistingcontentsline {listing}{\numberline {2}{\ignorespaces Code\relax }}{1}{listing.caption.2}
\contentsline {lstlisting}{\numberline {3}Listing}{1}{lstlisting.0.3}
\specialcodelistingcontentsline {listing}{\numberline {4}{\ignorespaces Code\relax }}{1}{listing.caption.3}


On which the function \specialcodelistingcontentsline just eats/throw away the first parameter for \contentsline (listing) and puts lstlisting in place of it. Then, when latex renders the toc, it correctly prints everything as lstlisting.

Full second example:

\PassOptionsToPackage{french}{babel}
\documentclass[english,12pt,a4paper,twoside]{abntex2}

\usepackage{caption,xpatch,listings}
\usepackage[newfloat,chapter]{minted}
\makeatletter

% https://tex.stackexchange.com/questions/320185/adjust-vertical-spacing
\captionsetup[listing]{position=top,skip=0pt}
\tracingpatches
\newcommand{\specialcodelistingcontentsline}[1]{\contentsline{lstlisting}}

\newenvironment{code}{
\captionsetup{type=listing}

% https://tex.stackexchange.com/questions/514847/xpatch-fails-if
\def\makesafecoloncatcoderestore##1{%
\edef##1{\catcode:=\the\catcode:\relax}%
}

% https://tex.stackexchange.com/questions/335104/save-and-restore-the
\makesafecoloncatcoderestore\restorecodecoloncatcode
\catcode\:=12

\xpatchcmd{\addcontentsline}
{\contentsline}
{\specialcodelistingcontentsline}{
% \message{^^J^^JCould YES patch the contentsline environment!^^J^^J^^J^^J^^J}
}
{\FAILEDPATCHSPECIALCONTENTSLINE}

% \xshowcmd\addcontentsline
\restorecodecoloncatcode
}{}

% https://tex.stackexchange.com/questions/26220/control-spacing-around
\AtBeginEnvironment{code}{\setcounter{listing}{\value{lstlisting}}
\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\parskip}{0pt}
\captionsetup{skip=0pt}
}
\AtEndEnvironment{code}{\stepcounter{lstlisting}}

% https://tex.stackexchange.com/questions/269491/mixing-minted-with
\AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
\AtEndEnvironment{listing}{\stepcounter{lstlisting}}
\newlength\mylen

\begingroup
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\endgroup

% Why the empty space size is increasing each call to my calculate
% https://tex.stackexchange.com/questions/388411/why-the-empty-space
\newlength\cftlstlistingoldnumwidth
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}

% Calculate the size of the header
% What is the use of percent signs (%) at the end of lines?
% https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent
\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}%
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}%
\setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\mylen}%
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}

% Ensure it is called at least one time
\calculatelisteningsheader

% https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text
\AtBeginDocument{\calculatelisteningsheader}
\makeatother

\begin{document}

{\renewcommand{\cftfigurename}{\listingname\space}
\lstlistoflistings
}
% \newpage

\begin{lstlisting}[caption={Listing}]
# If the body of the namespace is longer than this
# number, it won't be indented. Requires
\end{lstlisting}
% \newpage

\begin{code}
\caption{Code}
\begin{minted}{text}
$python3 unit_tests.py OK \end{minted} \end{code} % \newpage \begin{lstlisting}[caption={Listing}] # If the body of the namespace is longer than this # number, it won't be indented. Requires \end{lstlisting} % \newpage \begin{code} \caption{Code} \begin{minted}{text}$ python3 unit_tests.py
OK
\end{minted}
\end{code}
% \newpage

\end{document}


Now the only problem which remains is the first problem, in which the size of figure name does not auto expands:

You can fix the mixed items on the list of lists by completely stopping using the minted listing environment. Then, instead of configuring the new code environment to use the minted listing environment, configure it to use the lstlisting environment you already had setup and it is working correctly.

\documentclass[
brazilian,brazil,
12pt,
a4paper
]{abntex2}

\usepackage{caption}
\usepackage{listings}
\usepackage[newfloat,chapter]{minted}
\makeatletter

% https://tex.stackexchange.com/questions/269491/mixing-minted-with-lstlisting
% https://tex.stackexchange.com/questions/26220/control-spacing-around-table-caption
\newenvironment{code}{
\captionsetup{skip=0pt,type=lstlisting}
\setlength{\abovecaptionskip}{0pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\parskip}{0pt}
}{}

\begingroup
\let\newcounter\@gobble\let\setcounter\@gobbletwo
\globaldefs\@ne \let\c@loldepth\@ne
\newlistof{listings}{lol}{\lstlistlistingname}
\newlistof{lstlistoflistings}{lol}{\lstlistlistingname}
\newlistentry{lstlisting}{lol}{0}
\endgroup

% https://tex.stackexchange.com/questions/388411/why-the-empty-space-size-is-increasing-each-call
\newlength\cftlstlistingoldnumwidth
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}

% Calculate the size of the header
% https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines
\newcommand{\calculatelstlistingsheader}{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}%

% https://tex.stackexchange.com/questions/52861/what-is-the-plain-tex-equivalent-of-settowidth
\newlength\cftlstlistinginitialnumwidth
\settowidth\cftlstlistinginitialnumwidth{\cftlstlistingpresnum\cftlstlistingaftersnum}%
\setlength\cftlstlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\cftlstlistinginitialnumwidth+2pt}%
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}

% https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
\AtBeginDocument{\calculatelstlistingsheader}
\makeatother
\begin{document}

\lstlistoflistings*

\begin{code}
\caption{File}
Something.
\end{code}

\begin{lstlisting}[caption={First Code}]
code1
\end{lstlisting}

\end{document}