我在 eso-pic 中实现此解决方案时遇到了麻烦:如果pagestyle等于...——我该如何询问呢?看起来\thepagestyle
每个页面都显得“花哨”,包括明显“朴素”的页面。
\documentclass[12pt,twoside]{book}
\usepackage[paperwidth=4in,paperheight=2in,left=0.5in,top=0.25in,%
right=0.25in,bottom=0.25in]{geometry}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{eso-pic}
\makeatletter
\pretocmd{\pagestyle}{%
\gdef\@regularpagestyle{#1}%
\gdef\thepagestyle{\if@specialpage\@specialstyle\else\@regularpagestyle\fi}
}
{\message{(patching of \string\pagestyle\ succeeded)}}
{\message{(patching of \string\pagestyle\ failed)}}
\makeatother
\pagestyle{fancy}
\begin{document}
\AddToShipoutPicture{%
\fontsize{24.88pt}{24.88pt}\selectfont%
\ifdef{\thepagestyle}{%
The page style is `\thepagestyle'.%
}{%
There is no page style declared.%
}}
\thispagestyle{plain}
\ifdef{\thepagestyle}{
The page style is `\thepagestyle'.%
}{%
There is no page style declared.
}
\end{document}
我的目标是为每种页面样式设置不同的背景图像。我可以任何一个设置持续默认值(使用\AddToShipoutPicture
),或者单个页面(使用\AddToShipoutPicture*
),但不能同时进行。
同时使用它们会产生可怕的结果。清除单个页面的默认值后就无法再次设置默认值,除非我知道确切的页面边界。
我认为,这里的解决方案将是一个糟糕的借口:如何清除当前页面的背景?这会导致文件体积过大和内部数据质量低下。
现在我试图将所有逻辑放在一个调用中\AddToShipoutPicture
(在我的自定义命令中\PageDecor
):
\documentclass[10pt,twoside]{book}
\usepackage[paperwidth=6in,paperheight=9in,left=1in,top=1in,right=0.75in,bottom=1in]{geometry}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{ifthen}
\makeatletter
\pretocmd{\pagestyle}{
\gdef\@regularpagestyle{#1}
\gdef\thepagestyle{\if@specialpage\@specialstyle\else\@regularpagestyle\fi}
}
{\message{(patching of \string\pagestyle\ succeeded)}}
{\message{(patching of \string\pagestyle\ failed)}}
\makeatother
\usepackage{eso-pic}
\newcommand{\PageDecor}{\AddToShipoutPicture{%
\ifodd\value{page}%
\ifthenelse{\equal{\thepagestyle}{fancy}}{%
\includegraphics[width=6in]{decor-6x9-right-fancy.pdf}%
}{%
\includegraphics[width=6in]{decor-6x9-right-plain.pdf}%
}%
\else%
\ifthenelse{\equal{\thepagestyle}{fancy}}{%
\includegraphics[width=6in]{decor-6x9-left-fancy.pdf}%
}{%
\includegraphics[width=6in]{decor-6x9-left-plain.pdf}%
}%
\fi%
}}
\newcommand{\PageDecorFancyOnce}{\AddToShipoutPicture*{%
\ifodd\value{page}%
\includegraphics[width=6in]{decor-6x9-right-fancy.pdf}%
\else%
\includegraphics[width=6in]{decor-6x9-left-fancy.pdf}%
\fi%
}}
\newcommand{\PageDecorPlainOnce}{\AddToShipoutPicture*{%
\ifodd\value{page}%
\includegraphics[width=6in]{decor-6x9-right-plain.pdf}%
\else%
\includegraphics[width=6in]{decor-6x9-left-plain.pdf}%
\fi%
}}
\begin{document}
\pagestyle{fancy}
\pagenumbering{arabic}
\setcounter{page}{1}
\fancypagestyle{plain}{\fancyhf{} \cfoot[\thepage]{\thepage}}
\fancyhf{}
\lhead[{\makebox[0.25in][l]{\hfill\thepage\hfill}}]{}
\rhead[]{{\makebox[0.25in][r]{\hfill\thepage\hfill}}}
\PageDecor
\thispagestyle{plain}
Testing testing 0123456789
\clearpage
\thispagestyle{plain}
Testing testing 0123456789
\clearpage
Testing testing 0123456789
\clearpage
Testing testing 0123456789
\end{document}
我可以在这里模拟我想要的输出,因为我知道每个页面的边界:
%\PageDecor
\thispagestyle{plain}
\PageDecorPlainOnce
Testing testing 0123456789
\clearpage
\thispagestyle{plain}
\PageDecorPlainOnce
Testing testing 0123456789
\clearpage
\PageDecorFancyOnce
Testing testing 0123456789
\clearpage
\PageDecorFancyOnce
Testing testing 0123456789
恐怕我对 Matthew Leingang 的修改还不够了解,无法自己解决。我搜索了这些符号的定义\if@specialpage
等,但无济于事。对我来说,它仍然是一个黑匣子。
任何帮助或见解都将不胜感激。
答案1
以下提供页面样式特定背景的接口:
\documentclass[twoside]{book}
\usepackage{fancyhdr,graphicx,afterpage,eso-pic}
\renewcommand{\headrulewidth}{0pt}
\let\oldpagestyle\pagestyle
\let\oldthispagestyle\thispagestyle
\renewcommand{\pagestyle}[1]{% Update \pagestyle{<style>}...
\gdef\thepagestyle{#1}% ...to store <style> in \thepagestyle
\oldpagestyle{#1}}
\renewcommand{\thispagestyle}[1]{% Update \thispagestyle{<style>}...
\xdef\savepagestyle{\thepagestyle}% ...to store <style> in \thepagestyle ...
\gdef\thepagestyle{#1}%
\afterpage{\xdef\thepagestyle{\savepagestyle}}% ...and revert to original <style> after this page
\oldthispagestyle{#1}}
\newcommand{\PageDecor}{\AddToShipoutPicture{%
\ifodd\value{page} % Odd page
\ifnum\pdfstrcmp{\thepagestyle}{fancy} = 0 % Page style is fancy
\AtPageCenter{\makebox[0pt]{\raisebox{-.5\height}{\scalebox{10}{FO}}}}
\else% \thepagestyle = \pagestyleplain (or another page style)
\AtPageCenter{\makebox[0pt]{\raisebox{-.5\height}{\scalebox{10}{PO}}}}
\fi
\else% Even page
\ifnum\pdfstrcmp{\thepagestyle}{fancy} = 0 % Page style is fancy
\AtPageCenter{\makebox[0pt]{\raisebox{-.5\height}{\scalebox{10}{FE}}}}
\else% \thepagestyle = \pagestyleplain (or another page style)
\AtPageCenter{\makebox[0pt]{\raisebox{-.5\height}{\scalebox{10}{PE}}}}
\fi
\fi
}}
\fancyhf{}% Clear header/footer
\lhead[\thepage]{}% Odd left header
\rhead[]{\thepage}% Even right header
\fancypagestyle{plain}{%
\fancyhf{}% Clear header/footer
\cfoot[\thepage]{\thepage}% Center footer (odd & even)
\gdef\thepagestyle{plain}}
\begin{document}
\pagestyle{fancy}
\PageDecor
\thispagestyle{plain}
Page 1 (plain page style)
\clearpage
Page 2 (fancy page style)
\clearpage
Page 3 (fancy page style)
\clearpage
\thispagestyle{plain}
Page 4 (plain page style)
\end{document}
我们更新\pagestyle
并\thispagestyle
存储当前页面样式,\thepagestyle
并\AddToShipoutPicture
使用 eTeX在内部进行相应的条件调整\pdfstrcmp{<strA>}{<strB>}
,如果<strA>
=则返回 0 <strB>
。