머리글이 없으면 눈금자를 자동으로 제거합니다.

머리글이 없으면 눈금자를 자동으로 제거합니다.

이것은 촉발된 질문입니다.한 페이지짜리 문서에서 페이지 번호를 자동으로 제거합니다..

활성화할 때fancyhdr패키지를 사용하려면 헤더를 지워야 합니다. 그렇지 않으면 아래 문서의 헤더로 \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. 서로 다르다면 방해가 되지 않습니다.

코드에서는 두 가지 중요한 가정을 합니다. 대부분의 경우 하나는 아마도 합리적일 것입니다. 다른 하나는 아마도 그렇지 않을 것입니다.

  • 코드에서는 짝수 페이지에 빈 헤더가 없고 홀수 페이지에 내용이 풍부한 헤더가 없다고 가정합니다. 나는 이것이 대부분의 경우 합리적인 가정이라고 생각합니다.
  • 코드는 문서 시작 부분의 헤더가 비어 있는지 여부에 따라 전체 문서에 대한(또는 설정을 변경하는 명령을 실행할 때까지) 헤더 규칙을 활성화하거나 비활성화합니다. 따라서 구성이 Heiko Oberdiek의 예에서 규칙이 첫 번째 페이지에서는 활성화되지 않고 다음 페이지에서는 활성화되어야 하는 것과 같은 경우에는 작동하지 않습니다. 헤더에 전체 문서에 대한 내용이 있거나 전체 문서에 대한 내용이 없다고 가정하는 것은 대부분의 경우 합리적이지 않습니다.

규칙이 있는 헤더

헤더 라인을 주석 처리합니다:

헤더 없음 - 규칙 없음

관련 정보