我设法用 更改了我的lstlinsting
数字宽度\setlength\cftlstlistingnumwidth
。但是当我尝试对 minted 执行相同操作时listing
,我得到了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
我设法通过完全停止使用列表环境来修复此问题minted
。然后,只需要设置lstlisting
环境numwidth
值。
\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}
参考:
- https://github.com/gpoore/minted/issues/247如何配置
listing
环境间距?