如何将“范围”部分设置到头部?scrpage2、scrbook、titlesec

如何将“范围”部分设置到头部?scrpage2、scrbook、titlesec

我想将一页的各部分的“范围”纳入标题中。我使用scrpage2 scrlayer-scrpage来更改标题,并使用 scrbook 作为 documentclass。我尝试编写 MWE:

\documentclass[a4paper,12pt,twoside]{scrbook} %KOMA-version: 3.12 (overleaf)
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[forceoverwrite]{scrlayer-scrpage}

\pagestyle{scrheadings}
%some stuff here

\begin{document}

\chapter{Sample chapter}
\section{Sample section}
\lipsum[1-2]
\section{section 2}
\lipsum[3-4]
\section{section 3}
\lipsum[5-6]
\section{section 4}
\lipsum[7-8]
\section{section 5}
\lipsum[1-2]

\end{document}

例如,如果第 1 至第 3 节位于一页上,那么页码的“范围”应为 1-3 等等(下一页为 3-5),类似于圣经。

我尝试创建一个命令:(该命令位于下scrpage2,因此已经过时了)

\newcounter{sectionone}
\newcounter{sectiontwo}
\newcounter{recentpage}
\newcommand{\updateheadrange}{%
\ifnum\recentpage=\thepage%
    \setcounter{sectionone}{\value{section}}%
\else%
    \setcounter{sectiontwo}{\value{section}}%
    \setcounter{recentpage}{\value{page}}%
\fi}

并改变标题:

\lehead{\thesectionone--\thesectiontwo}
\rohead{\thesectionone--\thesectiontwo}

但它并没有像我想要的那样工作。我对 LaTeX 并不陌生,但对“那种”编程却很陌生。

有更好的解决方案吗?也许是一个包之类的?能找到一个就太好了……

谢谢

如果只有第一节的编号(左侧)和最后一节的编号(右侧)也是可以的。

答案1

的后继者scrpage2scrlayer-scrpage。使用此包和最新的 KOMA-Script 版本,您可以使用类似

\documentclass[12pt]{scrbook}
\renewcommand\thesection{\arabic{section}}

\usepackage[manualmark]{scrlayer-scrpage}
\renewcommand\chaptermark[1]{\markboth{}{}}
\renewcommand\sectionmark[1]{\markright{\thesection}}
\clearpairofpagestyles

\ohead*{%
  \ifstr{\rightfirstmark}{}{\ifstr{\rightbotmark}{}{}{%
      \ifstr{\rightbotmark}{1}{1}{1\enskip--\enskip\rightbotmark}}}%
    {\ifstr{\rightbotmark}{1}{1}{%
      \ifstr{\righttopmark}{\rightbotmark}{\rightbotmark}{%
        \righttopmark\enskip--\enskip\rightbotmark}}}
}

\ofoot*{\pagemark}

\usepackage{blindtext}% dummy text
\begin{document}
\chapter{Sample chapter}
\section{Sample section}
\blindtext
\section{section 2}
\Blindtext
\section{section 3}
\Blindtext
\section{section 4}
\section{section 5}
\end{document}

结果:

在此处输入图片描述

但如果某个部分从新页面的顶部开始,则不会出现问题:

在此处输入图片描述

为了避免这种情况你可以\ohead*改为

\ohead*{%
  \ifstr{\rightfirstmark}{}{\ifstr{\rightbotmark}{}{}{1}}{\rightfirstmark}%
  \ifstr{\rightfirstmark}{\rightbotmark}{}{\ifstr{\rightbotmark}{1}{}{%
    \enspace--\enspace\rightbotmark}}
}

结果:

在此处输入图片描述

但是,您只有页眉中从当前页面开始的部分(请参阅 MWE 中的第 2 页)。

我不知道在这个 MWE 中是否有可能在第 2 页的标题中获得 2-3 以及在第 4 页的标题中获得 4-5。


补充说明因为对这个答案的评论:

如果你正在使用KOMA-Script 版本低于 3.16在设置之前添加以下几行\ohead

\makeatletter
\providecommand*{\rightfirstmark}{\expandafter\@rightmark\firstmark\@empty\@empty}
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\providecommand*{\righttopmark}{\expandafter\@rightmark\topmark\@empty\@empty}
\makeatother

此外,您不能使用星号版本\ohead等。KOMA-Script 3.14 之前的版本。因此,您必须删除星号,并将强制参数的代码复制到 \ohead 等可选参数中:\ohead[<code>]{<code>]。或者,您可以使用\ohead{<code>}并将章节页面的页面样式更改为scrheadings使用

\renewcommand*\chapterpagestyle{scrheadings}

相关内容