6

This is a followup to How can I add “page # of ##” on my document?

The MWE below works fine with the scrpage2 line commented out, but not with. With that line uncommented, I get the error:

Class scrlttr2 Warning: Usage of package `fancyhdr' together
(scrlttr2)              with a KOMA-Script class is not recommended.
(scrlttr2)              I'd suggest to use 
(scrlttr2)              package `scrpage2' or `scrlayer-scrpage'.
(scrlttr2)              Nevertheless, using requested
(scrlttr2)              package `fancyhdr' on input line 7.

(/usr/share/texlive/texmf-dist/tex/latex/fancyhdr/fancyhdr.sty

! LaTeX Error: Command \chead already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.270 ...chead}{\@ifnextchar[{\@xchead}{\@ychead}}

So the scrlttr2 recommendation is apparently to move from fancyhdr to something based on scrpage2/scrheadings, which I'm already using for \rohead. How would do this? If I get an answer, I'll update my answer to the “page # of ##” question.

\documentclass[12pt]{scrlttr2}    
%\usepackage{scrpage2}
%\pagestyle{scrheadings}
%\rohead{\jobname.tex\hspace{0.5cm}\today}

\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhf{}%
  \fancyfoot[C]{\thepage\ of \pageref{LastPage}}}

\usepackage[T1]{fontenc}
\usepackage{fouriernc}

\begin{document}

\def\today{10th December, 2015}
\begin{letter}{To Address}

  \opening{Addressee}

  \closing{Yours Sincerely,}

\end{letter}
\end{document}
Faheem Mitha
  • 7,778
  • 2
    fancyhdr and scrpage2 are trying to do the same functionality for you. That can't work. Use only one of them: scrpage2 or better scrlayer-page or fancyhdr. – Mensch Dec 13 '15 at 17:47
  • 1
    As Kurt already noted: It's a wellknown fact that KOMA and fancyhdr do not really like each other –  Dec 13 '15 at 17:48
  • Yes, I get that. My attempt at replacing everything with KOMA specific stuff has not been successful, though. I guess I can add my attempts to this question. – Faheem Mitha Dec 13 '15 at 18:17
  • Why would you update your answer to the other question when KOMA is not involved there? fancyhdr is perfectly fine. It just doesn't agree with KOMA classes/packages. I rely exclusively on fancyhdr but I would not if I were using KOMA. – cfr Dec 13 '15 at 20:40
  • with scrlayer-scrpage you probably want \cfoot{\thepage\ of \pageref{LastPage}} – Johannes_B Dec 13 '15 at 20:46
  • @cfr Well, it's a general question. It doesn't specify what LaTeX class is being used. Actually it doesn't even specify LaTeX. – Faheem Mitha Dec 13 '15 at 20:56

1 Answers1

9

With KOMA-Script version 3.19 or newer and documentclass scrlttr2 there is no need to load an additional package to get page # of ## in your letter(s). You only have to redefine \pagemark using \pageref{\thisletter.lastpage}:

\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

If the page number should also be on the first page add the following line:

\setkomavar{firstfoot}{\parbox{\linewidth}{\centering\pagemark}}

MWE:

\documentclass[12pt]{scrlttr2}[2015/10/03]

\setkomavar{firstfoot}{\parbox{\linewidth}{\centering\pagemark}}
\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\usepackage{blindtext}
% only for dummytext
\begin{document}
\setkomavar{date}{10th December, 2015}% note that I have changed the koma variable date instead \today

\begin{letter}{To Address}
  \opening{Addressee}
  \Blindtext[10]
  \closing{Yours Sincerely,}
\end{letter}
\end{document}

Or with your headline:

\documentclass[12pt]{scrlttr2}[2015/10/03]
\usepackage{scrlayer-scrpage}
\ohead*{\jobname.tex\hspace{0.5cm}\usekomavar{date}}

\setkomavar{firstfoot}{\parbox{\linewidth}{\centering\pagemark}}
\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\usepackage{blindtext}
% only for dummytext
\begin{document}

\setkomavar{date}{10th December, 2015}
\begin{letter}{To Address}
  \opening{Addressee}
    \Blindtext[10]
  \closing{Yours Sincerely,}
\end{letter}
\end{document}

It is also possible to print a second letter with correct pagenumbers in the same document:

\documentclass[12pt]{scrlttr2}[2015/10/03]
\usepackage{scrlayer-scrpage}
\ohead*{\jobname.tex\hspace{0.5cm}\usekomavar{date}}

\setkomavar{firstfoot}{\parbox{\linewidth}{\centering\pagemark}}
\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\usepackage{blindtext}% only for dummytext
\begin{document}
\setkomavar{date}{10th December, 2015}

\begin{letter}{To Address}
  \opening{Addressee}
    \Blindtext[10]
  \closing{Yours Sincerely,}
\end{letter}

\begin{letter}{To Address}
\setkomavar{subject}{Second letter}
  \opening{Second Addressee}
    \Blindtext[20]
  \closing{Yours Sincerely,}
\end{letter}
\end{document}

Info for older versions: With KOMA-Script version 3.18 or older you have to load package lastpage.

\documentclass[12pt]{scrlttr2}
\usepackage{lastpage}
%\usepackage{scrlayer-scrpage}
%\ohead*{\jobname.tex\hspace{0.5cm}\usekomavar{date}}
\setkomavar{firstfoot}{\parbox{\linewidth}{\centering\pagemark}}
\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\usepackage{blindtext}
\begin{document}

\setkomavar{date}{10th December, 2015}
\begin{letter}{To Address}

\opening{Addressee}
\Blindtext[10]
\closing{Yours Sincerely,}

\end{letter}
\end{document}

Additionally here is an example for other classes with only one pagenumbering scheme. If you load both packages scrlayer-scrpage and lastpage you can redefine \pagemark:

\documentclass{book}
\usepackage{lastpage}
\usepackage{scrlayer-scrpage}
\renewcommand\pagemark{{\usekomafont{pagenumber}%
  \pagename\nobreakspace\thepage\nobreakspace
    of\nobreakspace\pageref{\thisletter.lastpage}%
}}

\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}
esdd
  • 85,675
  • Oh, very interesting. – Johannes_B Dec 13 '15 at 20:48
  • @Johannes_B AFAIR it has to do with http://tex.stackexchange.com/a/255392/43317, see also http://www.komascript.de/release3.19. – esdd Dec 13 '15 at 21:09
  • Does the new approach work with \documentclass{scrartcl}? I can't get it to; I keep getting ! Undefined control sequence. for both \pagename and \pageref{\thisletter.lastpage}. (Sorry, I'm a LaTeX newbie!) – ELLIOTTCABLE Jan 21 '16 at 20:08
  • @ELLIOTTCABLE Both commands are defined by the scrlttr2 class or the package scrletter for usage with letters. Note that \pageref{\thisletter.lastpage} only works inside the letter environment. – esdd Jan 21 '16 at 23:39
  • Your first version, for "KOMA-Script version 3.19 or newer ", works for me with the KOMA in TeX Live 2015 (labelled as 3.19a). But you wrote "documentclass scrlttr2". Does it not work for other KOMA classes then? Anyway, thanks very much for the through answer, and accepted. Sorry I was so slow. – Faheem Mitha Apr 05 '16 at 21:52
  • \pageref{\thisletter.lastpage} can only be used inside KOMA-Script letter environments. Therefore class scrlttr2 or package scrletter must be loaded. If package scrletter is used \letterpagemark must be redefined instead \pagemark. – esdd Apr 06 '16 at 06:39
  • @esdd Ok, so the package scrletter is required if the scrlttr2 class is not being used. Got it. Thanks. – Faheem Mitha May 18 '16 at 18:09