【问题标题】:Adapt latex page header depending on situations根据情况调整乳胶页眉
【发布时间】:2021-10-19 18:13:11
【问题描述】:

我在 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}

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

下面是它当前的外观示例:

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

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

【问题讨论】:

标签: latex pdflatex


【解决方案1】:

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

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

  • 如果您使用双面文档,您可以根据E(ven) 或O(dd) 页面定义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}

【讨论】:

  • 嗨,更改它以报告已修复的问题,非常感谢!顺便提一句。是否还有一个选项可以从没有任何部分的页面中删除页眉?例如,目录中没有页眉,或者当章节定义时没有任何部分?感谢您的帮助!
  • @igotPOWA 您可以在这些页面上使用\thispagestyle{plain} 或empty
  • 解决了,谢谢!
猜你喜欢
  • 2011-01-09
  • 1970-01-01
  • 2015-01-14
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多