1

I am using \pagenumbering{gobble} to hide page numbers in some pages. I am also using fancyhdr and lastpage packages to show page numbers for other pages like -

Page \thepage of \pageref{LastPage}

However for gobbled pages it shows something like -

Page of 4 %only last page number is shown

I want to hide the whole thing when \thepage is gobbled.

What I have tried so far is -

\rfoot{\ifnum \thepage>0 Page~\thepage~of~\pageref{LastPage}\else \fi}

This compiles and works as intended, but there is a error message when \newpage is incountered.

Missing number, treated as zero. \newpage

I have also tried to use \@ifundefined as was suggested in this answer but couldn't get it to work

Can anyone suggest any ideas?

Here's a compilable sample document source.

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}

\pagestyle{fancy}

\renewcommand{\footrulewidth}{0.4pt}% default is 0pt

\lhead{}
\chead{}
\rhead{}

\lfoot{}
\cfoot{}
\rfoot{\ifnum \thepage>0 Page~\thepage~of~\pageref{LastPage}\else \fi}

\begin{document}
    \pagenumbering{gobble}
    First Page
    \newpage
    Second Page
    \newpage
    \pagenumbering{arabic}
    Third Page
    \newpage
    Forth Page
\end{document}

I am using portable miketex for windows.

th1rdey3
  • 275

2 Answers2

3

While \pagenumbering{gobble} is a usefull trick in some cases, you should better define a real page style:

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}

\pagestyle{fancy}

\renewcommand{\footrulewidth}{0.4pt}% default is 0pt

\lhead{}
\chead{}
\rhead{}

\lfoot{}
\cfoot{}
\rfoot{Page~\thepage~of~\pageref{LastPage}}

\fancypagestyle{emptywithlines}{%
 \pagestyle{fancy}%
 \rfoot{}}

\begin{document}
    \pagestyle{emptywithlines}
    First Page
    \newpage
    Second Page
    \newpage
    \pagenumbering{arabic}% if page numbering should start by 1
    \pagestyle{fancy}
    Third Page
    \newpage
    Forth Page
\end{document}
Ulrike Fischer
  • 327,261
2

See the update at the end of the answer,please

Using \meaning\thepage reveals that

\thepage is defined as \csname @gobble\endcsname \c@page

if \pagenumbering{gobble} was applied.

This definition can be used in a \ifx... conditional statement to check whether precisely this has been done, i.e.

\makeatletter
    \rfoot{\def\foo{\csname @gobble\endcsname \c@page}
    \ifx\thepage\foo
    % Yes, it is gobbled!
    % Perhaps some other statement here? 
    \else 
    Page \thepage{} of \pageref{LastPage}%
    \fi
 }% End of \rfoot
\makeatother

The \makeatletter...\makeatother pair is needed for the \@gobble macro names having the @.

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}

\pagestyle{fancy}

\renewcommand{\footrulewidth}{0.4pt}% default is 0pt

\lhead{}
\chead{}
\rhead{}

\lfoot{}
\cfoot{}
\makeatletter
\rfoot{\def\foo{\csname @gobble\endcsname \c@page}\ifx\thepage\foo\else Page \thepage{} of \pageref{LastPage}\fi}
\makeatother

\begin{document}
    \pagenumbering{gobble}
    First Page
    \newpage
    Second Page
    \newpage
    \pagenumbering{arabic}
    Third Page
    \newpage
    Forth Page
\end{document}

Update -- with some improvements

It's possible to store the \thepage definition when gobble is active and compare this later on.

In order to simplify the format to be output, I've defined two hooks, \@gobble@thepagehook which should be executed if gobbling is active and \@nogobble@thepagehook which comes into action if gobble is not set. See the relevant code portions please.

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{fancyhdr}
\usepackage{lastpage}

\pagestyle{fancy}
\renewcommand{\footrulewidth}{0.4pt}% default is 0pt
\fancyhf{}

\pagenumbering{gobble}
\makeatletter
\let\gobble@thepage\thepage
\rfoot{%
  \ifx\gobble@thepage\thepage
  \@gobble@thepagehook%
  \else
  \@nogobble@thepagehook%
  \fi
}

%\let\@gobble@thepagehook\relax% For real gobbling
\newcommand{\@gobble@thepagehook}{\textbf{I am gobbled}}% For code golfing :-P
\newcommand{\@nogobble@thepagehook}{%
  Page \thepage{} of \pageref{LastPage}%
}

\makeatother
\pagenumbering{arabic}

\begin{document}
    \pagenumbering{gobble}
    First Page
    \newpage
    Second Page
    \newpage
    \pagenumbering{arabic}
    Third Page
    \newpage
    Forth Page
\end{document}

enter image description here

  • it's working :). thanks for the explanation. – th1rdey3 Aug 30 '16 at 12:10
  • 1
    @th1rdey3: Yes, as long as gobbling isn't defined otherwise! Happy TeXing! –  Aug 30 '16 at 12:11
  • 1
    @th1rdey3: I updated my answer with a better version -- it is easier to configure, in my point of view –  Aug 30 '16 at 20:16
  • that's pretty interesting too, though a little over my head. I would still prefer the one liner. – th1rdey3 Aug 31 '16 at 04:10
  • @th1rdey3: Regarding the number of upvotes my solution seems to be rubbish ;-) However, you can squeeze the new code in one-line if you wish :D –  Aug 31 '16 at 11:38