scrpage2 与 fancyhdr 冲突

scrpage2 与 fancyhdr 冲突

这是 如何在我的文档中添加“第 # 页,共 ## 页”?

下面的 MWE 在scrpage2注释掉该行后工作正常,但在注释掉该行后则不行。取消注释该行后,我收到错误:

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}}

因此,scrlttr2建议显然是从 转移fancyhdr到基于scrpage2/ 的内容scrheadings,我已经在使用它了\rohead。该怎么做?如果我得到答案,我会更新对“第 # 页,共 ## 页”问题的回答。

\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}

答案1

KOMA-Script 版本 3.19 或更新版本和 documentclassscrlttr2不需要加载额外的包来获取page # of ##您的信件。您只需\pagemark使用以下方法重新定义\pageref{\thisletter.lastpage}

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

如果页码也应该在第一页,请添加以下行:

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

梅威瑟:

\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}

或者使用你的标题:

\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}

还可以在同一文档中打印具有正确页码的第二封信件:

\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}

旧版本的信息:使用KOMA-Script 版本 3.18 或更早版本您必须加载包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}

other classes另外,这里是只有一个页码方案的示例。如果您加载两个包scrlayer-scrpage,则lastpage可以重新定义\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}

相关内容