我想将同一页中(章节、部分、小节)标题中的文本对齐到数字(6、6.1、6.1.1)后。但我只能找到数字的对齐方式,例如左对齐。
我现在遇到了新问题。它适用于章节、节、小节以及小节的标题。小节的标题没有编号。如何使小节的文本与章节或节或小节的编号对齐?
\documentclass
[openright,listof=totoc,bibliography=totoc,twoside,fontsize=12pt,
english,parskip=half,headinclude,footinclude=false,headsepline,
DIV17,BCOR16mm,numbers=noenddot,2.1headlines,appendixprefix,
cleardoublepage=empty
]{scrbook}
答案1
这里有一个建议,即仅针对整个文档(而不是每页)对齐文本。并且您必须手动设置节号所需的空间。
\documentclass{scrbook}[2015/10/03]
\newdimen\sectionnumberwdmax
\setlength\sectionnumberwdmax{2cm}% <- space needed for the section numbers
%\renewcommand\raggedsection{\raggedleft}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\@hangfrom%
{\makebox[\sectionnumberwdmax][l]{\hskip#2#3}}%
{\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #4}}%
}
\renewcommand\chapterlinesformat[3]{%
\@hangfrom%
{\makebox[\sectionnumberwdmax][l]{#2}}%
{\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #3}}%
}
\makeatother
\begin{document}
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\end{document}
如果未编号的标题应与其他标题的编号对齐:
\documentclass{scrbook}[2015/10/03]
\newdimen\sectionnumberwdmax
\setlength\sectionnumberwdmax{2cm}% <- space needed for the section numbers
%\renewcommand\raggedsection{\raggedleft}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
\ifstr{#3}{}
{\@hangfrom{\hskip#2#3}{#4}}
{\@hangfrom%
{\makebox[\sectionnumberwdmax][l]{\hskip#2#3}}%
{\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #4}}}%
}
\renewcommand\chapterlinesformat[3]{%
\ifstr{#2}{}
{\@hangfrom{#2}{#3}}
{\@hangfrom%
{\makebox[\sectionnumberwdmax][l]{#2}}%
{\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #3}}}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\end{document}
答案2
这适用于每页,但添加新标题后至少需要运行两次 LaTeX:
\documentclass{scrbook}
\makeatletter
\AtBeginDocument{%
\if@filesw
\write\@auxout{%
\noexpand\providecommand*\noexpand\minnumwidth[3]{}%
}%
\fi
}
\newcommand*{\minnumwidth}[3]{%
\scr@ifundefinedorrelax{maxnumwidth@page@#2}{%
\expandafter\gdef\csname maxnumwidth@page@#2\endcsname{#3}%
}{%
\expandafter\ifdim \csname maxnumwidth@page@#2\endcsname<#3\relax
\expandafter\gdef\csname maxnumwidth@page@#2\endcsname{#3}%
\fi
}%
\expandafter\gdef\csname pageof@#1\endcsname{#2}%
}
\newcommand*{\makenumberboxof}[1]{%
\scr@ifundefinedorrelax{pageof@#1}{%
#1\autodot\enskip
}{%
\scr@ifundefinedorrelax{maxnumwidth@page@\@nameuse{pageof@#1}}{%
#1\autodot\enskip
}{%
\makebox[\@nameuse{maxnumwidth@page@\@nameuse{pageof@#1}}][l]%
{#1\autodot\enskip}%
}%
}%
}
\newcommand*{\changenumformat}[1]{%
\expandafter\renewcommand\expandafter*\csname #1format\endcsname{%
\begingroup
\settowidth{\@tempdima}{\csname the#1\endcsname\autodot\enskip}%
\edef\reserved@a{\string\minnumwidth{\csname
the#1\endcsname}{\noexpand\thepage}{\the\@tempdima}}%
\if@filesw
\expandafter\write\expandafter\@auxout\expandafter{\reserved@a}%
\fi
\endgroup
\makenumberboxof{\csname the#1\endcsname}%
}%
}
\makeatother
\changenumformat{chapter}
\changenumformat{section}
\changenumformat{subsection}
\changenumformat{subsubsection}
\makeatother
\usepackage{blindtext}
\begin{document}
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\appendix
\chapter{An appendix}
\section{An appendix section}
\subsection{An appendix subsection}
\end{document}
第一次运行 LaTeX 后,最后一页的标题是:
但之后第三运行它们是:
请注意,只有当章节、节和小节的数量可扩展时,它才有效。
未编号的标题仍保持左对齐。因此,如果我理解您对问题的补充是正确的,则无需对这些标题进行任何操作。但如果我没有理解,并且如果此每页解决方案也应用于未编号的标题,则您必须添加其他代码,例如\subsubsection
:
\usepackage{xpatch}
\newcounter{notnumberedsection}
\renewcommand*{\thenotnumberedsection}{\arabic{notnumberedsection}*}
\makeatletter
\xpretocmd{\subsubsection}{%
\let\scr@subsubsection@sectionindent\z@
\ifnumbered{subsubsection}{%
\typeout{subsubsection numbered!}%
}{%
\stepcounter{notnumberedsection}%
\if@filesw
\edef\reserved@a{\string\minnumwidth{\thenotnumberedsection}{\noexpand\thepage}{\string\z@}}%
\show\reserved@a
\expandafter\write\expandafter\@auxout\expandafter{\reserved@a}%
\fi
\scr@ifundefinedorrelax{pageof@\thenotnumberedsection}{%
\typeout{Don't know page of \thenotnumberedsection!}%
}{%
\scr@ifundefinedorrelax{maxnumwidth@page@\@nameuse{pageof@\thenotnumberedsection}}{%
\typeout{Don't know max width for page \@nameuse{pageof@\thenotnumberedsection}!}%
}{%
\expandafter\let\expandafter\scr@subsubsection@sectionindent
\csname
maxnumwidth@page@\@nameuse{pageof@\thenotnumberedsection}\endcsname
\show\scr@subsubsection@sectionindent
}%
}%
}%
}{}{}
这将导致最后一页:
但大多数情况下看起来很奇怪,例如第 58 页的例子:
如果需要推荐不使用这个!如果能更好地\subsubsection
使用编号
\setcounter{secnumdepth}{\subsubsectionnumdepth}