2

I am using a document class that sets up some headers (using fancyhdr), and I would like to modify (not overwrite) these headers. For example:

The document class creates a right-side header saying "Default Text." I would like to change this to say "Default Text. My Text."

In my tex file, if I write:

\fancyhead[R]{My Text.}

then the text that was previously included in the right-side header gets erased and only "My Text." appears.

Is there some way to retrieve the contents of the header, such that I can reuse that content in my modified header? Something like:

\fancyhead[R]{\fetchRightHeader My Text.}

which (ideally) would result in the right-side header saying "Default Text. My Text."

In case it matters: I need to modify the right-side header on even pages and the left-side header on odd pages. The document class includes different text on the left and right side.

Bonus question: Is there a way to modify a fancypagestyle defined by the document class? The document class sets up a different style for the first page in order to use different headers for that page. I would like to modify these first page headers too.

Thanks!

Edit:

As suggested by Andrew, here is a minimal working example.

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{filecontents}

% Another file defines a header, which I want to modify.
\begin{filecontents*}{style.cls}
  \pagestyle{fancy}
  \fancyhead[R]{Default Text.}
\end{filecontents*}
\input{style.cls}

% This line overwrites the previously defined header,
% whereas I would like to keep the previous contents
% and also add my own.
\fancyhead[R]{My Text.}

\begin{document}
\section{Section 1}
Foo bar baz.
\end{document}

Here is the resulting PDF output from the above example:

Actual output from the example

For reference, here is my desired output:

Desired output

3
  • 3
    Welcome to TeX.SX!! Rather than posting code fragments it is better to give a full minimal working example. Currently we have to guess how you are defining your headers etc this makes it really hard to help you. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. It is really difficult to help you without more information.
    – user30471
    Commented May 12, 2019 at 6:35
  • Thank you for the suggestion, @Andrew! I edited my post to include a minimal working example. Commented May 12, 2019 at 20:01
  • To (re-) text, put it into a newcommand, first: ˋ\newcommand\mine[0]{my trxt}ˋ and call ˋ\mine{}ˋ .
    – MS-SPO
    Commented Aug 4, 2023 at 10:28

2 Answers 2

0

For me worked doing

\pagestyle{fancy}
\fancyhf[H]
{
 % header contents
}
\fancypagestyle{styleName}
{
 % specific page header content
}

For clarification, I created a general header using \fancyhf[H]{} and then created a specific page header using \fancypagestyle{}{}, I cannot say when this feature was implemented and if this answer is worth anything, but I'll leave it here for people who could stumble here like I had.

0

You can also save contents in macros, then use other macros to modify:

\documentclass{article}
\usepackage[twoside]{fancyhdr}
\edef\versoheadtext{Starting Verso Header}
\def\changeversohead#1{
  \edef\tempstring{\versoheadtext}
  \xdef\versoheadtext{\tempstring\space#1}
}
\edef\rectoheadtext{Starting Recto Header}
\def\changerectohead#1{
  \edef\tempstring{\rectoheadtext}
  \xdef\rectoheadtext{\tempstring\space#1}
}
\pagestyle{fancy}
\fancyhead[CE]{\versoheadtext}
\fancyhead[CO]{\rectoheadtext}
\begin{document}
It\par\clearpage
was\par\clearpage
\changerectohead{ ..darker}
\changeversohead{ ..stormier}
a\par\clearpage
dark\par\clearpage
and\par\clearpage
stormy\par\clearpage
night.\par\clearpage
\end{document}

You must log in to answer this question.

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