根据情况调整乳胶页眉

Adapt latex page header depending on situations

我在 LaTeX 中使用以下命令添加了页眉:

\pagestyle{fancy} \fancyhead[L]{\rightmark} \fancyhead[R]{\leftmark}

这是一个最小的可重现示例:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}  

\title{Titlepage}
\date{October 2021}

\pagestyle{fancy} \fancyhead[L]{\rightmark} \fancyhead[R]{\leftmark}
\usepackage{graphicx} 
\usepackage{titlesec} 
\usepackage{lipsum} %just to generate text for the example

\newcommand*\HUGE{\Huge}
\newcommand*\chapnamefont{\normalfont\LARGE\MakeUppercase CHAPTER}
\newcommand*\chapnumfont{\normalfont\HUGE}
\newcommand*\chaptitlefont{\normalfont\HUGE\bfseries}

\newlength\beforechapskip
\newlength\midchapskip
\setlength\midchapskip{\paperwidth}
\addtolength\midchapskip{-\textwidth}
\addtolength\midchapskip{-\oddsidemargin}
\addtolength\midchapskip{-1in}
\setlength\beforechapskip{18mm}

\titleformat{\section}[display]
  {\normalfont\filleft}
  {{\chapnamefont}%
    \makebox[0pt][l]{\hspace{.8em}%
      \resizebox{!}{\beforechapskip}{\chapnumfont\thesection}%
      \hspace{.8em}%
      \rule{\midchapskip}{\beforechapskip}%
    }%
  }%
  {25pt}
  {\chaptitlefont}
\titlespacing*{\section}
  {0pt}{40pt}{40pt}

\begin{document}

\maketitle

\newpage
blank page
\newpage

\section{Introduction}
the page header should not be visible here

\newpage

\subsection{Test2}
only the section should be visible in the page header (1 INTODUCTION) 

\newpage
only the subsection should be visible in the page header (1.1 Test2) 

\end{document}

使用此命令存在它出现在所有页面上的问题,但对于我的论文,我想将它从该部分首次出现的页面中删除

这是一个当前的示例:

当该部分首次出现时,它应该是这样的:

另外,我希望页眉根据它在哪一侧(偶数或奇数)进行调整。例如,我想在左侧页面上只显示部分名称(偶数页),在右侧显示小节名称(奇数页):

我会使用另一个文档class。这将自动解决您的大部分问题:

  • 在报告或书籍class中,章节的第一页自动使用另一种没有页眉的页面样式

  • 如果您使用双面文档,您可以根据 E(ven) 或 O(dd) pages

    定义 fancyhead

\documentclass[twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}  

\title{Titlepage}
\date{October 2021}

\pagestyle{fancy} 

\fancyhead{}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\usepackage{graphicx} 
\usepackage{titlesec} 
\usepackage{lipsum} %just to generate text for the example

\newcommand*\HUGE{\Huge}
\newcommand*\chapnamefont{\normalfont\LARGE\MakeUppercase CHAPTER}
\newcommand*\chapnumfont{\normalfont\HUGE}
\newcommand*\chaptitlefont{\normalfont\HUGE\bfseries}

\newlength\beforechapskip
\newlength\midchapskip
\setlength\midchapskip{\paperwidth}
\addtolength\midchapskip{-\textwidth}
\addtolength\midchapskip{-\oddsidemargin}
\addtolength\midchapskip{-1in}
\setlength\beforechapskip{18mm}

\titleformat{\chapter}[display]
  {\normalfont\filleft}
  {{\chapnamefont}%
    \makebox[0pt][l]{\hspace{.8em}%
      \resizebox{!}{\beforechapskip}{\chapnumfont\thechapter}%
      \hspace{.8em}%
      \rule{\midchapskip}{\beforechapskip}%
    }%
  }%
  {25pt}
  {\chaptitlefont}
\titlespacing*{\chapter}
  {0pt}{40pt}{40pt}

\begin{document}

\maketitle

\newpage
blank page
\newpage

\chapter{Introduction}
the page header should not be visible here

\newpage

\section{Test2}
only the section should be visible in the page header (1 INTODUCTION) 

\newpage
only the subsection should be visible in the page header (1.1 Test2) 

\end{document}