\thepage 计数器并不总是完美地工作

\thepage 计数器并不总是完美地工作

我有一个无法解决的小问题,而且我也不明白原因。

\thepage计数器有时比实际调用的页面早一个页面

我认为这是因为文本在添加到输出之前已被格式化。

这是一个最小工作示例。

\documentclass[9pt]{extbook}

\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}

\usepackage[french]{babel}

\usepackage{geometry}
\geometry{
    right=1.2cm,
    left=1.8cm,
    top=1.8cm,
    bottom=1cm,
    paperwidth=110mm,
    paperheight=178mm
}


\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}


\begin{document}
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup
  
\end{document}

在此处输入图片描述

您可以看到,第 2、3 和 4 页的顶部\thepage显示的是 1、2 和 3,而不是 2、3 和 4。

\\当您使用而不是 时,情况会更糟obeylines

\documentclass[9pt]{extbook}

\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}

\usepackage[french]{babel}

\usepackage{geometry}
\geometry{
    right=1.2cm,
    left=1.8cm,
    top=1.8cm,
    bottom=1cm,
    paperwidth=110mm,
    paperheight=178mm
}


\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}


\begin{document}
\titre{}
\begingroup
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\endgroup

\titre{}
\begingroup
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\

\titre{}
\begingroup
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\endgroup

\titre{}
\begingroup
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
~\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\thepage\\
I'm an amazing poem\\
\endgroup
  
\end{document}

在此处输入图片描述

我提前感谢那些能够向我解释该问题原因的聪明人。

万分感谢!

编辑

即使有了\label\pageref,问题仍然存在。

例如:

\documentclass[9pt]{extbook}


%pdfLaTeX
\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}

\usepackage[french]{babel}

\usepackage{geometry}
\geometry{
    right=1.2cm,
    left=1.8cm,
    top=1.8cm,
    bottom=1cm,
    paperwidth=110mm,
    paperheight=178mm
}


\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}

\newcounter{nbrefpage}
\setcounter{nbrefpage}{0}
\newcommand\afficherpage{%
    \stepcounter{nbrefpage}%
    \def\tmpnbrefpage{\arabic{nbrefpage}testpage}%
    \label{\tmpnbrefpage}%
    \pageref{\tmpnbrefpage}%
}


\begin{document}
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
  
\end{document}

结果(当然是经过两次编译之后)是一样的:

在此处输入图片描述

.aux文件中,我们看到:

\newlabel{16testpage}{{}{1}}

代替

\newlabel{16testpage}{{}{2}}

答案1

你允许在\label和打印数字之间留出分页符,添加一个框将它们放在一起

\documentclass[9pt]{extbook}


%pdfLaTeX
\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}

\usepackage[french]{babel}

\usepackage{geometry}
\geometry{
    right=1.2cm,
    left=1.8cm,
    top=1.8cm,
    bottom=1cm,
    paperwidth=110mm,
    paperheight=178mm
}


\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}

\newcounter{nbrefpage}
\setcounter{nbrefpage}{0}
\newcommand\afficherpage{%
    \stepcounter{nbrefpage}%
    \def\tmpnbrefpage{\arabic{nbrefpage}testpage}%
    \mbox{\label{\tmpnbrefpage}%
    \pageref{\tmpnbrefpage}}%
}


\begin{document}
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup

\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
  
\end{document}

相关内容