如果 \label 恰好放置在靠近页面上边缘的位置,则 fancyhdr 中的 \pagestyle{fancy} 似乎会破坏 \label-\ref

如果 \label 恰好放置在靠近页面上边缘的位置,则 fancyhdr 中的 \pagestyle{fancy} 似乎会破坏 \label-\ref

\label我在环境中使用\begin{theorem} ... \end{theorem}。如果定理语句开始于自然分页符附近并且启用了\pagestyle{fancy}from ,则该特定定理似乎出现故障。fancyhdr\label ... \ref

  • 问题:单击链接无需指向定理环境,而是会转到正确位置之前的页面。

除此之外,一切似乎都运行良好,我偶然在包含数百个实例的大型文档中发现了这种意想不到的效果\label ... \ref。当然,MWE 大大降低了。


平均能量损失

\documentclass[12pt]{book}
\usepackage{setspace}
\usepackage{amsthm}
\usepackage{thmtools}
%\usepackage{mathtools}
%\usepackage{tocloft}
%\usepackage{titlesec}
\usepackage{fancyhdr} 
\usepackage[hypertexnames=false]{hyperref}
%\usepackage{crossreftools}

\declaretheorem[numberwithin=chapter,style=plain,name=Theorem,refname={theorem, theorems},Refname={Theorem,Theorems}]{theorem}

\begin{document}
    \pagestyle{fancy} %%%% <-- comment out to get working label-ref for 1.2
    
    \chapter{Test Chapter}
    
    \section{Test}
    
    \subsection{Testing}
    
    \onehalfspacing
    
    \newpage 
    s\\ 
    s\\
    s\\
    s\\
    s\\ 
    s\\
    s\\
    s\\
    
    s\\ 
    s\\
    s\\
    s\\
    \begin{theorem}
        \label{trm: test-label-to-theorem}
        s\\ 
        s\\
        s\\
        s\\
        s\\
        s\\
        s\\
    \end{theorem}
    \begin{proof} s\\ 
        s\\
        s\\
        s\\
        
        s\\ 
        s\\
    \end{proof}
    
    s%\\ %%%% <-- OR uncomment to get working label-ref for 1.2
    \begin{theorem}
        \label{trm: test-label-to-second-theorem}
        sd
    \end{theorem}
    \noindent Working~\ref{trm: test-label-to-theorem}, not working~\ref{trm: test-label-to-second-theorem}
\end{document}

输出第三页: 第三页的输出

  • 重现:1.1 的链接工作正常,但 1.2 却不行(链接到第 2 页,而不是预期的第 3 页)。在我的大型原始文档中,我检查了大约 30 对其他\label-\ref链接,全部按预期工作。

.aux文件中,上述 MWE 除其他内容外,还写道:

\newlabel{trm: test-label-to-second-theorem}{{1.2}{3}{}{theorem.7}{}}

对于新手来说,这似乎没什么问题,因为 {3} 是目标页码。但是,单击 1.2 仍然会转到第 2 页。

  • 问题原因:您可以注释掉fancyheader,问题就消失了。或者,您可以稍微改变布局,在定理 1.2 之前添加一行,使其不那么靠近页面顶部。然后问题也会消失。

问题。如何解决这个问题?

要将修复程序应用于我的实际文档,我实际上无法删除\pagestyle{fancy}。由于对的数量很大\label ... \ref,手动检查哪些链接无法正常工作并逐一进行更改是不现实的,因此有必要采取一些全局解决方案。除了Underfull \hbox'es 之外,没有给出任何错误或警告,无论是 MWE 还是我的原始文档。

我正在运行pdfTeX,版本 3.141592653-2.6-1.40.25(MiKTeX 23.5)(预加载格式= pdflatex2023.7.12);LaTeX2e<2023-06-01>补丁级别 1 L3 编程层<2023-06-30>;最新fancyhdr2022/11/09 v4.1;hyperref2023-06-25 v7.01a。


