tex4ht:自定义列表...(第 2 部分,共 3 部分)

tex4ht:自定义列表...(第 2 部分,共 3 部分)

这是对tex4ht:自定义列表...(第 1 部分,共 2 部分)

我有一个自定义列表<things>。下面的代码提供了从 中的每个项目List of cmh到其在页面上的位置的链接。我希望(在 html 版本中)每次出现的 输出都\cmhcommand链接到 中的相关位置List of cmh

mwe.tex

\documentclass{article}

\usepackage{cmhloc}
\begin{document}
\tableofcontents

\section{normal section}
\cmhcommand{Here is some text}
some text
\section{another secton}
\cmhcommand{Another text}
\cmhcommand{some more}

\listofcmh
\end{document}

cmhloc.sty

\ProvidesPackage{cmhloc}
\newcommand{\cmhcommand}[1]{\addcontentsline{cmh}{subsection}{#1}#1}
\newcommand{\listofcmh}{\subsection*{List of cmh}\@starttoc{cmh}}
\endinput

cmhloc.4ht

\ConfigureToc{cmh}{\HCode{<div class="sectionToc">}}{~}{}{\HCode{</div>\Hnewline}} 
\renewcommand\listofcmh{\subsection*{List of cmh}\TableOfContents[cmh]}
\endinput

我用来生成版本的编译html

htlatex mwe.tex

答案1

在这种情况下,我们需要为每个 构建唯一的 id \cmhcommand,作为 TOC 的链接。我们将sty稍微修改一下文件:

\ProvidesPackage{cmhloc}
\newcommand\printcmhentry[1]{#1}
\newcommand{\cmhcommand}[1]{\addcontentsline{cmh}{subsection}{#1}\printcmhentry{#1}}
\newcommand{\listofcmh}{\subsection*{List of cmh}\@starttoc{cmh}}
\endinput

这样,我们只需要重新定义命令,而不需要在文件中\printcmhentry重复逻辑:\cmhcommand4ht

\RequirePackage{etoolbox}
\def\toccmh#1#2#3{%
  \bgroup% make the changes local
    \Configure{TocLink}{% This will print the link in TOC.
      \Link{##2}{\@nameuse{cmhcn-##4}}##4\EndLink% We need to  construct the ID parameter in second parameter for \Link
    }
    \HCode{<div class="sectionToc">}#2\HCode{</div>\Hnewline}% Print the entry
  \egroup%
}%


\newcount\cmhcount % each cmhentry will have it's unique idntifier
\renewcommand\printcmhentry[1]{%
  \advance\cmhcount by1\relax%
  \edef\cmhlink{cmh-\the\cmhcount}% this will be the id for the corresponding entry in the \listcmh
  \csxdef{cmhcn-#1}{\cmhlink}\Link{\cmhlink}{}#1\EndLink} % we use the entry text in csname, 
                                                          % in order to be able to retrieve the id later
                                                          % in the \listcmh entries

\append:def\listofcmh{\TableOfContents[cmh]}
\endinput

在这种情况下,\toccmh宏被定义而不是\ConfigureToc{cmh},它更灵活。我们使用\Configure{TocLink}来配置目录条目中的超链接,第一个参数是的id \addcontentsline,第二个参数是条目的锚点。例如\@nameuse{cmhcn-##4}扩展到\nameuse{cmhcn-Here is some text}。这个宏在中定义\renewcommand\printcmhentry,它包含文本链接到的id。

结果如下:

<h3 class="sectionHead"><span class="titlemark">1   </span>  <a 
 id="x1-20001"></a>normal section</h3>
<a 
 id="x1-2001"></a>
<!--l. 9--><p class="noindent" ><a 
href="#cmh-1">Here is some text</a> some text
</p>
   <h3 class="sectionHead"><span class="titlemark">2   </span>  <a 
 id="x1-30002"></a>another secton</h3>
<a 
 id="x1-3001"></a>
<!--l. 13--><p class="noindent" ><a 
href="#cmh-2">Another text</a>
</p><!--l. 16--><p class="indent" >   Inside paragraph: <a 
 id="x1-3002"></a><a 
href="#cmh-3">some more</a>. It doesn’t work well.
</p><!--l. 25--><p class="noindent" >
</p>
   <h4 class="likesubsectionHead"> <a 
 id="x1-40002"></a>List of cmh</h4>
   <div class="tableofcontents"> <div class="sectionToc"><a 
href="#x1-2001" id="cmh-1">Here is some text</a></div> 
 <div class="sectionToc"><a 
href="#x1-3001" id="cmh-2">Another text</a></div> 
 <div class="sectionToc"><a 
href="#x1-3002" id="cmh-3">some more</a></div> 

   </div> 

相关内容