98

I'm using the package fancyhdr to format my footers and headers, with my name in the header and a n of m pages format for the footer. Like so:

\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\fancyhead[C]{Name}
\fancyfoot[C]{\footnotesize Page \thepage\ of \pageref{LastPage}}

By default, the fancy page style only affects the pages after the first page. I know I can set the first page to the fancy page style using \thispagestyle, but I only want the first page to use the footer, not the header. Is there a way to create a custom style that just the first page will use?

0

5 Answers 5

97

The \fancypagestyle command can be used to create custom styles:

\fancypagestyle{firststyle}
{
   \fancyhf{}
   \fancyfoot[C]{\footnotesize Page \thepage\ of \pageref{LastPage}}
   \renewcommand{\headrulewidth}{0pt} % removes horizontal header line
}

Then just use \thispagestyle{firststyle} on the first page, immediately after \maketitle (if that is used).

2
  • how to remove the horizontal bar that is still appearing on the first page ?
    – The Beast
    Jun 3, 2016 at 2:02
  • 5
    The horizontal bar can be removed using the code \renewcommand{\headrulewidth}{0pt} Jan 24, 2017 at 10:07
36

The best strategy is probably to redefine the plain page style that LaTeX applies automatically with \maketitle in the article class or with \chapter in the report and book classes:

\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyfoot[C]{\footnotesize Page \thepage\ of \pageref{LastPage}}%
}
4
  • Do I need to explicitly put a \label{LastPage} on the last page?
    – Matsmath
    Nov 19, 2017 at 23:10
  • 5
    @Matsmath No, that's done automatically by the lastpage package, which you should load.
    – egreg
    Nov 19, 2017 at 23:26
  • Does not work for me in IEEE paper. Ony starts from second page. Had to add
    – dorien
    Dec 6, 2017 at 10:19
  • 1
    You need to add the lastpage package for this. Put \usepackage{lastpage} at the top of your file. Nov 2, 2020 at 5:02
12

A solution not constrained to cases where \maketitle is used and hence is perhaps more flexible:

\usepackage{ifthen}
...
\rhead{\ifthenelse{\value{page}=1}{\bfseries p1-title}{p2-and-following-title}}

MWE:

\documentclass{article}

\usepackage{lipsum}

\usepackage{ifthen}

% \usepackage[top=10mm, bottom=10mm, left=10mm, right=10mm,includehead,includefoot]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{}
\chead{}

\rhead{\ifthenelse{\value{page}=1}{\bfseries The performance of Black Swans}{second page}}

\lfoot{From: K. Smith}
\cfoot{To: Jean A. Cory}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}

\lipsum

\end{document}
4
  • The second of these pieces of code does not work, even when one corrects the obvious typos. \renewcommand is not approriate here, and you should just use the command \rhead as in you first snippet. There are formatting problems with you answer, and you should not just post a duplicate of another of your answers. A comment with a link would be more appropriate in this case. Jul 4, 2013 at 11:09
  • @AndrewSwann Marked the other Q&A as duplicate (and also deleted my answer there), and got rid of the "alternative" :-) Jul 4, 2013 at 11:48
  • That's an improvement. The first sentence of your answer still looks strange though and answers are better with a complete working document rather than code snippets. Jul 4, 2013 at 15:12
  • @AndrewSwann added an MWE, changed the first sentence ;) Jul 4, 2013 at 15:36
9

It might be the case that the first page is not showing the desired fancy style because you are using \maketitle in it. \maketitle automatically resets the pagestyle to plain.

In that case, simply adding \pagestyle{fancy} after the \maketitle command should solve the problem:

\maketitle
\pagestyle{fancy}   % add this after maketitle
3
  • Did you test this?
    – cfr
    Nov 4, 2016 at 3:21
  • @cfr, yes, I did test it. Note that different classes or packages my override \maketitle making it behave differently. Dec 20, 2016 at 19:53
  • 3
    For \documentclass{article} this worked for me: \maketitle \\ \thispagestyle{fancy}
    – alfC
    Nov 2, 2017 at 9:45
0

Another possibility: Instead of hard-coding something into the header or footer, use something like this:

\fancyhead[<where>]{\myCurrentHead}
\fancyfoot[<where>]{\myCurrentFoot}

Then dynamically re-define \myCurrentHead and \myCurrentFoot content on the fly. The headers and footers will apply the most recently seen values, at the time the end of the page is reached. The values can be styled. And, the values can be empty, equivalent to an omitted header or footer (without changing the page style).

I have used this for a collection of short stories, where the author's name appears in verso header, and the story title in recto header. Change the current content at each new story start.

Might be problems if the size of content (such as line count) changes, but I have not tested that.

You must log in to answer this question.