![現在のページのみ、または次のページにもヘッダー/フッターを表示する](https://rvso.com/image/390930/%E7%8F%BE%E5%9C%A8%E3%81%AE%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AE%E3%81%BF%E3%80%81%E3%81%BE%E3%81%9F%E3%81%AF%E6%AC%A1%E3%81%AE%E3%83%9A%E3%83%BC%E3%82%B8%E3%81%AB%E3%82%82%E3%83%98%E3%83%83%E3%83%80%E3%83%BC%2F%E3%83%95%E3%83%83%E3%82%BF%E3%83%BC%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B.png)
\pagecurrent
ヘッダーとフッターに関連する他のマクロで使用される という名前のマクロを定義します。たとえば\cfoot, \chead, \renewcommand{\headrule}
、... です。その機能は、マクロ (\cfoot,\chead,\renewcommand{\headrule}
など) のアクションが現在のページのみに適用されるか、残りのページにも適用されるかを判断することです。次の例では、2 番目のページのヘッダー コンテンツを「現在のページ」にし、3 番目のページのヘッダーを「その他のページ」にします。しかし、実際には、両方のページに同じヘッダー「現在のページ」があります。これは「 」が原因だと思います\the\value{page}
。ただし、 以外に余分なコードを追加したくはありません\pagecurrent
。この問題に対処する簡単な方法はありますか?
MWE:
\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: 多くのコマンドが受け入れられる完全な例に編集されました