tex4ht 中自定义部分级别和 ID

tex4ht 中自定义部分级别和 ID

我需要自定义 html 文档中的所有五级部分 ID。我将尝试通过我的.cfg文件获取下面提到的要求。但我无法获得所需的输出。

我的必需部分 ID 输出:

\section{} - <secone id="j_\jobname_s_001"> - 1
\subsection{} - <sectwo id="j_\jobname_s_001_s_001"> - 1.1
\subsubsection{} - <secthree id="j_\jobname_s_001_s_001_s_001"> - 1.1.1
\paragraph{} - <secfour id="j_\jobname_s_001_s_001_s_001_s_001"> - 1.1.1.1
\subparagraph{} - <secfive id="j_\jobname_s_001_s_001_s_001_s_001_s_001"> - 1.1.1.1.1 

我当前的输出:

<secone id="j_Sample_s_1">
<title>
   <a 
 id="x1-10001"></a>Heading Level 1</title>

应删除下面提到的标签: <a id="x1-10001"></a>

我所需的 html 输出是:

<secone id="j_\jobname_s_001">
<title>1 Heading Level 1</title>
<p>Section Paragraph.</p>
<sectwo id="j_\jobname_s_001_s_001">
<title>1.1 Heading Level 2</title>
<p>SubSection Paragraph.</p>
<secthree id="j_\jobname_s_001_s_001_s_001">    
<title>1.1.1 Heading Level 3</title>
<p>SubSubSection Paragraph.</p>
<secfour id="j_\jobname_s_001_s_001_s_001_s_001">
<title>1.1.1.1 Heading Level 4</title>
<p>Paragraph Sample.</p>
<secfive id="j_\jobname_s_001_s_001_s_001_s_001_s_001">
<title>1.1.1.1.1 Heading Level 5</title>
<p>SubParagraph Sample.</p>
</secfive>
</secfour>
</secthree>
</sectwo>
</secone> 

我的CFG文件是:

