我想知道为什么以下代码在第 1 章(简介)开始后打印标题“CONTENTS”,而不是在标题中写入正确的章节名称“Intro”(就像第 2 章“Test”一样)。我需要修补“titlesec”吗?
\documentclass{book}
\usepackage{titlesec}
\newpagestyle{main}{%
\headrule
\sethead{\chaptername\ \thechapter}{}{\chaptertitle}
\setfoot{}{}{\thepage}
}
\begin{document}
\tableofcontents
\chapter*{intro}
b \newpage b
\chapter{test}
a \newpage b
\end{document}
答案1
默认情况下,LaTeX 不会使用带星号的章节命令来更新页眉和页脚。您必须自己使用\chaptermark
(避免使用\markboth
和\markright
)。此外,您可能希望删除章节编号,这可以通过 来完成\ifthechapter
。不要忘记包选项pagestyles
。
\documentclass{book}
\usepackage[pagestyles]{titlesec}
\newpagestyle{main}{%
\headrule
\sethead{\ifthechapter{\chaptername\ \thechapter}{}}{}{\chaptertitle}
\setfoot{}{}{\thepage}
}
\begin{document}
\pagestyle{main}
\tableofcontents
\chapter*{intro}
\chaptermark{intro}
b \newpage b
\chapter{test}
a \newpage b
\end{document}
答案2
问题是,这\chapter*
不会重置或更改运行头。KOMA-Script 类为此目的提供了命令\addchap
响应:\addsec
\documentclass[headings=standardclasses,headsepline,cleardoublepage=headings]{scrbook}
% Following lines are only to make a similar heading like your `\newpagestyle` definition but using KOMA-Script
\usepackage{scrlayer-scrpage}
\automark{chapter}
\clearscrheadfoot
\renewcommand*{\chaptermark}[1]{%
\markdouble{\MakeMarkcase{\Ifnumbered{chapter}{\chaptermarkformat}{}\hfill#1}}%
}
\ohead{\headmark}
\ofoot*{\pagemark}
\begin{document}
\tableofcontents
\addchap{intro}% Chapter with ToC entry and running head
b \newpage b
\chapter{test}
a \newpage b
\end{document}
对于标准课程,您还需要\markboth
:
\documentclass{book}
\usepackage{titlesec}
\newpagestyle{main}{%
\headrule
\sethead{\chaptername\ \thechapter}{}{\chaptertitle}
\setfoot{}{}{\thepage}
}
\begin{document}
\tableofcontents
\chapter*{intro}% does neither add a ToC entry nor change the running head
\markboth{into}{into}
b \newpage b
\chapter{test}
a \newpage b
\end{document}
titleps
但是使用( ) 页面样式时,效果并不如预期titlesec
。下面是一个不使用 的示例,titlesec
但使用了scrlayer-scrpage
:
\documentclass{book}
% Following lines are only to make a similar heading like your `\newpagestyle` definition but using KOMA-Script
\usepackage[headsepline]{scrlayer-scrpage}
\automark{chapter}
\clearscrheadfoot
\renewcommand*{\chaptermark}[1]{%
\markdouble{\MakeMarkcase{\chaptermarkformat\hfill#1}}%
}
\ohead{\headmark}
\ofoot*{\pagemark}
\begin{document}
\tableofcontents
\chapter*{intro}% does neither add a ToC entry nor change the running head
\markboth{into}{into}
b \newpage b
\chapter{test}
a \newpage b
\end{document}