不存在標題時自動刪除標尺

不存在標題時自動刪除標尺

這是一個由以下問題引發的問題自動刪除一頁文件中的頁碼

當啟動奇特博士包時,有必要清除標題,我在下面使用 執行此操作\fancyhf{},因為否則它將列印MY FIRST SECTION為下面文件的標題。但是,它仍然會在空標題下方列印標尺,因此我使用 刪除它\renewcommand{\headrulewidth}{0pt}

問題是這樣的:我把這些行放在我的自訂中.sty文件中。當我如果我的文件中想要一個標題,我顯然不想將該標題硬連接到我的.sty文件中。它應該放在我正在編寫的文件的序言中,如下所示。但自從我放入\renewcommand{\headrulewidth}{0pt}文件後.sty,即使我添加了標題,它現在也不會列印任何標尺。

\renewcommand{\headrulewidth}{0pt}我正在尋找一種讓 LaTeX僅在標題實際上為空時自動啟動的方法。

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}
\fancyhead[C]{Woah! There's no ruler here!}

\begin{document}
\section{My first section}
\end{document}

在此輸入影像描述

答案1

以下範例將標題元素(左、中、右)放入框中並測量其寬度。如果寬度為零,則抑制標題規則:

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\RequirePackage{fancyhdr}
\let\org@fancyhead\@fancyhead
\renewcommand*{\@fancyhead}[5]{%
  \sbox0{#2#3#4}%
  \ifdim\wd0=\z@
    \let\headrule\relax
  \fi
  \org@fancyhead{#1}{#2}{#3}{#4}{#5}%
}
\pagestyle{fancy}
\fancyhf{}
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}

\begin{document}
\section{My first section}

\newpage
\fancyhead[C]{Header is set}
\section{My next section}

\end{document}

評論:

  • 的重新定義\headrule對於目前標頭是本地的,因為 LaTeX 在框/群組內呼叫標頭代碼。

答案2

也許是這樣的:

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhf{}
    \makeatletter
    \AtBeginDocument{% don't make the comparison until the start of the document as the header may get specified later in the preamble
      \xdef\tempa{\f@ncyoch\f@ncyech\f@ncyorh\f@ncyerh\f@ncyolh\f@ncyelh}% get the value of the header parts
      \xdef\tempb{\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x}% value for comparison: contents of header parts if no content is specified
      \ifx\tempa\tempb% test to see if they are the same
        \renewcommand{\headrulewidth}{0pt}% if they are, the centre header is empty to set the rule width to 0pt
      \fi% otherwise, do nothing
    }
    \makeatother
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}
\fancyhead[C]{Woah! There's no ruler here!}
% \fancyhead[C]{}

\begin{document}
\section{My first section}
\end{document}

此程式碼的工作原理是將標頭組件 ( ) 的值\f@ncyoch\f@ncyech\f@ncyorh\f@ncyerh\f@ncyolh\f@ncyelh與標頭沒有實質內容 ( \unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x\unhbox \voidb@x) 的值進行比較。如果它們相同,則將規則寬度設為0pt。如果它們不同,則不會幹擾。

程式碼做出了 2 個重要假設。在大多數情況下,一種可能是合理的。另一個可能不是。

  • 程式碼假設您在偶數頁上沒有空白標題,並且在奇數頁上沒有內容豐富的標題。我認為在大多數情況下這是一個合理的假設。
  • 程式碼根據文件開頭的標題是否為空來啟用或停用整個文件的標題規則(或直到您發出更改設定的命令)。因此,如果您的配置類似於 Heiko Oberdiek 的範例,其中規則不應在第一頁上處於活動狀態,而應在下一頁上,則它將不起作用。假設標題要么包含整個文件的內容,要么不包含整個文件的內容,在大多數情況下可能是不合理的。

帶有規則的標題

註解掉標題行:

沒有標題 - 沒有規則

相關內容