如何設定用前導 0 填滿的部分?

如何設定用前導 0 填滿的部分?

我正在為某人格式化文檔,他希望每個部分編號都是三位數,以 000 開頭,並在需要時填充零。

我發現了一個類似的問題,並嘗試調整該範例,但它給了我一個

! TeX capacity exceeded, sorry [input stack size=5000].
\thesection ->\thesection 
                          .\three@digits {\value {section}}
l.11 \section{Synopsis}

If you really absolutely need more capacity,
you can ask a wizard to enlarge me.

我的 MNWE 是:

 \documentclass[letterpaper]{article}

 %Padding the leading zeros \makeatletter
 \renewcommand\thesection{\thesection.\three@digits{\value{section}}}
 \makeatother

 \begin{document} \setcounter{section}{-1} \tableofcontents
 \section{Synopsis} foo

 \section{Language} bar

 \section{Legal} baz

 \end{document}

答案1

\three@digits中不存在核心預設情況下。不過,你可以定義它,也需要調整節的ToC相關的數位間距:

在此輸入影像描述

\documentclass[letterpaper]{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

%Padding the leading zeros
\makeatletter
\patchcmd{\l@section}{1.5em}{2em}{}{}% Adjust section ToC number width
%\def\two@digits#1{\ifnum#1<10 0\fi\number#1}% http://mirrors.ctan.org/macros/latex/unpacked/latex.ltx
\def\three@digits#1{\ifnum#1>99\else\ifnum#1>9 0\else00\fi\fi\number#1}
\renewcommand\thesection{\three@digits{\value{section}}}
\makeatother

\begin{document} 
\setcounter{section}{-1}
\tableofcontents
\section{Synopsis} foo

\section{Language} bar

\section{Legal} baz

\end{document}

使用 ToC 相關的間距進行調整etoolbox修補。您可能還想調整\subsection間距,其定義為

\newcommand*\l@subsection{\@dottedtocline{2}{1.5em}{2.3em}}

預設情況下(從article.cls)。這1.5em是縮進,也是2.3em數字的空格。所以你可能會選擇

\renewcommand*\l@subsection{\@dottedtocline{2}{2em}{3em}}

說。


有關用零填充計數器的更多信息,請參閱如何輸出帶有前導零的計數器?

相關內容