如何右对齐目录中的节/小节/...编号?编号和标题之间的距离应该是固定的。
代替
| 1 Section
| 1.1 Subsection
我想要一些类似的东西
| 1 Section
| 1.1 Subsection
(|
=左边距)
一定有一个适合这个的包...
(我也想玩一下缩进)
答案1
\numberline
可以重新定义:
\documentclass{article}
\makeatletter
\renewcommand*{\numberline}[1]{%
\hb@xt@\@tempdima{\hfil#1 }%
}
\makeatother
\begin{document}
\tableofcontents
\section{First}
\subsection{Sub}
\addtocounter{subsection}{9}
\subsection{Foo}
\addtocounter{section}{9}
\section{Next}
\subsection{bar}
\end{document}
具体设置
由于\numberline
适用于所有级别。\numberline
执行 时,有关级别的信息不再可用。因此,配置\numberline
特定于级别的对齐方式要麻烦得多。
以下代码挂接了\contentsline
宏中的级别名称以进行捕获\nl@current@levelname
。然后\numberline
读取级别名称并调用其实现来进行指定的对齐,请参阅示例中的注释。
\documentclass{article}
% \usepackage[bookmarksnumbered, bookmarksopen]{hyperref}
\makeatletter
% Patch, that hooks into \contentsline to store the
% level name in \nl@current@levelname.
% If package 'hyperref' is loaded, then this
% needs to be called *after* package `hyperref`.
\AtBeginDocument{%
\let\nl@org@contentsline\contentsline
\def\contentsline#1{%
\def\nl@current@levelname{#1}%
\nl@org@contentsline{#1}%
}%
}
% \numberline evaluates \nl@current@levelname to find
% the horizontal alignment
\protected\def\numberline#1{%
\begingroup
\edef\nl@align{%
nl@align@%
\@ifundefined{nl@current@levelname}{}{\nl@current@levelname}%
}%
\edef\nl@align{%
\@ifundefined{\nl@align}\nl@align@{\csname\nl@align\endcsname}%
}%
\@ifundefined{nl@numberline@\nl@align}{%
\errmessage{Unknown alignment '\nl@align' for \noexpand\numberline}%
\nl@numberline@l{#1}%
}{%
\csname nl@numberline@\nl@align\endcsname{#1}%
}%
\endgroup
}
% Implementations of `\numberline` for the different horizontal alignments
\newcommand*{\nl@numberline@l}[1]{% left-aligned
\hb@xt@\@tempdima{#1 \hfil}%
}
\newcommand*{\nl@numberline@c}[1]{% centered
\hb@xt@\@tempdima{\hfil#1 \hfil}%
}
\newcommand*{\nl@numberline@r}[1]{% right-aligned
\hb@xt@\@tempdima{\hfil#1 }%
}
% Configuration
% -------------
% Horizonal alignment in \numberline:
% l: left-aligned
% c: centered
% r: right-aligned
% \nl@align@: Default setting
% \nl@align@<levelname>: Setting for specific level
\def\nl@align@{l}% default
\def\nl@align@section{r}
\makeatother
\begin{document}
\tableofcontents
\section{First}
\subsection{Sub}
\addtocounter{subsection}{9}
\subsection{Foo}
\addtocounter{section}{9}
\section{Next}
\subsection{bar}
\end{document}