日志文件(用户账户名称已更改)

    This is pdfTeX, Version 3.141592653-2.6-1.40.25 (MiKTeX 23.5) (preloaded format=pdflatex 2023.7.17)  17 JUL 2023 23:13
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
**./VeaOtsing.tex
(VeaOtsing.tex
LaTeX2e <2023-06-01> patch level 1
L3 programming layer <2023-06-30>
(C:\Users\UserNameChanged\MikTeX\tex/latex/base\book.cls
Document Class: book 2023/05/17 v1.4n Standard LaTeX document class
(C:\Users\UserNameChanged\MikTeX\tex/latex/base\bk12.clo
File: bk12.clo 2023/05/17 v1.4n Standard LaTeX file (size option)
)
\c@part=\count185
\c@chapter=\count186
\c@section=\count187
\c@subsection=\count188
\c@subsubsection=\count189
\c@paragraph=\count190
\c@subparagraph=\count191
\c@figure=\count192
\c@table=\count193
\abovecaptionskip=\skip48
\belowcaptionskip=\skip49
\bibindent=\dimen140
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/setspace\setspace.sty
Package: setspace 2022/12/04 v6.7b set line spacing
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/amscls\amsthm.sty
Package: amsthm 2020/05/29 v2.20.6
\thm@style=\toks17
\thm@bodyfont=\toks18
\thm@headfont=\toks19
\thm@notefont=\toks20
\thm@headpunct=\toks21
\thm@preskip=\skip50
\thm@postskip=\skip51
\thm@headsep=\skip52
\dth@everypar=\toks22
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thmtools.sty
Package: thmtools 2023/05/04 v0.76
\thmt@toks=\toks23
\c@thmt@dummyctr=\count194

(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-patch.sty
Package: thm-patch 2023/05/04 v0.76

(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\parseargs.sty
Package: parseargs 2023/05/04 v0.76
\@parsespec=\toks24
))
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-kv.sty
Package: thm-kv 2023/05/04 v0.76
Package thm-kv Info: Theorem names will be uppercased on input line 42.

(C:\Users\UserNameChanged\MikTeX\tex/latex/graphics\keyval.sty
Package: keyval 2022/05/29 v1.15 key=value parser (DPC)
\KV@toks@=\toks25
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/kvsetkeys\kvsetkeys.sty
Package: kvsetkeys 2022-10-05 v1.19 Key value parser (HO)
)
Package thm-kv Info: kvsetkeys patch (v1.16 or later) on input line 158.
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-autoref.sty
Package: thm-autoref 2023/05/04 v0.76

(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\aliasctr.sty
Package: aliasctr 2023/05/04 v0.76
))
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-listof.sty
Package: thm-listof 2023/05/04 v0.76
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-restate.sty
Package: thm-restate 2023/05/04 v0.76
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/thmtools\thm-amsthm.sty
Package: thm-amsthm 2023/05/04 v0.76
\thmt@style@headstyle=\toks26
))
(C:\Users\UserNameChanged\MikTeX\tex/latex/fancyhdr\fancyhdr.sty
Package: fancyhdr 2022/11/09 v4.1 Extensive control of page headers and footers

\f@nch@headwidth=\skip53
\f@nch@O@elh=\skip54
\f@nch@O@erh=\skip55
\f@nch@O@olh=\skip56
\f@nch@O@orh=\skip57
\f@nch@O@elf=\skip58
\f@nch@O@erf=\skip59
\f@nch@O@olf=\skip60
\f@nch@O@orf=\skip61
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/hyperref\hyperref.sty
Package: hyperref 2023-06-25 v7.01a Hypertext links for LaTeX

(C:\Users\UserNameChanged\MikTeX\tex/generic/ltxcmds\ltxcmds.sty
Package: ltxcmds 2020-05-10 v1.25 LaTeX kernel commands for general use (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/iftex\iftex.sty
Package: iftex 2022/02/03 v1.0f TeX engine tests
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/pdftexcmds\pdftexcmds.sty
Package: pdftexcmds 2020-06-27 v0.33 Utility functions of pdfTeX for LuaTeX (HO
)

(C:\Users\UserNameChanged\MikTeX\tex/generic/infwarerr\infwarerr.sty
Package: infwarerr 2019/12/03 v1.5 Providing info/warning/error messages (HO)
)
Package pdftexcmds Info: \pdf@primitive is available.
Package pdftexcmds Info: \pdf@ifprimitive is available.
Package pdftexcmds Info: \pdfdraftmode found.
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/kvdefinekeys\kvdefinekeys.sty
Package: kvdefinekeys 2019-12-19 v1.6 Define keys (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/pdfescape\pdfescape.sty
Package: pdfescape 2019/12/09 v1.15 Implements pdfTeX's escape features (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/hycolor\hycolor.sty
Package: hycolor 2020-01-27 v1.10 Color options for hyperref/bookmark (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/letltxmacro\letltxmacro.sty
Package: letltxmacro 2019/12/03 v1.6 Let assignment for LaTeX macros (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/auxhook\auxhook.sty
Package: auxhook 2019-12-17 v1.6 Hooks for auxiliary files (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/hyperref\nameref.sty
Package: nameref 2023-06-18 v2.52 Cross-referencing by name of section

(C:\Users\UserNameChanged\MikTeX\tex/latex/refcount\refcount.sty
Package: refcount 2019/12/15 v3.6 Data extraction from label references (HO)
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/gettitlestring\gettitlestring.sty
Package: gettitlestring 2019/12/15 v1.6 Cleanup title references (HO)

(C:\Users\UserNameChanged\MikTeX\tex/latex/kvoptions\kvoptions.sty
Package: kvoptions 2022-06-15 v3.15 Key value format for package options (HO)
))
\c@section@level=\count195
)
(C:\Users\UserNameChanged\MikTeX\tex/latex/etoolbox\etoolbox.sty
Package: etoolbox 2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
\etb@tempcnta=\count196
)
\@linkdim=\dimen141
\Hy@linkcounter=\count197
\Hy@pagecounter=\count198

(C:\Users\UserNameChanged\MikTeX\tex/latex/hyperref\pd1enc.def
File: pd1enc.def 2023-06-25 v7.01a Hyperref: PDFDocEncoding definition (HO)
Now handling font encoding PD1 ...
... no UTF-8 mapping file for font encoding PD1
)
(C:\Users\UserNameChanged\MikTeX\tex/generic/intcalc\intcalc.sty
Package: intcalc 2019/12/15 v1.3 Expandable calculations with integers (HO)
)
\Hy@SavedSpaceFactor=\count199

(C:\Users\UserNameChanged\MikTeX\tex/latex/hyperref\puenc.def
File: puenc.def 2023-06-25 v7.01a Hyperref: PDF Unicode definition (HO)
Now handling font encoding PU ...
... no UTF-8 mapping file for font encoding PU
)
Package hyperref Info: Option `hypertexnames' set `false' on input line 4050.
Package hyperref Info: Hyper figures OFF on input line 4167.
Package hyperref Info: Link nesting OFF on input line 4172.
Package hyperref Info: Hyper index ON on input line 4175.
Package hyperref Info: Plain pages OFF on input line 4182.
Package hyperref Info: Backreferencing OFF on input line 4187.
Package hyperref Info: Implicit mode ON; LaTeX internals redefined.
Package hyperref Info: Bookmarks ON on input line 4434.
\c@Hy@tempcnt=\count266

(C:\Users\UserNameChanged\MikTeX\tex/latex/url\url.sty
\Urlmuskip=\muskip16
Package: url 2013/09/16  ver 3.4  Verb mode for urls, etc.
)
LaTeX Info: Redefining \url on input line 4772.
\XeTeXLinkMargin=\dimen142

(C:\Users\UserNameChanged\MikTeX\tex/generic/bitset\bitset.sty
Package: bitset 2019/12/09 v1.3 Handle bit-vector datatype (HO)

(C:\Users\UserNameChanged\MikTeX\tex/generic/bigintcalc\bigintcalc.sty
Package: bigintcalc 2019/12/15 v1.5 Expandable calculations on big integers (HO
)
))
\Fld@menulength=\count267
\Field@Width=\dimen143
\Fld@charsize=\dimen144
Package hyperref Info: Hyper figures OFF on input line 6051.
Package hyperref Info: Link nesting OFF on input line 6056.
Package hyperref Info: Hyper index ON on input line 6059.
Package hyperref Info: backreferencing OFF on input line 6066.
Package hyperref Info: Link coloring OFF on input line 6071.
Package hyperref Info: Link coloring with OCG OFF on input line 6076.
Package hyperref Info: PDF/A mode OFF on input line 6081.

(C:\Users\UserNameChanged\MikTeX\tex/latex/base\atbegshi-ltx.sty
Package: atbegshi-ltx 2021/01/10 v1.0c Emulation of the original atbegshi
package with kernel methods
)
\Hy@abspage=\count268
\c@Item=\count269
\c@Hfootnote=\count270
)
Package hyperref Info: Driver (autodetected): hpdftex.

(C:\Users\UserNameChanged\MikTeX\tex/latex/hyperref\hpdftex.def
File: hpdftex.def 2023-06-25 v7.01a Hyperref driver for pdfTeX

(C:\Users\UserNameChanged\MikTeX\tex/latex/base\atveryend-ltx.sty
Package: atveryend-ltx 2020/08/19 v1.0a Emulation of the original atveryend pac
kage
with kernel methods
)
\Fld@listcount=\count271
\c@bookmark@seq@number=\count272

(C:\Users\UserNameChanged\MikTeX\tex/latex/rerunfilecheck\rerunfilecheck.sty
Package: rerunfilecheck 2022-07-10 v1.10 Rerun checks for auxiliary files (HO)

(C:\Users\UserNameChanged\MikTeX\tex/generic/uniquecounter\uniquecounter.sty
Package: uniquecounter 2019/12/15 v1.4 Provide unlimited unique counter (HO)
)
Package uniquecounter Info: New unique counter `rerunfilecheck' on input line 2
85.
)
\Hy@SectionHShift=\skip62
)
\c@theorem=\count273

(C:\Users\UserNameChanged\MikTeX\tex/latex/l3backend\l3backend-pdftex.def
File: l3backend-pdftex.def 2023-04-19 L3 backend support: PDF output (pdfTeX)
\l__color_backend_stack_int=\count274
\l__pdf_internal_box=\box51
)
(VeaOtsing.aux)
\openout1 = `VeaOtsing.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for PD1/pdf/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
LaTeX Font Info:    Checking defaults for PU/pdf/m/n on input line 14.
LaTeX Font Info:    ... okay on input line 14.
Package hyperref Info: Link coloring OFF on input line 14.
 (VeaOtsing.out) (VeaOtsing.out)
\@outlinefile=\write3
\openout3 = `VeaOtsing.out'.


Chapter 1.
[1


{C:/Users/UserNameChanged/AppData/Local/MiKTeX/fonts/map/pdftex/pdftex.map}]
Underfull \hbox (badness 10000) in paragraph at lines 26--34

 []

Package hyperref Info: bookmark level for unknown theorem defaults to 0 on inpu
t line 40.

Underfull \hbox (badness 10000) in paragraph at lines 41--48

 []


Underfull \hbox (badness 10000) in paragraph at lines 49--53

 []

[2] [3] (VeaOtsing.aux)
 ***********
LaTeX2e <2023-06-01> patch level 1
L3 programming layer <2023-06-30>
 ***********
Package rerunfilecheck Info: File `VeaOtsing.out' has not changed.
(rerunfilecheck)             Checksum: 70E8118191D5A2EEAB659BE4B76F93F2;268.
 ) 
Here is how much of TeX's memory you used:
 8368 strings out of 475513
 132662 string characters out of 5771520
 1919238 words of memory out of 5000000
 29521 multiletter control sequences out of 15000+600000
 561190 words of font info for 46 fonts, out of 8000000 for 9000
 1141 hyphenation exceptions out of 8191
 75i,5n,79p,548b,538s stack positions out of 10000i,1000n,20000p,200000b,200000s
<C:/Users/UserNameChanged/MikTeX/fonts/type1/public/amsfonts/cm/cmbx12.pfb><C:/Users/R
asmus/MikTeX/fonts/type1/public/amsfonts/cm/cmr12.pfb><C:/Users/UserNameChanged/MikTeX/f
onts/type1/public/amsfonts/cm/cmsl12.pfb><C:/Users/UserNameChanged/MikTeX/fonts/type1/pu
blic/amsfonts/cm/cmti12.pfb>
Output written on VeaOtsing.pdf (3 pages, 49180 bytes).
PDF statistics:
 60 PDF objects out of 1000 (max. 8388607)
 11 named destinations out of 1000 (max. 500000)
 25 words of extra memory for PDF output out of 10000 (max. 10000000)

答案1

根据@Ulrike Fischer 的建议,可以amsthm修补\@thm\@begintheoremhttps://github.com/latex3/hyperref/issues/332#issuecomment-2004158459\usepackage[hypertexnames=false]{hyperref}例如,在 MWE 中用以下代码替换:

\makeatletter
\let\@kernel@refstepcounter\refstepcounter
\usepackage[hypertexnames=false]{hyperref}
\usepackage{xpatch}
\xpatchcmd\@thm{\refstepcounter}{\@kernel@refstepcounter}{}{}%
\xpatchcmd\@begintheorem{\thmhead{#1}}{\thmhead{\MakeLinkTarget{\@currentcounter}#1}}{}{}%
\makeatother

所以它根本不使用 hyperref 来放置目标锚点。

相关内容