\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subsection}
   {\EndP\IgnorePar\Tg<sectwo id="j_\jobname_s_\thesubsection">}
   {\EndP\Tg</sectwo>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subsubsection}
   {\EndP\IgnorePar\Tg<secthree id="j_\jobname_s_\thesubsubsection">}
   {\EndP\Tg</secthree>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{paragraph}
   {\EndP\IgnorePar\Tg<secfour id="j_\jobname_s_\theparagraph">}
   {\EndP\Tg</secfour>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

\Configure{subparagraph}
   {\EndP\IgnorePar\Tg<secfive id="j_\jobname_s_\thesubparagraph">}
   {\EndP\Tg</secfive>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

梅威瑟:

\documentclass{article}



\begin{document}

\section{Heading Level 1}

Section Paragraph. 

\subsection{Heading Level 2}

SubSection Paragraph.

\subsubsection{Heading Level 3}

SubSubSection Paragraph. 

\paragraph{Heading Level 4}

Paragraph Sample. 

\subparagraph{Heading Level 5}

SubParagraph Sample. 

\end{document}

如果该部分未编号,则不应在 html 输出中查看该编号。例如:

\section*{Heading Level 1}

The output should be  
<secone id="j_\jobname_s_001">
<title>Heading Level 1</title>

请指教如何自动化部分级别 ID。

答案1

标签<a id="x1-10001"></a>是从目录到某个部分的目的地。您可以删除它,但请注意,您无法通过这种方式从目录中获得有效的链接(至少不是以一种简单的方式)。如果可以的话,您可以使用以下配置:

\def\gobbleone#1#2{}
\Configure{toTocLink}{\gobbleone}{}

默认使用产生链接的命令toTocLink,它有两个参数,所以我们使用丢弃两个参数的命令来代替。

要获取部分编号,您必须将\TitleMark命令添加到section配置中:

\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

您还必须配置\TitleMark以摆脱添加的标记:

\ConfigureMark{section}{\thesection\space}

未编号的部分存在问题,因为它们会破坏您的id方案。您想将其用作\thesectionid 的基础,但 id 不会为它们更新,因此您会得到重复的ids。您应该改用独立计数器,并且可能使用一些不同的方案。像这样:

\newcount\unnumbered
\unnumbered=0

\Configure{likesection}
   {%
   \advance\unnumbered by1%
   \EndP\IgnorePar\Tg<secone id="j_\jobname_us_\the\unnumbered">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}

完整的配置文件可能如下所示:

\Preamble{xhtml}
\setcounter{secnumdepth}{5}
\Configure{section}
   {\EndP\IgnorePar\Tg<secone id="j_\jobname_s_\thesection">}
   {\EndP\Tg</secone>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{section}{\thesection\space}

\Configure{subsection}
   {\EndP\IgnorePar\Tg<sectwo id="j_\jobname_s_\thesubsection">}
   {\EndP\Tg</sectwo>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subsection}{\thesubsection\space}

\Configure{subsubsection}
   {\EndP\IgnorePar\Tg<secthree id="j_\jobname_s_\thesubsubsection">}
   {\EndP\Tg</secthree>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subsubsection}{\thesubsubsection\space}
\Configure{paragraph}
   {\EndP\IgnorePar\Tg<secfour id="j_\jobname_s_\theparagraph">}
   {\EndP\Tg</secfour>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{paragraph}{\theparagraph\space}
\Configure{subparagraph}
   {\EndP\IgnorePar\Tg<secfive id="j_\jobname_s_\thesubparagraph">}
   {\EndP\Tg</secfive>}
   {\Tg<title>\TitleMark}
   {\Tg</title>\ShowPar}

\ConfigureMark{subparagraph}{\thesubparagraph\space}

% Get rid of 

\def\gobbleone#1#2{}
\Configure{toTocLink}{\gobbleone}{}

\newcount\unnumbered
\unnumbered=0

\Configure{likesection}
   {%
   \advance\unnumbered by1%
   \EndP\IgnorePar\Tg<secone id="j_\jobname_us_\the\unnumbered">}
   {\EndP\Tg</secone>}
   {\Tg<title>}
   {\Tg</title>\ShowPar}
\begin{document}
\EndPreamble

请注意使用\setcounter{secnumdepth}{5},这是获得编号段落和小段落所必需的。

现在我们需要修复 ID 以遵循您请求的方案。您现在得到的内容例如:

 <sectwo id="j_sample_s_1.1">

_s_虽然可以在 TeX 端用零填充并用 替换点,但我认为使用Lua提供的过滤器更容易make4ht。将此文件另存为mybuild.mk4

Make:htlatex{}

local filter = require "make4ht-filter"

-- number of characters at each section level
-- adjust this to your needs
local zeroes = 3

local fixsecid = function(s) 
  return s:gsub('id="j_([^%"]-)_s_([0-9%.]+)"', function(a,b)
    local t = {}
    for part in b:gmatch("([0-9]+)") do
      -- print(mytonumber(part))
      t[#t+1] = string.rep("0", zeroes - string.len(part)) .. part 
    end
    local new = table.concat(t, "_s_")
    print("new section id",a,new)
    return string.format('id="j_%s_s_%s"', a,new)
  end
  ) 
end

local process = filter{fixsecid}

Make:match("html$",process)

它使用正则表达式来检测部分id并修复编号。

使用以下方式编译文件

 make4ht -uc config-file.cfg -e mybuild.mk4 filename

这是我为您的样本所得到的结果,其中添加了一些未编号的部分:

<secone id="j_sample_us_1">
<title>
   Unnumbered section</title>
<!--l. 9--><p class="noindent" >
</p>
</secone>
<secone id="j_sample_s_001">
<title>
   1 Heading Level 1</title>
<!--l. 11--><p class="indent" >   Section Paragraph.
</p><!--l. 14--><p class="noindent" >
</p>
<sectwo id="j_sample_s_001_s_001">
<title>
   1.1 Heading Level 2</title>
<!--l. 16--><p class="indent" >   SubSection Paragraph.
</p><!--l. 18--><p class="noindent" >
</p>
<secthree id="j_sample_s_001_s_001_s_001">
<title>
   1.1.1 Heading Level 3</title>
<!--l. 20--><p class="indent" >   SubSubSection Paragraph.
</p>
<secfour id="j_sample_s_001_s_001_s_001_s_001">
<title>
   1.1.1.1 Heading Level 4</title>
<!--l. 24--><p class="indent" >   Paragraph Sample.
</p>
</secfour>
<secfour id="j_sample_s_001_s_001_s_001_s_002">

<title>
   1.1.1.2 Heading Level 4. Again</title>
<!--l. 28--><p class="indent" >   Another paragraph sample.
</p>
<secfive id="j_sample_s_001_s_001_s_001_s_002_s_001">
<title>
   1.1.1.2.1 Heading Level 5</title>
<!--l. 32--><p class="indent" >   SubParagraph Sample.
</p><!--l. 34--><p class="noindent" >
</p>
</secfive>
</secfour>
</secthree>
</sectwo>
</secone>
<secone id="j_sample_us_2">
<title>
   again</title>
<!--l. 36--><p class="noindent" >
</p>
</secone>
<secone id="j_sample_us_3">
<title>
   and again</title>
</secone>

相关内容