将章节和节标题与其编号/计数器对齐

将章节和节标题与其编号/计数器对齐

如何在不干扰目录的情况下对齐标题?这是我所拥有的:

1 Chapter
1.1 Section
1.1.1. Subsection

这就是我需要的

1      Chapter
1.1    Section
1.1.1. Subsection

我正在使用该titlesec软件包,但到目前为止我还没有找到解决方案。另一个问题基本上阻止我“作弊”,那就是我正在使用该tocloft软件包。因此,我需要找到一种方法来对齐我的标题而不干扰目录。

女士:

\documentclass{scrreprt}

\usepackage{atveryend}
\usepackage{titlesec}


\titleformat{\chapter}{\normalfont\fontsize{12.5}{0}\bfseries}{\thechapter}{1em}{}
\titleformat{\section}{\normalfont\fontsize{10}{0}\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\normalfont\fontsize{10}{0}\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\normalfont\fontsize{10}{0}\bfseries}{\thesubsubsection}{1em}{}
\titleformat{\paragraph}{\normalfont\fontsize{10}{0}\bfseries}{\theparagraph}{1em}{}

\titlespacing*{\chapter}{0pt}{0pt}{14pt}
\titlespacing*{\section}{0pt}{14pt}{7pt}
\titlespacing*{\subsection}{0pt}{14pt}{0pt}
\titlespacing*{\subsubsection}{0pt}{7pt}{0pt}
\titlespacing*{\paragraph}{0pt}{14pt}{0pt}
\titlespacing*{\bhead}{0pt}{14pt}{0pt}

\makeatletter
\newlength{\secnumwidth}
\setlength{\secnumwidth}{0pt}
\providecommand*{\usesecnumwidth}{0pt}

