更改章节编号格式而不更改目录

更改章节编号格式而不更改目录

我正在尝试使用 renewcommand 像这样对节/小节/小子节的编号进行样式化:

\renewcommand{\thesection}{\opensans\arabic{section}}
\renewcommand{\thesubsection}{\hspace{2em}\bfseries\thesection.\alph{subsection}}
\renewcommand{\thesubsubsection}{\hspace{2em}\thesubsection.\roman{subsubsection}}

这对于主要内容非常有用;但是,它也会改变目录的格式。我怎样才能避免这些更改影响目录?

编辑:这是一本 LaTeX 书,如果有所不同的话,它被分为 \frontmatter 和 \mainmatter。

答案1

你应该做的是重新格式化章节、小节等。重新定义编号会改变打印的章节编号到处在文档中(包括内容标题和文中引用!)。

这里有一种方法,使用 titlesec 包。由于我的系统上没有安装 OpenSans,我用cabin另一种 sansserif 字体替换了它。

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{heuristica}
\usepackage{ebgaramond}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage[dotinlabels]{titletoc}
\titleformat{\section}[hang]{\sffamily\bfseries\Large}{Section \thesection.}{0.4em}{}
\titlespacing*{\section}{0pt}{2\baselineskip}{1.25\baselineskip}
\titleformat{\subsection}[hang]{\sffamily\bfseries\large}{\thesubsection}{0.4em}{}
\titlespacing*{\subsection}{2em}{1\baselineskip}{1\baselineskip}
\dottedcontents{section}[3.8em]{}{2.3em}{1pc}
\usepackage{cleveref}

\begin{document}

\tableofcontents

\section{Preliminaries}\label{sec:prelim}
\subsection{A subsection}
\lipsum[1]
\subsection{Another subsection}
\lipsum[2]

\section{Another section}
See \Cref{sec:prelim}.

\end{document}

简而言之,在此代码中, \titleformat 的第一个参数是分段命令的名称,第二个(括号内)是其样式。第三个参数由应用于标签和标题的命令组成,第四个参数用于格式化标签,第五个参数是标签和标题之间的间距,第六个参数是在标题之前应用的命令;使用选项explicit,标题可以称为 #1。还有第七个(optiaanl)参数,这里未使用,其中包含要在标题后应用的代码。

间距命令具有相同的第一个参数;第二个参数是左边距的增加,第三个参数是标题前的垂直空间,第四个参数是标题和后续文本之间的间距。

不同参数的解释可能根据切片命令的样式而有所不同,更多细节请参阅文档§3。

在此处输入图片描述

相关内容