0

I managed to change my lstlinsting numwidth with \setlength\cftlstlistingnumwidth. But when I try to do the same with minted listing, I got Undefined control sequence: \cftlistingnumwidth

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

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

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

% https://tex.stackexchange.com/questions/269491/mixing-minted-with-lstlisting
% 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}}

\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+20pt}%

    \setlength\cftlistingnumwidth{\dimexpr\cftlstlistingoldnumwidth+\cftlstlistinginitialnumwidth+20pt}%
    \renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%
}

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

{\renewcommand{\cftfigurename}{\listingname~}
\lstlistoflistings*
}

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

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

\end{document}
1
  • I am getting the feeling that the minted package does not work well with the memoir class. Commented Nov 6, 2019 at 19:27

1 Answer 1

0

I managed to fix it by stopping using completely the minted listing environment. Then, it is only required to set the lstlisting environment numwidth value.

\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+20pt}%
    \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}

References:

  1. https://github.com/gpoore/minted/issues/247 How to configure the listing environment spacings?

You must log in to answer this question.

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