Problem with page numbering styles with letter page style in scrletter

koma-scriptpage-numberingpagestylescrletter

All page references here correspond to the English KOMA-Script manual, version number 2.28.

Consider the following TeX file.

\documentclass[12pt,headheight=30pt,headinclude,firsthead=false,firstfoot=false,foldmarks=false,foldmarks=blmtP,fromalign=center,fromphone,fromemail,version=last, backaddress=false, subject=titled, twoside=semi, cleardoublepage=current]{scrletter}
\usepackage{scrlayer-scrpage}
\usepackage{lipsum}
\usepackage{lastpage}
\ohead{\jobname.tex\hspace{0.5cm}\today}
\renewcommand\pagemark{{\usekomafont{pagenumber}\thepage\ of \pageref{LastPage}}}
\ofoot*{}
\cfoot{\pagemark}
\RequirePackage[T1]{fontenc}
\begin{document}
\def\today{19th February, 2022}
\begin{letter}{Name}
  \opening{Dear Name,}
  \thispagestyle{scrheadings}
  \lipsum
\end{letter}
\lipsum
\end{document}

The document generated with PDFLaTeX has 4 pages. My question is about
the page numbers. As you can see, the page numbers are Page 1, Page 2, 3 of 4, 4 of 4. This is unexpected behavior.

If one replaces scrletter with scrlttr2, one gets the expected
behavior, namely 1 of 4, 2 of 4, 3 of 4, 4 of 4.

This is probably related to the fact that scrletter defines its own
page styles, namely plain.letter and letter. It seems that this
page style has been created for letters that are embedded in documents
of other classes, which is the scrletter use case. And it also seems
that this page style silently overrides the style i've set, at least
for the page numbers. But this is all semi-guesswork. I've been
reading the manual, trying to make sense of it.

For example, in Section 4.13: "Headers and Footers with the Default
Page Style" pg 231 is written:

scrletter letter Since the headings page style is generally already in
use by the classes, the scrletter package instead
defines the letter page style. This is accomplished
with the help of scrlayer-scrpage in chapter 5, page
254. With the automark=true setting enabled, letter
then assumes the role played by headings in
scrlttr2. With automark=false set, letter assumes the
role of myheadings.

I've read this a number of times, but it still reads like borderline
gibberish. But perhaps I haven't read it often enough. I've also read
the relevant parts of Ch 5, which make sense on their own, but I don't
see how they relate to the issue here.

On pg 229 it says:

See section 22.2 for more information about the characteristics of
the page style of the scrletter package.

Section 22.2 says

Currently, additional information on this topic can be found at the
same point in the German KOMA-Script book [Koh20a] only.

If anyone has the German book, and would like to summarize the
contents of that section, please do.

And also on pg 229, it says

Example: You are using the scrletter package and want letters to
use the same page style that was set for the rest of the document
with \pagestyle. To do this, put the command
\renewcommand{\letterpagestyle}{} in your document preamble. Notice
the star in \renewcommand*.

This seems like it might do what I want, but it didn't work for me.

Best Answer

The class scrletter is an undocumented wrapper-class. It loads class scrartcl with font size 12pt and package scrletter.

\pagemark is already defined by the KOMA-Script scrartcl. Therefore package scrletter defines \letterpagemark for the letters in the document. If the format of the page numbers in the letters should be the same as in the other part of the document, use

\let\letterpagemark\pagemark

before environment letter.

\documentclass[
  %12pt,% default
  headheight=30pt,headinclude,
  firsthead=false,firstfoot=false,
  foldmarks=false,
  %foldmarks=blmtP,% disabled by foldmarks=false
  %fromalign=center,% disabled by firsthead=false
  %fromphone,fromemail,% disabled by firsthead=false
  %version=last,
  backaddress=false,
  subject=titled,
  twoside=semi,
  cleardoublepage=current
]{scrletter}
%\usepackage{scrlayer-scrpage}% load by package scrletter 
\usepackage{lipsum}% only for dummy text
\usepackage[T1]{fontenc}

\clearpairofpagestyles
\ohead{\jobname.tex\hspace{0.5cm}\usekomavar{date}}
\cfoot{\pagemark}
\renewcommand*{\pagemark}{{\usekomafont{pagenumber}{\thepage\ of \pageref{LastPage}}}}
\let\letterpagemark\pagemark
\AddToHook{shipout/lastpage}{\label{LastPage}}% replaces package lastpage

\renewcommand*{\letterpagestyle}{scrheadings}% page style for the next pages of the letter
\AddToHook{cmd/opening/after}{\thispagestyle{\letterpagestyle}}% use the same page style on first letter page

\setkomavar{date}{19th February, 2022}% do not change \today

\begin{document}
\begin{letter}{Name}
  \opening{Dear Name,}
  \thispagestyle{scrheadings}
  \lipsum
\end{letter}
\lipsum
\end{document}

enter image description here

Note:

\renewcommand*{\letterpagestyle}{scrheadings}% page style for the next pages of the letter
\AddToHook{cmd/opening/after}{\thispagestyle{\letterpagestyle}}% use the same page style on first letter page

can be replaced by

\renewcommand*\letterpagestyle{}% do not change the page style for the letter
\AddToHook{cmd/opening/after}{\thispagestyle{scrheadings}}% use scrheadings on first letter page

The result would be the same.

Related Question