最小工作示例:
\documentclass[12pt, a4paper, oneside, parskip=half]{scrbook}
\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage{blindtext}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\lhead[\leftmark]{\MakeUppercase{\leftmark}}
\rhead[\pagemark]{\pagemark}
\fancypagestyle{plain}{%
%\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}} %error
%\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{##1}}{}} %nothing happend
\fancyhead{}%
\fancyfoot{}%
\rhead[\pagemark]{\pagemark}%
}
\begin{document}
\chapter{This is a minimal working example}
\blindtext[8]
\end{document}
我想显示不带前导数字的章节标题。两个注释都\renewcommand
不起作用。
答案1
以下是使用scrlayer-scrpage
和定义新的另一个建议mark
:
\documentclass[12pt, oneside, parskip=half]{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[manualmark,markcase=upper,headsepline,plainheadsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\setkomafont{pagehead}{\upshape}
\ohead*{\pagemark}
\newmarks\chaptermarkwithoutnum
\makeatletter
\renewcommand\chaptermark[1]{%
% code by @Schweinebacke (see https://tex.stackexchange.com/a/452733/9057):
\begingroup
\let\label\relax \let\index\relax \let\glossary\relax
\@temptokena{#1}%
\unrestored@protected@xdef\@themark{\the\@temptokena}%
\@temptokena\expandafter\expandafter{\expandafter\MakeMarkcase\expandafter{\@themark}}%
\marks\chaptermarkwithoutnum{\the\@temptokena}%
\endgroup
%
\markboth{\MakeMarkcase{\ifnumbered{chapter}{\chaptermarkformat}{}#1}}{}%
}
\makeatother
\ihead[\firstmarks\chaptermarkwithoutnum]{\leftmark}
\begin{document}
\chapter{This is a minimal working example}
\Blindtext[10]
\end{document}
更新
不加附加标记的建议:
\documentclass[12pt, oneside, parskip=half]{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[markcase=upper,headsepline,plainheadsepline]{scrlayer-scrpage}
\automark[chapter]{chapter}
\clearpairofpagestyles
\setkomafont{pagehead}{\upshape}
\ohead*{\pagemark}
\renewcommand\chaptermark[1]{%
\markboth{\MakeMarkcase{\ifnumbered{chapter}{\chaptermarkformat}{}#1}}{\MakeMarkcase{#1}}%
}
\ihead[\righttopmark]{\leftmark}
\begin{document}
\chapter{This is a minimal working example}
\Blindtext[10]
\end{document}
结果和上面一样。
答案2
fancyhdr
通常不应与 KOMA 脚本类一起使用,因为您会收到以下警告.log
:
Class scrbook Warning: Usage of package `fancyhdr' together
(scrbook) with a KOMA-Script class is not recommended.
(scrbook) I'd suggest to use
(scrbook) package `scrlayer-scrpage'.
(scrbook) Nevertheless, using requested
(scrbook) package `fancyhdr' on input line 3.
但是,您可以按照以下方法获得所需的输出。即plain
页面样式(与章节的第一页相关联)显示章节标题没有一个数字,而其他页面则用数字显示:
\documentclass{scrbook}
\usepackage{fancyhdr}
\usepackage{blindtext}
\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\lhead[\leftmark]{\MakeUppercase{\leftmark}}
\rhead[\pagemark]{\pagemark}
\makeatletter
\fancypagestyle{plain}{%
\fancyhf{}% Clear header/footer
\rhead[\pagemark]{\pagemark}%
\lhead{\MakeUppercase{\scr@ds@head}}% Chapter title
}
\makeatother
\begin{document}
\chapter{This is a minimal working example}
\blindtext[8]
\end{document}
答案3
在页面样式中重新定义\chaptermark
是完全没用的,因为命令是由 发出的\chapter
。
我不知道目的是什么:我发现将章节标题放在起始章节页的顶部是错误的,因为相同的标题在下面几行。
但是,有一个技巧就是将章节号隐藏在一个宏中,该宏可以在本地重新定义,以便在排版纯文本标题时不返回任何内容。
该类scrbook
会警告您有用于设置页眉和页脚的 Koma 脚本方法,但fancyhdr
仍然允许您使用。不过,我将语法改成了更好的语法。
\documentclass[12pt, a4paper, oneside, parskip=half]{scrbook}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{blindtext}
\newrobustcmd{\perhaps}[1]{#1}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\perhaps{\thechapter.\ }#1}{}}
\fancyhead{}
\fancyfoot{}
\fancyhead[L]{\MakeUppercase{\leftmark}}
\fancyhead[R]{\thepage}
\makeatletter
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyhead[L]{\let\perhaps\@gobble\MakeUppercase{\leftmark}}%
\fancyhead[R]{\thepage}%
}
\makeatother
\begin{document}
\chapter{This is a minimal working example}
\blindtext[8]
\end{document}