仅在当前页面或其后续页面显示页眉/页脚

仅在当前页面或其后续页面显示页眉/页脚

我定义了一个名为的宏\pagecurrent,它将在其他与页眉和页脚相关的宏中使用,例如\cfoot, \chead, \renewcommand{\headrule}...。它的功能是确定宏(\cfoot,\chead,\renewcommand{\headrule}等)的操作是否仅在当前页面上,还是也在其他页面上。在下面的例子中,我希望第二页的页眉内容是“当前页”,第三页的页眉为“其他页”。但实际上,两个页面都有相同的页眉 - “当前页”。我猜这是由“ \the\value{page}”引起的。但我不想添加额外的代码\pagecurrent。有没有简单的方法来解决这个问题?

梅威瑟:

\documentclass{article}
\usepackage{fancyhdr,geometry,etoolbox}
\geometry{showframe}

\begin{document}
\pagestyle{fancy}
\pagenumbering{Roman}
%
\newcommand{\pagecurrent}[3]{%
  % #1-the current page number. If #1=0 then act on all pages from the current page, else only on the current page.
  % #2-action on the current page
  % #3-action on the following pages other than the current page
  \ifboolexpr{
    test {\ifnumequal{#1}{\value{page}}}
    or
    test {\ifnumequal{#1}{0}}
  }
  {#2} {#3}
}
% example:
first page\clearpage
\chead{\pagecurrent{\the\value{page}}{current page}{other pages}}
second page\clearpage third page
\end{document}

编辑:我想提供以下代码以使我的问题更加清晰和集中:

\documentclass{article}
\usepackage{fancyhdr,geometry,etoolbox}
\geometry{showframe}

\begin{document}
\pagenumbering{Roman}
\pagestyle{fancy}

\chead{...}\rhead{...}\cfoot{...}\renewcommand{headrule}{...}...% macros related to header/footer that defines the original header/footer content.

...some text

at a certain page: \chead{\pagecurrent{content of chead from only this page changed}} 
% all other headers/footers of this page keep there original content. All headers/footers before/after this page keep there original content.

...some text

%\chead is used to illustrate my intention, maybe other macros like \rhead, \lfoot...in real article meet also the same problem.

\end{document}

答案1

这是一种方法,但是我改变了你的命令的参数并且还添加了包atbgshi而不是etoolbox

\documentclass[twoside]{article}
\usepackage{fancyhdr,geometry}
\usepackage{atbegshi}
\geometry{showframe}
\usepackage{lipsum}
\newcommand{\pagecurrent}[4][\empty]{%
% #2 is the command to be executed with:
% optional argument #1 (if not empty=default)
% argument #3 for current page and 
% argument #4 for the upcoming page.
  \xdef\FArg{#1}%
  \ifx\FArg\empty
  #2{#3}%
  \AtBeginShipout{#2{#4}}%
  \else 
  #2[\FArg]{#3}%
  \AtBeginShipout{#2[\FArg]{#4}}%
  \fi
}

\begin{document}
\pagestyle{fancy}
% example:
\section{Test section 1}
\pagenumbering{Roman}
first page\clearpage
\subsection{Test subsection 1}
\pagecurrent{\chead}{current page}{other pages}
\pagecurrent{\cfoot}{Current Page: \arabic{page}}{Other pages: \arabic{page}}
\pagecurrent{\rhead}{Current Page:\rightmark}{\rightmark}
\pagecurrent[LE,RO]{\fancyfoot}{Current page: \thepage}{Other pages: \thepage}
second page\clearpage third page
\lipsum[1-20]
\section{Test section 2}
\lipsum[1-15]
\subsection{Test subsection 1}
\lipsum[1-20]
\end{document}

PS:编辑为一个完整的示例,其中接受了许多命令

相关内容