fancyhdr:标题与标题编号不匹配

fancyhdr:标题与标题编号不匹配

标题中的一些标题与数字不匹配。示例中的第 3 页就出现了这种情况。有什么解决办法或解决方法吗?

\documentclass[twoside]{report}
\usepackage[ansinew]{inputenc}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\renewcommand{\subsectionmark}[1]{\markright{ #1}}
\renewcommand{\sectionmark}[1]{\markright{ #1}}
\renewcommand{\chaptermark}[1]{\markright{ #1}}

\rfoot{}
\cfoot{}
\fancyhead[LO,RE]{\thesubsection \rightmark}
\fancyhead[RO,LE]{\thepage}
\renewcommand {\headrulewidth}{0pt}
\renewcommand {\footrulewidth}{0pt}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}


\chapter{Some chapter}

\section{Section I}
\lipsum[1]

\subsection{Subsection}
\lipsum[1-2]

\section{Section II}

\lipsum[1-2]

\subsection{Subsection One}
\lipsum[1-2]

\subsection{Subsection Two}
\lipsum[1-2]



\section{Section III}
\lipsum[1-2]

\subsection{Subsection for this section}
\lipsum[1-2]

\subsection{Another subsection}
\lipsum[1-2]

\end{document}

答案1

章节编号必须与标题一起添加\markright。在页眉中添加已经太晚了。

编号由计数器控制secnumdepth。因此,示例还检查了的值,secnumdepth以避免在标题中给出数字,但在文本中没有给出数字的情况。

\documentclass[twoside]{report}
\usepackage[ansinew]{inputenc}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy}
\renewcommand{\subsectionmark}[1]{%
  \markright{%
    \ifnum\value{secnumdepth}>1 %
      \thesubsection~%
    \fi
    #1%
  }%
}
\renewcommand{\sectionmark}[1]{%
  \markright{%
    \ifnum\value{secnumdepth}>0 %
      \thesection~%
    \fi
    #1%
  }%
}
\renewcommand{\chaptermark}[1]{%
  \markright{%
    \ifnum\value{secnumdepth}>-1 %
      \thechapter~%
    \fi
    #1%
  }%
}

\rfoot{}
\cfoot{}
\fancyhead[LO,RE]{\rightmark}
\fancyhead[RO,LE]{\thepage}
\renewcommand {\headrulewidth}{0pt}
\renewcommand {\footrulewidth}{0pt}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\begin{document}

\chapter{Some chapter}

\section{Section I}
\lipsum[1]

\subsection{Subsection}
\lipsum[1-2]

\section{Section II}

\lipsum[1-2]

\subsection{Subsection One}
\lipsum[1-2]

\subsection{Subsection Two}
\lipsum[1-2]

\section{Section III}
\lipsum[1-2]

\subsection{Subsection for this section}
\lipsum[1-2]

\subsection{Another subsection}
\lipsum[1-2]

\end{document}

相关内容