頁首/頁尾僅在目前頁面或其後續頁面上

頁首/頁尾僅在目前頁面或其後續頁面上

我定義了一個名為 的宏\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:編輯為完整範例,接受許多指令

相關內容