使用 Confiig 文件更改 tex4ht 跨度类

使用 Confiig 文件更改 tex4ht 跨度类

我的目录如下:

<div class="col-sm-2" id="leftCol">
   <div class="nav nav-stacked" id="sidebar">
       <h3 class="likesectionHead">Contents</h3>

       <div class="tableofcontents">
       <span class="chapterToc">1 <a href="#x1-20001" id="QQ2-1-2">Python Primer</a></span><br />
       <span class="sectionToc">1.1 <a href="#x1-30001" id="QQ2-1-3">Reshaping the future</a></span><br />
       <span class="subsectionToc">1.1.1 <a href="#x1-40001" id="QQ2-1-4">Exercises</a></span><br />
       <span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-5">R-1.1</a></span>
       </div>
   </div>
</div>

我希望能够访问这些甜蜜的东西<span class="subsectionToc">并向<span class="subsubsectionToc">其中添加一些东西,例如:

<li class="nav-header"> <a href="#" data-toggle="collapse" data-target="#userMenu">
    <span class="subsectionToc"><i class="glyphicon glyphicon-chevron-down"></i>1.1.1 <a href="#x1-40001" id="QQ2-1-4">Exercises</a></span> 
    <ul class="list-unstyled collapse in" id="userMenu">
       <li class="active"> <span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-5">R-1.1</a></span></li>
       <li><span class="subsubsectionToc"><a href="#x1-50001" id="QQ2-1-6">R-1.2</a></span></li>
    </ul>
</li>

无需添加ConfigureEnv

我似乎无法找到使用来访问特定跨度类的方法\Configure,有人知道怎么做吗?

答案1

这绝对不容易。默认情况下,toc生成的tex4ht不是分层的,我们必须使用相当复杂的代码来实现它。这是从 借来的tex4ebook。第一个简单的帮助文件,hierarchictoc.sty

\ProvidesPackage{hiearchictoc}
\RequirePackage{etoolbox}
\endinput

该文件的主要目的是使我们能够\usepackage{hierarchictoc}.cfg文件中使用,而该文件又将包括主文件hierarchictoc.4ht

\NewConfigure{NavSection}{4}

\newbool{tocnoempty}

\newcount\navpoint
\def\stepnavpoint{\advance\navpoint by1\relax\the\navpoint}

% Commands for support of hiearchical table of contents
% Support for: chapter, section, subsection
\def\resettoclevels#1{%
\def\do##1{\csgdef{nav##1finish}{}}
\docsvlist{#1}
}
\def\usetoclevels#1{%
\def\do##1{\csuse{nav##1finish}}%
\docsvlist{#1}%
}
\def\finishtoclevel#1{%
\csgdef{nav#1finish}{\d:NavSection}
}
\def\closelevels#1{
\usetoclevels{#1}
\resettoclevels{#1}
}
% 1 - section type 2 - closed sections
\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}
\def\Ncx:Mark#1{%
\edef\:temp{#1}
\def\z@ro{0}
\ifx\:temp\z@ro\else#1\fi
}
%
\Configure{tableofcontents}{\HCode{<nav>\Hnewline<ul>}}{%
\usetoclevels{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}%
\HCode{</ul></nav>\Hnewline}}{}{}{}
% We need to configure TocLink
% Configuraion of entries
\resettoclevels{part,chapter,section,subsection,subsubsection}
\navsection{part}{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{chapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likechapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesection}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesubsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsubsection}{subsubsection,likesubsubsection}
\navsection{likesubsubsection}{subsubsection,likesubsubsection}

这是怎么回事?重要的是这个宏:

\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}

其调用方式如下:

\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}

这是对每个级别的直接配置toc。代码中配置\Configure{NavSection}了 ,并根据需要关闭子级别。您可以NavSection.cfg文件中配置:

\usepackage{hiearchictoc}
\Preamble{xhtml}
\begin{document}
\Configure{NavSection}{\HCode{<li><span class="\spanclass">}}{\HCode{</span><ul>}}{ }{\HCode{</ul></li>}}
\EndPreamble

它有四个参数。第一个参数插入在toc条目的开头。注意\spanclass命令。当前类存储在这里,您可以使用一些条件来处理它。第二个参数插入在条目之后,如您所见,<span>元素在这里关闭,并且<ul>对于子条目插入。第三个参数并不有趣,它是插入在章节编号和节链接之间的代码,在我们的例子中是空格。最后一个参数在级别关闭时插入。

对于一些示例文件:

\documentclass{book}
\begin{document}
\tableofcontents
\chapter{Hello}
\section{world}
\subsection{hello, hello}
\subsection{hello, again}
\section{next section}
\end{document}

结果如下:

在此处输入图片描述

   <h2 class="likechapterHead"><a 
 id="x1-1000"></a>Contents</h2> <nav> 
<ul>     <li><span class="chapterToc">  1 <a 
href="#x1-20001" id="QQ2-1-2">Hello</a></span><ul>      <li><span class="sectionToc">  1.1 <a 
href="#x1-30001.1" id="QQ2-1-3">world</a></span><ul>      <li><span class="subsectionToc">  1.1.1 <a 
href="#x1-40001.1.1" id="QQ2-1-4">hello, hello</a></span><ul>  </ul></li>    <li><span class="subsectionToc">  1.1.2
<a 
href="#x1-50001.1.2" id="QQ2-1-5">hello, again</a></span><ul>  </ul></li></ul></li>    <li><span class="sectionToc">  1.2 <a 
href="#x1-60001.2" id="QQ2-1-6">next section</a></span><ul>
   </ul></li></ul></li></ul></nav> 

我不太明白你想如何决定将哪些类添加到单个元素,但如果你添加一些更详细的信息, <span>应该可以使用各种技巧spanclass

相关内容