fancyhdr 列印錯誤的章節標題的問題

fancyhdr 列印錯誤的章節標題的問題

fancyhdr我在與套餐結合時遇到問題appendix。根據我迄今為止觀察到的情況,只有當我編譯一頁文件時,才會出現此問題。在twoside模式下,一切似乎都可以正確編譯。

正如您在 MWE 中看到的那樣,主要部分的標題位於每個頁面上。在參考書目頁面上,標題也位於標題中。然而,儘管附件有自己的標題,但它仍保留在文件的後續頁面中。

twoside模式下並非如此。

我的程式碼中是否存在錯誤,或者這是fancyhdrappendix套件中的錯誤嗎?

我非常感謝任何幫助。


微量元素

\documentclass[11pt]{article}
% \documentclass[11pt, twoside]{article}

\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage[titletoc, title, header]{appendix}
\usepackage{biblatex}
\usepackage{lipsum}

\geometry{%
    a4paper,%
    top = 3cm,%
    bottom = 3.5cm,%
    inner = 2.5cm,%
    outer = 2.5cm,%
    nomarginpar,%
    showframe = false%
}

\fancypagestyle{general}{%
    \fancyhf{}                  % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyhead[R]{\itshape\nouppercase{\leftmark}}
    \fancyfoot[R]{\thepage}
}

\let\printbibliographyCopy\printbibliography
\renewcommand{\printbibliography}{%
    \cleardoublepage%
    \addcontentsline{toc}{section}{\refname}%
    \pagenumbering{Roman}%
    \printbibliographyCopy%
    \cleardoublepage%
}

\AtBeginEnvironment{appendices}{%
    \let\oldSection\section%
    \renewcommand{\section}[1]{%
        \cleardoublepage%
        \oldSection{#1}%
    }%
}

\title{\texttt{fancyhdr} issue}
\author{Sam}
\date{}

\bibliography{refs}

\begin{document}
    \thispagestyle{general}%
    \pagestyle{general}%

    \maketitle

    \section{\lipsum[1][1]}
    \lipsum[1-3]

    \subsection{\lipsum[1][2]}
    \lipsum[4-6]

    \subsection{\lipsum[1][3]}
    \lipsum[7-10]

    \section{\lipsum[2][1]}
    \lipsum[1-3]

    \subsection{\lipsum[2][2]}
    \lipsum[4-6]

    \subsection{\lipsum[2][3]}
    \lipsum[7-10]

    \section{\lipsum[3][1]}
    \lipsum[1-3]

    \subsection{\lipsum[3][2]}
    \lipsum[4-6]

    \subsection{\lipsum[3][3]}
    \lipsum[7-13]\cite{Nobody06}

    \printbibliography

    \begin{appendices}
        \section{\lipsum[4][1]}
        \lipsum[1-3]

        \section{\lipsum[5][1]}
        \lipsum[1-3]

        \section{\lipsum[6][1]}
        \lipsum[1-3]
    \end{appendices}
\end{document}

截圖

onesidetwoside模式下的正常部分 正常部分


onesidetwoside模式下的引用 參考


twoside模式中的附錄 雙面模式下的附錄


oneside模式中的附錄 單獨模式下的附錄

答案1

可以在以下簡化的 MWE 中重現相同的效果

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[titletoc, title, header]{appendix}
\usepackage{lipsum}


\fancypagestyle{general}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
  \fancyhead[R]{\itshape\nouppercase{\leftmark}}%
  \fancyfoot[R]{\thepage}%
}
\pagestyle{general}%

\begin{document}
  \section{\lipsum[1][1]}
  \lipsum[1-3]

  \begin{appendices}
    \section{\lipsum[4][1]}
    \lipsum[1-3]
  \end{appendices}
\end{document}

問題具體在於header傳遞給 的選項appendix。該選項在手冊中描述為

在頁眉中的每個附錄之前新增名稱(例如“附錄”)。此名稱由 的值給出\appendixname。請注意,這是具有章節的類別的預設行為。

如果啟用該選項,將在環境\sectionmark開始時重新定義如下appendices

\def\sectionmark##1{%
  \if@twoside
    \markboth{\@formatsecmark@pp{#1}}{}
  \else
    \markright{\@formatsecmark@pp{#1}}{}
  \fi}

twoside請注意和之間的定義有何不同oneside。這個定義與 中通常的定義是一致的article.cls

fancyhdr載入時,類似\sectionmarkinarticle的類別的定義始終只涉及/\markboth設定。總是這樣twosideoneside

\def\sectionmark##1{%
  \markboth{\MakeUppercase{%
    \ifnum \c@secnumdepth>\z@
      \thesection\hskip 1em\relax
    \fi
    ##1}}{}

簡單的解決方案是不使用header的選項appendix,因為它顯然不是根據 的特性編寫的fancyhdr。然後你就不會看到附錄不過,在標題中。如果你想保留它,你可以在加載appendices時修補。fancyhdr

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage[titletoc, title, header]{appendix}
\usepackage{lipsum}


\fancypagestyle{general}{%
  \fancyhf{}%
  \renewcommand{\headrulewidth}{0.4pt}%
  \renewcommand{\footrulewidth}{0.4pt}%
  \fancyhead[R]{\itshape\nouppercase{\leftmark}}%
  \fancyfoot[R]{\thepage}%
}
\pagestyle{general}%

% patch appendices for fancyhdr
\makeatletter
\@ifpackageloaded{fancyhdr}
  {\apptocmd\appendices{%
     \if@chapter@pp
     \else
       \if@dohead@pp
         \def\sectionmark#1{\markboth{\@formatsecmark@pp{#1}}{}}%
       \fi
     \fi}}
  {}
\makeatother

\begin{document}
  \section{\lipsum[1][1]}
  \lipsum[1-3]

  \begin{appendices}
    \section{\lipsum[4][1]}
    \lipsum[1-3]
  \end{appendices}
\end{document}

標題顯示「附錄 A Quisque ullamcorper placerat ipsum」。

相關內容