2

I'm having trouble implementing this solution inside eso-pic: If pagestyle equal to... -- How should I ask it? It looks like \thepagestyle is coming out equal to "fancy" on every page, including obviously and explicitly "plain" pages.

\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}

MWE

My goal is to set different background images for each page style. I can either set a continuing default (using \AddToShipoutPicture), or a single page (using \AddToShipoutPicture*), but not both.

Using them concurrently gives terrible results. Clearing the default for a single page leaves no way to set the default again, unless I know exact page boundaries.

The solution here would be a terrible cop-out imho: How to clear the background of just the current page? It would result in a bloated filesize and low quality internal data.

Right now I'm trying to put all the logic in a single call to \AddToShipoutPicture (in my custom command \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}

Actual Result

I can simulate my desired output here only because I know every page boundary:

%\PageDecor

\thispagestyle{plain}
\PageDecorPlainOnce
Testing testing 0123456789
\clearpage
\thispagestyle{plain}
\PageDecorPlainOnce
Testing testing 0123456789
\clearpage
\PageDecorFancyOnce
Testing testing 0123456789
\clearpage
\PageDecorFancyOnce
Testing testing 0123456789

Desired Result

I'm afraid I just don't understand Matthew Leingang's modifications well enough to troubleshoot them myself. I've searched for definitions to these symbols \if@specialpage, etc. to no avail. It remains entirely a black box for me.

Any help or insight would be appreciated.

2
  • A fundamental problem with your approach is that \thispagestyle never actually calls \pagestyle, so you don't really pick up on page styles set by something other than \pagestyle.
    – Werner
    May 4, 2016 at 20:30
  • Not that I don't believe you, but I'm confused by that because (as in the super-MWE I just added) the mod seems to pick up changes made by \thispagestyle just fine when used outside \AddToShipoutPicture
    – Quartz
    May 4, 2016 at 20:51

1 Answer 1

2

The following provides an interface for page style-specific backgrounds:

enter image description here

\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}

We update \pagestyle and \thispagestyle to store the current page style in \thepagestyle and condition accordingly inside \AddToShipoutPicture using the eTeX \pdfstrcmp{<strA>}{<strB>} which returns 0 if <strA> = <strB>.

3
  • You can put your background inclusions where I use \AtPageCenter{...} in each of the four possible conditional branches.
    – Werner
    May 4, 2016 at 21:03
  • There's something funny about your example, you set the page numbers in the bottom center for plain pages but they're showing up in the top corners anyway ... But your modifications to \pagestyle and \thispagestyle work like a charm in my MWE! Thanks!
    – Quartz
    May 4, 2016 at 21:18
  • Got it! You just forgot to include \oldthispagestyle again at the end of your new definition of \thispagestyle.
    – Quartz
    May 4, 2016 at 21:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .