将计数器的第一个值和最后一个值放在页面标题处

将计数器的第一个值和最后一个值放在页面标题处

我使用计数器来记录我的问题。我使用 fancyhdr。我需要在页面标题处显示计数器值的第一个值和最后一个值。

在以下示例中,第 2 页标题应为:问题:1-2

第 3 页标题应为:问题:2-3

第 4 页标题应为:问题:4-5

第 5 页标题应为:问题:6

我尝试了网上给出的几种建议。对我而言,没有一种奏效!特别是在第 3 页,当 LaTeX 决定将问题 4 放在下一页时,计数器已经递增,然后标题会显示问题 2-4,而问题 4 现在完全在下一页。当然,我永远无法在标题上单独看到问题:6。

这是我的 LaTeX 文件:

%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[12pt]{amsbook}
\usepackage{fancyhdr}
\usepackage{lipsum}

\newcounter{beans}
\setcounter{beans}{1}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{Dante}
\fancyhead[LO]{Meaning of Life}
\fancyfoot[LE,RO]{\thepage}

\textheight 7cm

\begin{document}
\chapter{Gate}
{\bf \thebeans.  Question: }
\lipsum[\thebeans]
\stepcounter{beans}

{\bf \thebeans.  Question: }
\lipsum[\thebeans]
\stepcounter{beans}

{\bf \thebeans.  Question: }
\lipsum[\thebeans]
\stepcounter{beans}

{\bf \thebeans.  Question: }
\lipsum[\thebeans]
\stepcounter{beans}

{\bf \thebeans.  Question: }
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo
\stepcounter{beans}

{\bf \thebeans.  Question: }
\lipsum[\thebeans]
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo
\stepcounter{beans}

\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%

谢谢。

答案1

您可以使用标记寄存器,而不必写入辅助文件。这样做的好处是只需要运行一次 LaTeX,而不是两次。代码还解决了另一个答案的评论:“问题 2-2”被替换为“问题 2”,并且支持跨多页的问题。

\documentclass[12pt]{amsbook}
\usepackage{fancyhdr}
\usepackage{lipsum}

\newmarks\beansmark
\newcounter{beans}

\newcommand{\beancounter}{%
  \leavevmode
  \stepcounter{beans}%
  \marks\beansmark{\arabic{beans}}%
}

\newcommand{\beanhead}{%
  \begingroup
    \edef\firstbean{\firstmarks\beansmark}%
    \edef\lastbean{\botmarks\beansmark}%
    \ifx\firstbean\lastbean
      Question \firstbean
    \else
      Question \firstbean--\lastbean
    \fi
  \endgroup
}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{Dante}
\fancyhead[LE]{\beanhead}
\fancyhead[LO]{Meaning of Life}
\fancyhead[RO]{\beanhead}
\fancyfoot[LE,RO]{\thepage}

\textheight 7cm

\begin{document}
\chapter{Gate}

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]


\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo

\end{document}

答案2

确定文本最终出现在哪一页的唯一可靠方法是将其写入辅助文件。需要运行两次才能更新标题。

\beancounter步骤 bean 并写入\thepage辅助文件。它必须与 在同一行Question,因此\leavevmode

\myhead搜索此页面上的所有问题并格式化标题。

\documentclass[12pt]{amsbook}
\usepackage{fancyhdr}
\usepackage{lipsum}

\newcounter{beans}
\newcounter{done}
\setcounter{done}{1}
\newif{\ifdone}

\makeatletter
\newcommand{\beancounter}{\leavevmode\stepcounter{beans}%
  \protected@write{\@auxout}{}{\string\gdef\string\beanpage\roman{beans}{\thepage}}}

\newcommand{\myhead}{\bgroup
  \donefalse
  \let\firstbean=\empty
  \let\lastbean=\empty
  \loop
    \@ifundefined{beanpage\roman{done}}{\donetrue}{%
      \ifnum\c@page=\csname beanpage\roman{done}\endcsname\relax
        \ifx\empty\firstbean\relax \edef\firstbean{\thedone}\fi
        \edef\lastbean{\thedone}
      \else
        \ifnum\c@page<\csname beanpage\roman{done}\endcsname\relax \donetrue\fi
      \fi}
  \ifdone\else\stepcounter{done}\repeat
  \ifx\empty\firstbean
  \else
    \ifx\firstbean\lastbean
      Question \firstbean
    \else
      Question \firstbean-\lastbean
    \fi
  \fi
\egroup}
\makeatother

\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{Dante}
\fancyhead[LE]{\myhead}
\fancyhead[LO]{Meaning of Life}
\fancyhead[RO]{\myhead}
\fancyfoot[LE,RO]{\thepage}

\textheight 7cm

\begin{document}
\chapter{Gate}

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]


\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]

\beancounter
{\bf \thebeans.  Question: }
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo

\beancounter
{\bf \thebeans.  Question: }
\lipsum[\thebeans]
Fusce mauris. Vestibulum luctus nibh at lectus.
Sed bibendum, nulla a faucibus semper, leo velit ultricies tellus, ac
venenatis arcu wisi vel nisl. Vestibulum diam. Aliquam pellentesque,
augue quis sagittis posuere, turpis lacus congue quam, in hendrerit
risus eros eget felis. Maecenas eget erat in sapien mattis porttitor.
Vestibulum porttitor. Nulla facilisi. Sed a turpis eu lacus commodo

\end{document}

相关内容