这是我的 LaTeX 文件foo.tex
:
\documentclass{article}
\usepackage{amsmath}
\title{Demo}
\begin{document}
\begin{align}
1 + 0 & = 1, \label{eq1} \\
1 + 1 & = 2, \label{eq2} \\
e^{i \pi} = -1. \label{eq3}
\end{align}
Equations \( \eqref{eq1} \), \( \eqref{eq2} \) and \( \eqref{eq3} \)
describe eternal truths.
The equation \( e^{i \pi} = -1 \) can also be rewritten as \( e^{i \pi}
+ 1 = 0 \) which is known as Euler's identity.
\end{document}
我将其转换为 HTML 如下:
$ make4ht foo mathjax
[STATUS] make4ht: Conversion started
[STATUS] make4ht: Input file: foo
[STATUS] make4ht: Conversion finished
输出如下foo.html
:
<!DOCTYPE html>
<html lang='en-US' xml:lang='en-US'>
<head><title></title>
<meta charset='utf-8' />
<meta name='generator' content='TeX4ht (https://tug.org/tex4ht/)' />
<meta name='viewport' content='width=device-width,initial-scale=1' />
<link href='foo.css' type='text/css' rel='stylesheet' />
<meta name='src' content='foo.tex' />
<script>window.MathJax = { tex: { tags: "ams", inlineMath: [ ["\\\(","\\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ], processEscapes: true, processEnvironments: true, packages: ['base', 'color', 'ams'] }, loader: { load: ['[tex]/color', '[tex]/ams'] } }; </script>
<script id='MathJax-script' async='async' type='text/javascript' src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js'></script>
</head><body>
<!-- l. 10 --><p class='noindent'>\begin{align} 1 + 0 & = 1, \label{eq1} \\ 1 + 1 & = 2, \label{eq2} \\ e^{i \pi } = -1. \label{eq3} \end{align}
</p><!-- l. 12 --><p class='noindent'>Equations \( \eqref{eq1} \), \( \eqref{eq2} \) and \( \eqref{eq3} \) describe eternal truths.
</p><!-- l. 15 --><p class='indent'> The equation \( e^{i \pi } = -1 \) can also be rewritten as \( e^{i \pi } + 1 = 0 \) which is known as Euler’s identity.
</p>
</body>
</html>
到目前为止一切顺利。但是,我想要使用自己的 HTML 模板来决定页眉和页脚。我只希望由 生成主要内容make4ht
。
这是我的模板template.html
。
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset='utf-8' />
<meta name='viewport' content='width=device-width,initial-scale=1' />
<style>
body {background: gray}
main {background: white; max-width: 40em; margin: 0 auto; padding: 1em}
</style>
<script>window.MathJax = {tex: {tags: "ams"}}</script>
<script src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js'></script>
</head>
<body>
<main>
<!-- HTML converted by make4ht goes here -->
</main>
</body>
</html>
有没有办法要求make4ht
使用这个模板而不是使用它自己的模板来生成输出 HTML?
答案1
.log
最简单的方法是使用 TeX4ht 提供的可配置钩子来进行基本的 HTML 配置。以下是使用info
选项时放入文件中的基本信息make4ht
:
\Configure{DOCTYPE}.........1
\Configure{HTML}............2
\Configure{HEAD}............2
\Configure{@HEAD}...........1
\Configure{BODY}............2
\Configure{TITLE+}..........1
\Configure{TITLE}...........2
\Configure{@TITLE}..........1
\Configure{Preamble}........2
<DOCTYPE>
<HTML 1>
<HEAD 1>
<TITLE 1>
<@TITLE>
<TITLE+>
<TITLE 2>
<@HEAD>
<HEAD 2>
<BODY 1>
......
<BODY 2>
<HTML 2>
The \Configure{@HEAD}{...} command is additive, concatenating the
content of all of its appearances. An empty parameter requests
the cancellation of the earlier contributions.
For instance,
\Configure{@HEAD}{A}
\Configure{@HEAD}{}
\Configure{@HEAD}{B}
\Configure{@HEAD}{C}
contributes `BC'.
The \Configure{TITLE+} provides the content for the title,
\Configure{TITLE} sets the envelop, and \Configure{@TITLE} acts as a
hook for introducing localized configurations. As is the case for
\Configure{@HEAD}, the contribution of \Configure{@TITLE} is also
additive.
These configurations should be introduced early enough in the
compilation. For instance, in the case of LaTeX, between \Preamble
and \begin{document} of a local configuration file.
\Preamble
%%% here %%%
\begin{document}
...
\EndPreamble
\Configure{@BODY}...........1
\Configure{@/BODY}..........1
Variants of \Configure{@HEAD} which contribute their content,
respectively, after <body> and before </body>.
因此,您将需要使用\Configure{HTML}
从元素中删除属性<html>
、\Configure{TITLE+}
设置自己的标题、\Configure{@HEAD}
将大部分内容插入<head>
元素以及\Configure{@BODY}
插入\Configure{@/BODY}
元素<main>
。
以下是一个完整的.cfg
文件,展示了如何做到这一点:
\Preamble{xhtml}
\Configure{@HEAD}{} % remove default content in HTML header
\Configure{@HEAD}{%
\HCode{<meta charset='utf-8' /> \Hnewline
<meta name='viewport' content='width=device-width,initial-scale=1' /> \Hnewline
<style>\Hnewline
body {background: gray}\Hnewline
main {background: white; max-width: 40em; margin: 0 auto; padding: 1em}\Hnewline
</style>\Hnewline
<script>window.MathJax = {tex: {tags: "ams"}}</script> \Hnewline
<script src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js'></script> \Hnewline
}}
\Configure{TITLE+}{test}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
\Configure{@BODY}{\ifvmode\IgnorePar\fi\EndP\HCode{<main>\Hnewline}}
\Configure{@/BODY}{\ifvmode\IgnorePar\fi\EndP\HCode{\Hnewline</main>}}
\begin{document}
\EndPreamble
第一次使用\Configure{@HEAD}{}
只是删除了 TeX4ht 插入到标题中的常见内容。我们在第二次调用该配置时插入所有标签。请注意宏的使用\Hnewline
,它用于在生成的 HTML 文件中插入换行符。
因为\Configure{@BODY}
和\Configure{@/BODY}
是在文档文本已经排版的情况下使用的,所以我们需要处理段落。\ifvmode\IgnorePar\fi\EndP
用于避免标签不匹配。
以下是生成的 HTML
<!DOCTYPE html>
<html>
<head> <title>test</title>
<meta charset='utf-8' />
<meta content='width=device-width,initial-scale=1' name='viewport' />
<style>
body {background: gray}
main {background: white; max-width: 40em; margin: 0 auto; padding: 1em}
</style>
<script>window.MathJax = {tex: {tags: "ams"}}</script>
<script src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js'></script>
</head><body>
<main>
<!-- l. 11 --><p class='indent'> \begin{align} 1 + 0 & = 1, \label{eq1} \\ 1 + 1 & = 2, \label{eq2} \\ e^{i \pi } = -1. \label{eq3} \end{align}
</p><!-- l. 13 --><p class='indent'> Equations \( \eqref{eq1} \), \( \eqref{eq2} \) and \( \eqref{eq3} \) describe eternal truths.
</p><!-- l. 16 --><p class='indent'> The equation \( e^{i \pi } = -1 \) can also be rewritten as \( e^{i \pi } + 1 = 0 \) which is known as Euler’s identity.
</p>
</main>
</body>
</html>
它看起来是这样的: