2

Consider this MWE (compiles with pdflatex -shell-escape test.tex):

\documentclass{book}
\usepackage{filecontents}
\begin{filecontents*}{insert.tex}
  \documentclass{article}
  \usepackage{lipsum}
  \begin{document}
  \thispagestyle{empty}
  \lipsum[1-2]
  \end{document}
\end{filecontents*}

\usepackage{pdfpages}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} %delete everything
\renewcommand{\headrulewidth}{0pt} %remove the horizontal line in the header
\fancyhead[RE]{\small\nouppercase\leftmark} %even page - chapter title
\fancyhead[LO]{\small\nouppercase\rightmark} %uneven page - section title
\fancyfoot[CE,CO]{AX-\thepage} %page number on all pages

\immediate\write18{pdflatex insert.tex} % compile this, to insert as pdf

\begin{document}

\chapter{Test chapter}
\thispagestyle{fancy}
\section{Test section}

So, blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah ...

\clearpage

\includepdf[pages=-,pagecommand=\thispagestyle{plain}]{insert.pdf}

\end{document}

Here, the first page outputs "AX-1" as pagenumber; but the included pdf shows just "2", because I use pagecommand=\thispagestyle{plain}.

I would like to have page number "AX-2" on the second page too - but if I use pagecommand=\thispagestyle{fancy}, while I get "AX-2" there, I also get the section header which I don't want.

What I'd want ideally is to make a "copy" of the plain pagestyle, say named as plainB, and only redefine \fancyfoot[CE,CO]{AX-\thepage} for it; so I could use it as pagecommand=\thispagestyle{plainB} and have only the customized page number "AX-2" output.

Is this possible at all to do - and if it is, how can I do it?

1

2 Answers 2

1

You can define a new page style. For example:

\fancypagestyle{customplain}{%
\fancyhf{}%
\fancyhf[cf]{<whatever>}%
\renewcommand*\headrulewidth{0pt}}

and then use it as you wish.

2
  • Many thanks for that @cfr, worked great: I used pagecommand=\thispagestyle{customplain}, and inside the handler, both \fancyhf[CF]{AX-\thepage} and \fancyfoot[CE,CO]{AX-\thepage} seem to work the same (and as expected). Cheers!
    – sdaau
    Jun 5, 2015 at 0:31
  • @sdaau Yes. I just tend to use the \fancyhf version for whatever reason. Glad it helped.
    – cfr
    Jun 5, 2015 at 0:54
0

To make a copy of an existing page style, include it in your new page style and redefine whatever you need:

enter image description here

\documentclass{article}
\usepackage{fancyhdr,lipsum}

% fancy page style
\fancyhf{}% Clear header/footer
\fancyhead[L]{LH}\fancyhead[C]{CH}\fancyhead[R]{RH}
\fancyfoot[L]{LF}\fancyfoot[C]{CF}\fancyfoot[R]{RF}

% New fancy page style
\fancypagestyle{newfancy}{
  \pagestyle{fancy}% Duplicate fancy page style
  \fancyhead[C]{ch}% Update components of fancy
}

\begin{document}
\pagestyle{fancy}
\lipsum[1-5]

\lipsum[1-5]
\pagestyle{newfancy}

\end{document}

\pagestyle{<style>} (and \thispagestyle{<style>}) is purely a directive to execute a bunch of macros (contained within \ps@<style>). That's why calling the page style from within another is okay - it sets the "defaults" associated with the original style, together with any modifications you include after it.

5
  • This is interesting but it doesn't seem to work for plain.
    – cfr
    Jun 5, 2015 at 0:45
  • @cfr: Can you provide an example?
    – Werner
    Jun 8, 2018 at 16:08
  • 3.5 years later?! For all I know, whatever I tried works now. If you have any idea what I might have meant, I might be able to figure out some more details. Why do you think I wanted it with plain?
    – cfr
    Jun 9, 2018 at 2:19
  • I'm not responsible for the down-vote, by the way. If that's what you're wondering. At least, the system says I'm not. I assume it is right. And it seems independently plausible.
    – cfr
    Jun 9, 2018 at 2:20
  • Possibly: that if you substitute plain for fancy in the definition of newfancy, you do not get what you might expect. That is, it makes no difference whether you use fancy or plain in the definition of newfancy: the result is the same. Similarly, empty etc. I don't think you can clone the styles this way, at least in these cases. But probably I misunderstood your answer and you meant something else.
    – cfr
    Jun 9, 2018 at 3:12

You must log in to answer this question.

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