\newcommand*{\secnumwidthbox}[1]{%
  \begingroup
    \sbox0{#1}%
    \ifdim\wd0>\secnumwidth
      \global\secnumwidth=\wd0\relax
    \fi
  \endgroup
  \leavevmode
  \hbox to 
    \ifdim\usesecnumwidth>\secnumwidth
      \usesecnumwidth
    \else
      \secnumwidth
    \fi
  {#1\hfil}%
}

\let\org@chapterformat\chapterformat
\renewcommand*{\chapterformat}{%
  \secnumwidthbox{\org@chapterformat}%
}

\renewcommand*{\othersectionlevelsformat}[3]{%
  \secnumwidthbox{#3\autodot\hfill\enskip}%
}

\AfterLastShipout{%
  \if@filesw
    \immediate\write\@mainaux{%
      \string\gdef\string\usesecnumwidth{\the\secnumwidth}%
    }%
  \fi
  \ifdim\usesecnumwidth=\secnumwidth
  \else
    \@latex@warning@no@line{Rerun LaTeX, because \noexpand\usesecnumwidth
      has changed}
  \fi
}
\makeatother

\begin{document}
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\end{document}

答案1

下面的例子以类scrreprt为例(问题没有指定类)。这些方法可以应用于其他类(或多或少容易)。

\chapterformat重新定义了数字 in\chapter\othersectionlevelsformat数字 in 和 friedns 的宏\section。首先测量包括点和分隔空间的数字,然后将数字放在一个宽度可以容纳最长条目的框中。最宽数字的宽度在.aux文档末尾写入文件中。因此需要运行两次 LaTeX。

示例文件:

\documentclass{scrreprt}

\usepackage{atveryend}

\makeatletter
\newlength{\secnumwidth}
\setlength{\secnumwidth}{0pt}
\providecommand*{\usesecnumwidth}{0pt}

\newcommand*{\secnumwidthbox}[1]{%
  \begingroup
    \sbox0{#1}%
    \ifdim\wd0>\secnumwidth
      \global\secnumwidth=\wd0\relax
    \fi
  \endgroup
  \leavevmode
  \hbox to 
    \ifdim\usesecnumwidth>\secnumwidth
      \usesecnumwidth
    \else
      \secnumwidth
    \fi
  {#1\hfil}%
}

\let\org@chapterformat\chapterformat
\renewcommand*{\chapterformat}{%
  \secnumwidthbox{\org@chapterformat}%
}

\renewcommand*{\othersectionlevelsformat}[3]{%
  \secnumwidthbox{#3\autodot\hfill\enskip}%
}

\AfterLastShipout{%
  \if@filesw
    \immediate\write\@mainaux{%
      \string\gdef\string\usesecnumwidth{\the\secnumwidth}%
    }%
  \fi
  \ifdim\usesecnumwidth=\secnumwidth
  \else
    \@latex@warning@no@line{Rerun LaTeX, because \noexpand\usesecnumwidth
      has changed}
  \fi
}
\makeatother

\begin{document}
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\end{document}

结果

包裹titlesec

软件包重新定义了 KOMA-Script 的内部结构。要添加titlesec的位置如下:titlesec\secnumwidthbox\ttlh@hang

\documentclass{scrreprt}

\usepackage{atveryend}
\usepackage{etoolbox}
\usepackage{titlesec}


\titleformat{\chapter}{\normalfont\fontsize{12.5}{0}\bfseries}{\thechapter}{1em}{}
\titleformat{\section}{\normalfont\fontsize{10}{0}\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}{\normalfont\fontsize{10}{0}\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\normalfont\fontsize{10}{0}\bfseries}{\thesubsubsection}{1em}{}
\titleformat{\paragraph}{\normalfont\fontsize{10}{0}\bfseries}{\theparagraph}{1em}{}

\titlespacing*{\chapter}{0pt}{0pt}{14pt}
\titlespacing*{\section}{0pt}{14pt}{7pt}
\titlespacing*{\subsection}{0pt}{14pt}{0pt}
\titlespacing*{\subsubsection}{0pt}{7pt}{0pt}
\titlespacing*{\paragraph}{0pt}{14pt}{0pt}
\titlespacing*{\bhead}{0pt}{14pt}{0pt}

\makeatletter
\newlength{\secnumwidth}
\setlength{\secnumwidth}{0pt}
\providecommand*{\usesecnumwidth}{0pt}

\newcommand*{\secnumwidthbox}[1]{%
  \begingroup
    \sbox0{#1}%
    \ifdim\wd0>\secnumwidth
      \global\secnumwidth=\wd0\relax
    \fi
  \endgroup
  \leavevmode
  \hbox to 
    \ifdim\usesecnumwidth>\secnumwidth
      \usesecnumwidth
    \else
      \secnumwidth
    \fi
  {#1\hfil}%
}

\let\org@chapterformat\chapterformat
\renewcommand*{\chapterformat}{%
  \secnumwidthbox{\org@chapterformat}%
}

\renewcommand*{\othersectionlevelsformat}[3]{%
  \secnumwidthbox{#3\autodot\hfill\enskip}%
}

% Patch for titlesec
\patchcmd\ttlh@hang{%
  \sbox\z@{#2\strut\ttl@calc\hspace{#3}}%
}{%
  \sbox\z@{\secnumwidthbox{#2\strut\ttl@calc\hspace{#3}}}%
}{}{\errmessage{Cannot patch \string\ttlh@hang}}

\AfterLastShipout{%
  \if@filesw
    \immediate\write\@mainaux{%
      \string\gdef\string\usesecnumwidth{\the\secnumwidth}%
    }%
  \fi
  \ifdim\usesecnumwidth=\secnumwidth
  \else
    \@latex@warning@no@line{Rerun LaTeX, because \noexpand\usesecnumwidth
      has changed}
  \fi
}
\makeatother

\begin{document}
\chapter{Chapter}
\section{Section}
\subsection{Subsection}
\end{document}

结果标题sec

答案2

最简单的方法是将标题计数器包裹在固定宽度的框中。例如,40pt 宽度的框:

\makeatletter
\def\@seccntformat#1{\makebox[40pt][l]{\csname the#1\endcsname}}
\makeatother

示例预览

包装盒

相关内容