
これが私の 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
最も簡単な方法は、TeX4ht が基本的な HTML 設定用に提供する設定可能なフックを使用することです。オプションを.log
使用する場合にファイルに入力される基本情報は次のとおりです。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 がヘッダーに挿入する通常の内容が削除されるだけです。その構成の 2 回目の呼び出しで、すべてのタグが挿入されます。マクロの使用に注意してください。\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>
そして、それは次のようになります: