5

I learned how to create a list of codes on my thesis on the question Setting Entries of List of Listings in LaTeX. Package Listings and I put that command on another file with english tiles as \lstlistingname{Code} and \lstlistlistingname{List of Codes}.

However my thesis is written in multiple languages, therefore later I need to recreate the code table titles \lstlistingname{Códigos} and \lstlistlistingname{Lista de Códigos} when I switch the language of the thesis as explained on the question Is it possible to keep my translation together with original text?

% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
    \renewcommand\cftlstlistingpresnum{\lstlistingname~}
    \settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
    \addtolength\cftlstlistingnumwidth{\mylen} %
    \renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}

On this following example, I created the command \calculatelisteningsheader to reevaluate the headings size of the titles:

\documentclass[12pt,openright,oneside,a4paper]{abntex2}
\usepackage{listings}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Setting Entries of List of Listings in LaTeX. Package Listings
% http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings
\newlength\mylen

\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}

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

% Calculate the size of the header
\newcommand{\calculatelisteningsheader}
{
    \renewcommand\cftlstlistingpresnum{\lstlistingname~}
    \settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
    \addtolength\cftlstlistingnumwidth{\mylen} %
    \renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}

% Ensure it is called at least one time
\calculatelisteningsheader

% Later...
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\calculatelisteningsheader


\begin{document}

\lstlistoflistings

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

\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}

\end{document}

enter image description here

However something is going wrong. The size of the empty space is more than doubled on the second time I call \calculatelisteningsheader:

enter image description here

Why the space size is increasing each call to \calculatelisteningsheader?


Update

As suggested by @koleygrso, I already tried removing the indentation and adding a % on the \calculatelisteningsheader but it stills the same:

\newcommand{\calculatelisteningsheader}
{%
\renewcommand\cftlstlistingpresnum{\lstlistingname~}
\settowidth\mylen{\cftlstlistingpresnum\cftlstlistingaftersnum}
\addtolength\cftlstlistingnumwidth{\mylen} %
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}
}
1

1 Answer 1

7

The problem is that everytime \calculatelisteningsheader is called, \cftlstlistingnumwidth length is being added on (because of \addtolength).

A quick workaround would be to store the old \cftlstlistingnumwidth as a length beforehand, outside of the \calculatelisteningsheader macro, and using this when "adding to length" of \cftlstlistingnumwidth. This way, even if \calculatelisteningsheader is called multiple times, the sum remains consistent.

See the following code -- I've marked out the relevant changes.

\documentclass[12pt,openright,oneside,a4paper]{abntex2}
\usepackage{listings}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Setting Entries of List of Listings in LaTeX. Package Listings
% http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings
\newlength\mylen

\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}

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

\newlength\cftlstlistingoldnumwidth% <---------
\setlength\cftlstlistingoldnumwidth{\cftlstlistingnumwidth}% <---------
% Calculate the size of the header
\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

% Later...
\renewcommand\lstlistingname{Code}
\renewcommand\lstlistlistingname{List of Codes}
\calculatelisteningsheader
\calculatelisteningsheader % <------- For testing purposes
\calculatelisteningsheader % <------- For testing purposes
\calculatelisteningsheader % <------- For testing purposes

\begin{document}

\lstlistoflistings

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

\begin{lstlisting}[caption={Second Code}]
code2
\end{lstlisting}

\end{document}

lstlisting code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .