对齐目录中的项目编号

对齐目录中的项目编号

我正在做一本教科书的笔记,想模仿书中的风格。我已经非常接近了,只剩下一个小问题需要解决。请看下面的例子。我唯一不喜欢的是目录中的文章编号没有对齐。我希望所有的文章编号在句号处对齐。我尝试使用右对齐框来做到这一点,如序言中注释行所示,但我得到了一些我不明白的错误。

\documentclass{article}
    \usepackage{enumitem}

    \newenvironment{article}[1]{\item\addcontentsline{toc}{subsubsection}{\arabic{enumi}. #1}}{}
    % \newenvironment{article}[1]{\item\addcontentsline{toc}{subsubsection}{\makebox[2em][r]{\arabic{enumi}.} #1}}{}
    \setcounter{secnumdepth}{1}

\begin{document}

\tableofcontents

\begin{enumerate}[label = \arabic*. , leftmargin = 0pt]

\section{topic 1}

\subsection{subtopic 1.1}

\begin{article}{article title 1} article text 1 \end{article}
\begin{article}{article title 2} article text 2 \end{article}

\subsection{subtopic 1.2}

\begin{article}{article title 3} article text 3 \end{article}
\begin{article}{article title 4} article text 4 \end{article}
\begin{article}{article title 5} article text 5 \end{article}
\begin{article}{article title 6} article text 6 \end{article}
\begin{article}{article title 7} article text 7 \end{article}
\begin{article}{article title 8} article text 8 \end{article}
\begin{article}{article title 9} article text 9 \end{article}
\begin{article}{article title 10} article text 10 \end{article}

\end{enumerate}

\end{document}

答案1

根据要求将评论转换为答案:

您需要添加一个\protect\makebox

在此处输入图片描述

笔记:

  • 不确定您的实际用例是什么,但对于给定的 MWE,我建议使用\newcommand而不是定义“\newenvironment”。

代码:

\documentclass{article}
    \usepackage{enumitem}

    \newenvironment{article}[1]{\item\addcontentsline{toc}{subsubsection}{\protect\makebox[2em][r]{\arabic{enumi}.} #1}}{}
    %\newenvironment{article}[1]{\item\addcontentsline{toc}{subsubsection}{\makebox[2em][r]{\arabic{enumi}.} #1}}{}
    \setcounter{secnumdepth}{1}

\begin{document}

\tableofcontents

\begin{enumerate}[label = \arabic*. , leftmargin = 0pt]

\section{topic 1}

\subsection{subtopic 1.1}

\begin{article}{article title 1} article text 1 \end{article}
\begin{article}{article title 2} article text 2 \end{article}

\subsection{subtopic 1.2}

\begin{article}{article title 3} article text 3 \end{article}
\begin{article}{article title 4} article text 4 \end{article}
\begin{article}{article title 5} article text 5 \end{article}
\begin{article}{article title 6} article text 6 \end{article}
\begin{article}{article title 7} article text 7 \end{article}
\begin{article}{article title 8} article text 8 \end{article}
\begin{article}{article title 9} article text 9 \end{article}
\begin{article}{article title 10} article text 10 \end{article}

\end{enumerate}

\end{document}

相关内容