如何获得动态的 \cft 数字宽度,即根据数字大小?

如何获得动态的 \cft 数字宽度,即根据数字大小?

例如,当此列表示例在表格中达到 10000 个项目(列表)时,数字隐藏在连字符后面,即:

在此处输入图片描述

您可以通过以下示例中创建 10000 个列表来执行此操作,或者编辑main.lol文件并将 10000 和 20000 作为列表编号。

\PassOptionsToPackage{french}{babel}
\documentclass[english,12pt,a4paper,twoside]{abntex2}
\usepackage{caption,xpatch,listings}
\makeatletter
\tracingpatches

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

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

\begin{document}

{
\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{lstlisting}[caption={Listing}]
# If the body of the namespace is longer than this
# number, it won't be indented. Requires
\end{lstlisting}
% \newpage

\end{document}

参考:

  1. 增加目录中章节编号和标题之间的间距

答案1

这似乎有效,尽管我不太确定该aftersnum部分应该做什么

需要经过几次传球才能解决。

\PassOptionsToPackage{french}{babel}
\documentclass[english,12pt,a4paper,twoside]{abntex2}
\usepackage[T1]{fontenc} 
\usepackage{caption,listings}
\makeatletter

% get rid of the one listings may have provided
\let\lstlistoflistings\relax

% whatever this does, it seems to work
\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

\newlength\widestlistings

% use the hook in numberline to gather the width of the widest number
\renewcommand*\numberlinehook[1]{%
  \expandafter\nametest\expandafter{\cftwhatismyname}{lstlisting}%
  \ifsamename%
  \settowidth\@tempdimc{\@nameuse{cft\cftwhatismyname font}#1}%
  \ifdim\@tempdimc>\widestlistings\relax
  \global\widestlistings=\@tempdimc
  \fi
  \fi
}

% widest number in the aux to be used on the next pass 
\renewcommand\cftlolafterlisthook{
  \typeout{w: \the\widestlistings}
  \immediate\write\@mainaux{\string\gdef\string\savedlolwidth{\the\widestlistings}}
}

% prefix listing entries
\renewcommand\cftlstlistingname{\lstlistingname~}%
% autoadjust the width of the numwidth box, delayed so we can access
% the saved number
\AtBeginDocument{
  \setlength\cftlstlistingnumwidth{%
    \dimexpr\@ifundefined{savedlolwidth}{1em}{\@nameuse{savedlolwidth}}
    +2em
  }%
}
\renewcommand\cftlstlistingaftersnum{\hfill\textendash\hfill}%


\makeatother

\begin{document}


\lstlistoflistings*



% \newpage

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

\setcounter{lstlisting}{1000}


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

\end{document}

相关内容