在书籍类中设置非章节页面的页面样式

在书籍类中设置非章节页面的页面样式

当我尝试格式化一本已开始撰写的小说时,我遇到了 fancyhdr 的一个问题,而我无法通过软件包文档来修复该问题。

我想要的是章节页和非章节页上的页眉都相同。每个页眉的右侧都应该有页码,左侧应该有章节标题。

现在,我已经知道 \pagesyle{fancy} 不适用于章节页面;因此,我通过 etoolbox 重新定义了它,并且该部分工作正常。然而,在非章节页面上,fancyhdr 不想显示标题规则。这就是我想要修复的:我希望章节页面和非章节页面上的标题看起来相似。

以下是我到目前为止所做的大部分 MWE:

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{geometry}
    \geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
    \setlength\parindent{0pt}
    \setlength\parskip{18pt}
    \setlength{\headheight}{30pt} 

\usepackage{etoolbox}
    \makeatletter
        \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages
        \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
    \makeatother

\usepackage{fancyhdr}
    \fancyhead[R]{\thepage}
    \fancyfoot{}

\usepackage{titlesec}
    \titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
    \titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}

\tableofcontents
\thispagestyle{empty}

\chapter{Lorem Ipsum A}
\setcounter{page}{1}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 

\pagebreak

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 

\pagebreak

\chapter{Lorem Ipsum B}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

\end{document}



答案1

为了(我的)方便,我添加了包kantlipsum 来测试更多页面。更重要的是,还添加了一个新功能pagestyle,将章节名称放在所有页眉中。页眉现在出现在章节页面和非章节页面上:右侧是页码,左侧是章节标题,遵守规则。

% !TeX TS-program = pdflatex

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{kantlipsum} % added dummy text

\usepackage{geometry}
\geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
\setlength\parindent{0pt}
\setlength\parskip{18pt}
\setlength{\headheight}{30pt} 

\usepackage{etoolbox}
\makeatletter
    \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages
    \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
\makeatother

\usepackage{fancyhdr}   
 %\fancyhead[R]{\thepage}
 %\fancyfoot{}

\fancypagestyle{mystyle}{% added
    \fancyhf{}
    \fancyhead[L]{\leftmark}
    \fancyhead[R]{\thepage}
    \fancyfoot{}
}

\usepackage{titlesec}
\titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
\titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}
\tableofcontents
\thispagestyle{empty}

\setcounter{page}{1}
\pagestyle{mystyle} % apply from here to the end
\chapter{Lorem Ipsum A}
\kant[1-10]

\chapter{Lorem Ipsum B}
\kant[14-23]

\end{document}

德

或者得到相同的结果,也许更简单的代码

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{kantlipsum}

\usepackage{geometry}
\geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
\setlength\parindent{0pt}
\setlength\parskip{18pt}
\setlength{\headheight}{30pt} 

\usepackage{etoolbox}
\makeatletter
    %   \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages NOT LONGER NEEDED
    \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
\makeatother

\usepackage{fancyhdr}   
\fancypagestyle{plain}{%
\fancyhf{}
\fancyhead[L]{\leftmark}
\fancyhead[R]{\thepage}
\fancyfoot{}
}

\usepackage{titlesec}
\titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
\titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}
\tableofcontents
\thispagestyle{empty}   
\setcounter{page}{1}
\pagestyle{plain}
\chapter{Lorem Ipsum A}
\kant[1-10] 
\chapter{Lorem Ipsum B}
\kant[14-23]    
\end{document}

虽然与问题无关,但我还是建议测试memoir专门用于编写书籍的软件包。它不仅集成了许多软件包,简化了升级,还在单一环境中提供了许多章节样式、目录、页眉、标题页等。

相关内容