ヘッダーが存在しない場合はルーラーを自動的に削除します

ヘッダーが存在しない場合はルーラーを自動的に削除します

これは、1ページの文書からページ番号を自動的に削除する

起動するとファンシーHDRパッケージでは、ヘッダーをクリアする必要があります。これは、以下で で行います。そうし\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

次の例では、ヘッダー要素 (左、中央、右) をボックス内に配置し、その幅を測定します。幅が 0 の場合、ヘッダー ルールは抑制されます。

\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}

備考:

  • \headruleLaTeX はボックス/グループ内でヘッダー コードを呼び出すため、の再定義は現在のヘッダーに対してローカルです。

答え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 つの重要な仮定が立てられています。1 つはほとんどの場合に妥当な仮定ですが、もう 1 つは妥当でない可能性があります。

  • コードでは、たとえば偶数ページには空のヘッダーがなく、奇数ページにはコンテンツのあるヘッダーがあると想定しています。ほとんどの場合、これは妥当な想定だと思います。
  • このコードは、ドキュメントの先頭でヘッダーが空であるかどうかに基づいて、ドキュメント全体 (または設定を変更するコマンドを発行するまで) のヘッダー ルールを有効または無効にします。したがって、Heiko Oberdiek の例のように、たとえばルールが最初のページではアクティブではなく、次のページでアクティブになるような構成の場合は機能しません。ヘッダーにドキュメント全体のコンテンツが含まれるか、ドキュメント全体のコンテンツが含まれないかのどちらかであると想定することは、ほとんどの場合おそらく合理的ではありません。

ルール付きヘッダー

ヘッダー行をコメントアウトします。

ヘッダーなし - ルールなし

関連情報