# Resetting LastPage in scrlttr2 letters

I'm trying to find a way to add "page X of Y" to the footers of my scrlttr2 letters. At first sight, using the lastpage package and \pageref*{LastPage} works. However, as soon as multiple letters are included in the document the value of LastPage is equal to the number of pages in the last letter. So, if, for example I have two letters in my document, one with 2 pages followed by on of 4 pages, the "Y" above is set to 4 also in the first letter.

My first idea was to define the footers (by setting \firstfoot and \nextfoot in \AtEndLetter{} because at that time I can use \thepage. Unfortunately (and, in retrospect obviously), in that case only the last page in each letter gets the correct footer (the ones before get the default scrlttr2 footers).

By the way, this is not a duplicate of How can I add "page # of ##" on my document?, I have read the answers there, but (understandably) none seem to deal with the fact that there can be more than one last page in a document.

You can define a new counter and then step the counter at the start of a letter and set label at the end:

\newcounter{Letter}
\AtBeginLetter{\stepcounter{Letter}}
\AtEndLetter{\label{lastpage\theLetter}}
\renewcommand\pagemark{\usekomafont{pagenumber}
page \thepage{} of \pageref{lastpage\theLetter}}


Do not use letter as counter name because the next KOMAScript version will define a new counter named letter (see the additional Information below).

Code:

\documentclass{scrlttr2}
\setkomavar{fromname}{Name}
\setkomavar{firstfoot}{\centering \pagemark}

\newcounter{Letter}
\AtBeginLetter{\stepcounter{Letter}}
\AtEndLetter{\label{lastpage\theLetter}}
\renewcommand\pagemark{\usekomafont{pagenumber}
page \thepage{} of \pageref{lastpage\theLetter}}

\usepackage{blindtext}% dummy text
\begin{document}
\begin{letter}{letter \theLetter}
\opening{letter \theLetter}
\Blindtext
\end{letter}

\begin{letter}{letter \theLetter}
\opening{letter \theLetter}
\Blindtext[20]
\end{letter}
\end{document}


I have read on the KOMA-Script website (only German) that there will be a new command \letterlastpage starting with version 3.19. Then you can simple use

\renewcommand\pagemark{\usekomafont{pagenumber}%
page \thepage{} of \letterlastpage}

• Thanks! That works brilliantly. I didn't think of redefining the \pagemark. Great to hear that there will be a command for this in the upcoming release. Jul 15 '15 at 14:20

After playing around with esdd's solution I decided to go for the following (since I'm defining my own footers):

\newcounter{Letter}
\AtBeginLetter{\stepcounter{Letter}}
\AtEndLetter{\label{lastpage\theLetter}}
\setkomavar{firstfoot}{\thepage/\pageref*{lastpage\theLetter}}
\setkomavar{nextfoot}{\usekomavar{firstfoot}}


This prints 1/3 on the first page of a 3-page letter, etc.

The following slightly more elaborate code only prints the X/Y in the footer if the total number of pages (Y) is larger than 1.

\newcounter{Letter}
\AtBeginLetter{\stepcounter{Letter}}
\AtEndLetter{\label{lastpage\theLetter}}

\AtBeginLetter{%
\ifnumcomp{\getpagerefnumber{lastpage\theLetter}}{>}{1}{%
\setkomavar{firstfoot}{\thepage/\pageref*{lastpage\theLetter}%
}{%
\setkomavar{firstfoot}{}}}%
}
\setkomavar{nextfoot}{\usekomavar{firstfoot}}