如果您使用 documentclass 书籍或 scrbook,新章节看起来会相当无趣:它只是标题加上数字,例如:
因此,人们可能希望在前后添加水平线——我认为这会让它看起来更令人愉悦:
如何做到这一点在 Stackoverflow 上,修改后的代码在此处重现:
\documentclass{book}
\usepackage{titlesec}
\usepackage{graphicx}
% Surrounds all chapter titles by lines (see screenshot),
% which includes the titles in the TOC and list of figures and tables
\titleformat{\chapter}[display]
{\bfseries\huge}
{\filleft\Large\chaptertitlename~\thechapter}
{3ex}
{\titlerule\vspace{1.5ex}\filright}
[\vspace{1ex}\titlerule]
% The following code removes the lines (that are added above)
% from the TOC and list of figures and tables
\titleformat{name=\chapter, numberless}[block]
{\bfseries\huge}
{}
{0pt}
{\filright}
\begin{document}
% Puts TOC and list of figures on tables on a single pages
% makes sense for minimal working examples so that one
% doesn't have to scroll for ages just to check appearance
\tableofcontents
\begingroup
\let\clearpage\relax
\listoffigures
\let\clearpage\relax
\listoftables
\endgroup
\newcommand{\ShortLorem}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu-
lum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris.
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. }
\chapter{First Chapter Headline}
\ShortLorem
\begin{figure}[h]
\centering
\includegraphics[width=.35\textwidth]{example-image-a}
\caption{Dummy caption\label{fig:dummyFig}}
\end{figure}
\ShortLorem
\begin{table}[h]
\begin{center}
\begin{tabular}{cc}
A & B\\
\hline
a & b
\end{tabular}
\caption{Dummy caption\label{tab:dummyTab}}
\end{center}
\end{table}
\ShortLorem
\chapter{Second Chapter Headline}
\ShortLorem
\section{First Section in 2nd Chapter}
\ShortLorem
\section{Second Section in 2nd Chapter}
\ShortLorem
\end{document}
只是为了完整性:我的问题(见下文)是我链接的问题的一个变体。原始问题询问如何防止目录和图表列表中出现这些额外的行。这就是开头的第二个代码块(The following code removes...
)的作用。其影响如下图所示:
(如果你觉得以上内容有用,请点赞原始问题!我只是稍微改进了代码(添加了注释,删除了无意义的包),并添加了屏幕截图。)
现在,我的问题是:
当我将文档类从 book 更改为 scrbook 时,如何使此代码正常工作?
当我这样做时,我收到以下错误消息:
! Missing number, treated as zero.
<to be read again>
}
l.74 \section{First Section in 2nd Chapter}
我不知道为什么...这可以“修复”吗?我使用 scrbook 而不是 book 的唯一原因是前者提供了很多空间更大,即边框更小。到目前为止,我还没有发现任何其他变化(例如那些明显导致上述代码不再起作用的变化)。如能提供帮助,我将不胜感激。:)
答案1
插入以下代码即可使其工作:
\titleformat{\section}
{\normalfont\Large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\large\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}
{\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}
(取自堆栈溢出。但我很高兴得到进一步的解释,例如,为什么添加包titlesec
会导致问题。)