我的代码:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
Top of document
\section{Section heading}
Top of section
. \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\
. \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\
\phantomsection
\label{fooref}
We want to create a reference to this part of the document.
. \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\
. \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\ . \\
Go to reference \ref{fooref}.
Bottom of document.
\end{document}
如果我使用 PDF 查看器创建文档pdflatex foo.tex && pdflatex foo.tex
并打开它foo.pdf
,单击创建的引用\ref{fooref}
就会带我到当前的\phantomsection
位置\label{fooref}
。
但是,如果我使用 创建文档make4ht foo.tex
并用浏览器打开foo.html
,单击创建的引用\ref{fooref}
会将我带到页面顶部。这是一个错误吗?有办法解决这个问题吗?
答案1
尝试这个配置文件:
\Preamble{xhtml}
\makeatletter
\def\phantomsection{%
\Hy@GlobalStepCount\Hy@linkcounter
\xdef\@currentHref{likesection.\the\Hy@linkcounter}%
\Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
\edef\@currentlabel{\the\Hy@linkcounter}%
\AnchorLabel%
}
\makeatother
\begin{document}
\EndPreamble
\phantomesection
它重新定义了TeX4ht 使用的默认定义,它\label
通过以下几行添加了支持:
\edef\@currentlabel{\the\Hy@linkcounter}%
\AnchorLabel%
第一行定义文本中将使用的引用的值,第二行将链接目的地插入文本中。
这是最终的 HTML 代码:
<!-- l. 5 --><p class='noindent'>Top of document
</p>
<h3 class='sectionHead' id='section-heading'><span class='titlemark'>1 </span> <a id='x1-10001'></a>Section heading</h3>
<!-- l. 8 --><p class='noindent'>Top of section
</p><!-- l. 10 --><p class='indent'> . <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />
</p><!-- l. 13 --><p class='indent'> <a id='likesection.1'></a> <a id='x1-1001x1'></a> We want to create a reference to this part of the document.
</p><!-- l. 17 --><p class='indent'> . <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />. <br class='newline' />
</p><!-- l. 24 --><p class='indent'> Go to reference <a href='#x1-1001x1'>1<!-- tex4ht:ref: fooref --></a>.
</p><!-- l. 26 --><p class='indent'> Bottom of document. </p>
如您所见,<a id='x1-1001x1'></a>
插入到的位置\phantomsection
,并<a href='#x1-1001x1'>1</a>
指向该位置。