僅當節號 > 0 時才顯示標題

僅當節號 > 0 時才顯示標題

目前章節名稱+章節編號顯示在標題中。這是正常行為(我想要的)。

例子:

在此輸入影像描述

如果頁面上沒有部分,如何刪除標題文字。下面的例子說明了我的問題。標題是“1.0”,但我不想顯示任何內容。

例子:

在此輸入影像描述

這是我的 MWE:

\documentclass{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}

\fancyhead{}
\fancyhead[LE]{\makebox[2cm][r]{}{}}
\fancyhead[RO]{\rightmark\hspace*{0.2cm}\thesection}


\begin{document}

\chapter{An example chapter}

\blindtext[25]

\section{An example section}

\blindtext
\end{document}

答案1

不建議fancyhdr與一起使用。scrbook但是,這裡有一個解決方案:\thesection\markright.請注意,\markright僅需要一個參數(而\markboth需要兩個)。

\documentclass{scrbook}
\usepackage[english]{babel}
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
  \markright{#1\ \thesection}%
}

\fancyhf{}
\fancyhead[RO]{\rightmark}
\fancyfoot[C]{\thepage}

\begin{document}

\chapter{An example chapter}

\lipsum[1-20]

\section{An example section}

\lipsum[1-20]
\end{document}

相關內容