如何修复我的代码列表上的铸造和列表连字符对齐?

如何修复我的代码列表上的铸造和列表连字符对齐?

我设法将listingsminted代码都放在一个名为的代码列表中LISTINGS。但是,代码列表中的铸造包条目在listing name和之间的距离上是短路的listing title

这会导致两个问题。


第一个问题是,当我有超过 10 个项目时,它们会被 minted 代码环境的连字符吞噬,而不是扩展以容纳连字符。在我的论文中,这个问题发生在第 10 个项目之后,如下图所示:

在此处输入图片描述

然而,当我尝试创建一个最小示例时,它仅在第 1000 个项目之后发生,如下图所示:

在此处输入图片描述

第一个问题最小示例

\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}

第二个问题是,当我同时使用列表和铸造的代码时,它们在代码列表上变得不一致(见图)。

在此处输入图片描述

第二个问题最小示例

\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}

相关问题:

  1. 铸造清单和 TOC 冲突
  2. 自定义来自 minted 的列表

答案1

我设法解决了第二个问题: 在此处输入图片描述

有了这个:

\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
}{}

当示例最初生成时,其内容main.lol如下:

\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}

然后,我的修复将它改成这样:

\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}

函数\specialcodelistingcontentsline只是吃掉/扔掉\contentsline(listing) 的第一个参数并将lstlisting其替换。然后,当 latex 呈现目录时,它会正确地将所有内容打印为lstlisting

完整的第二个例子:

\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}

现在剩下的唯一问题是第一个问题,其中图形名称的大小不会自动扩展:

在此处输入图片描述

答案2

您可以通过完全停止使用列表环境来解决列表列表中的混合项目minted。然后,不要将新code环境配置为使用minted列表环境,而是将其配置为使用lstlisting您已设置且正常运行的环境。

\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}

相关内容