页眉中已加星号的章节编号和名称

页眉中已加星号的章节编号和名称

我已经创建了自定义章节标题:

\documentclass[final]{book}
\usepackage{lipsum}

% Custom macros 
\def \mychapter#1{%
\chapter*{\centering{\LARGE #1}\thispagestyle{empty}}%
\addcontentsline{toc}{chapter}{#1}%
}

\def \mychapterwithsubtitle#1#2{%
\chapter*{\baselineskip=20pt\centering{\LARGE #1}\\*\vspace*{0.3cm}\textnormal{\Large #2}\thispagestyle{empty}}%
\addcontentsline{toc}{chapter}{#1: #2}%
}

% Headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

% Document
\begin{document}
\mychapter{Some chapter}
\lipsum[1-10]
\mychapterwithsubtitle{Chapter Two}{Subtitle of this chapter}
\lipsum[11-30]
\mychapterwithsubtitle{Chapter Three}{Some other subtitle}
\lipsum[31-50]
\end{document}

现在,我希望能够将mychapter标题和名称的文本以及mychapterwithsubtitles标题的副标题放在页眉中(比如说奇数页页眉的左侧)。

我知道有一些命令可以与像、和这样fancyhdr的包一起使用,但是这些命令只适用于常规章节,并且不会对我倾向于使用的带星号版本的章节产生任何结果。\leftmark\rightmark\thechapter

有没有办法将带星号的章节名称和编号放在页眉中?

答案1

正如 Barbara 在她的评论中提到的,您可以使用类似 \markboth或 的机制\markright。我只是在您的定义中添加了标记。

我还定义了一个命令xchapter,该命令带有一个可选参数,该参数将被排版为字幕。该命令通常会修补原始内容\@makeschapterhead,制作起始页empty,最重要的是,清除任何预先存在的材料的标题。

由于这修补了原始的书籍章节定义,因此它与 KOMA 脚本不兼容。

\documentclass[final]{book}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{lipsum}


\def\mychapter#1{%
\chapter*{\centering{\LARGE #1}\thispagestyle{empty}}
\addcontentsline{toc}{chapter}{#1}%
\markright{#1}%
}

\def \mychapterwithsubtitle#1#2{%
\chapter*{\baselineskip=20pt\centering{\LARGE#1}\\*\vspace*{0.3cm}\textnormal{\Large#2}\thispagestyle{empty}}%
\markright{#1: #2}%
\addcontentsline{toc}{chapter}{#1: #2}%
}

\makeatletter
\DeclareDocumentCommand{\xchapter}{ m o }{
    \begingroup
    \ProvideDocumentCommand{\schapterheadendvskip}{}{\vskip 40\p@}
    \patchcmd{\chapter}{plain}{empty}{}{}
    \patchcmd{\@makeschapterhead}{\Huge}{\LARGE}{}{}
    \patchcmd{\@makeschapterhead}{\raggedright}{\centering}{}{}
    \patchcmd{\@makeschapterhead}{\vskip 40\p@}{\schapterheadendvskip}{}{}
    \clearpage\markboth{}{}
    \IfNoValueTF{#2}{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
    \markright{#1}
}{
    \renewcommand{\schapterheadendvskip}{\vskip
        15\p@{\normalfont\Large\centering#2\par\nobreak} \vskip 20\p@}
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1:\space#2}
    \markright{#1: #2}
}
\endgroup
}
\makeatother

\usepackage{showframe}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LO]{\rightmark}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}


\setlipsumdefault{1-24}
\begin{document}
\tableofcontents
\mychapter{A chapter without a subtitle}
\lipsum
\mychapterwithsubtitle{Chapter with many many little
ducklings}{And a subtitle}
\lipsum
\mychapterwithsubtitle{no ducklings}{but some puppies}
\lipsum
\xchapter{There once was a little duckling}[and it was sooo cute]
\lipsum
\xchapter{The first law of thermodynamics}
\lipsum
\chapter*{Look at the header on the next odd page}
\lipsum
\end{document}

相关内容