Skip to content

Instantly share code, notes, and snippets.

@stefanozanella
Last active March 7, 2023 22:10
Show Gist options
  • Save stefanozanella/8892211 to your computer and use it in GitHub Desktop.
Save stefanozanella/8892211 to your computer and use it in GitHub Desktop.
Custom tex4ht (htlatex) configuration, so that it generates HTML5 code with MathJax rendering
% We are generating HTML + MathML code
\Preamble{xhtml,mathml}
% We don't want to translate font suggestions with ugly wrappers like
% <span class="cmti-10"> for italic text
\NoFonts
% Don't output xml version tag
\Configure{VERSION}{}
% Output HTML5 doctype instead of the default for HTML4
\Configure{DOCTYPE}{\HCode{<!doctype html>\Hnewline}}
% Custom page opening
\Configure{HTML}{\HCode{<html lang="en">\Hnewline}}{\HCode{\Hnewline</html>}}
% Set custom page title
\Configure{TITLE+}{Network Security Documentation}
% Reset <head>, aka delete all default boilerplate
\Configure{@HEAD}{}
% Setup custom <head> content
\Configure{@HEAD}{\HCode{<meta charset="UTF-8" />\Hnewline}}
\Configure{@HEAD}{\HCode{<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/\string~gurari/TeX4ht/)" />\Hnewline}}
% Setup MathJax
\Configure{@HEAD}{\HCode{<script type="text/javascript"\Hnewline src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"\Hnewline></script>\Hnewline}}
% Tweak default style a little bit
\Configure{@HEAD}{\HCode{<style type="text/css">\Hnewline .MathJax_MathML {text-indent: 0;}\Hnewline</style>\Hnewline}}
% Translate \textbf, \textit and \texttt directives into <b>, <em> and <code>
\Configure{textbf}{\ifvmode\ShowPar\fi\HCode{<b>}}{\HCode{</b>}}
\Configure{textit}{\ifvmode\ShowPar\fi\HCode{<i>}}{\HCode{</i>}}
\Configure{texttt}{\ifvmode\ShowPar\fi\HCode{<code>}}{\HCode{</code>}}
% Translate verbatim and lstlisting blocks into <pre> elements
\ConfigureEnv{verbatim}{\HCode{<pre>}}{\HCode{</pre>}}{}{}
\ConfigureEnv{lstlisting}{\HCode{<pre>}}{\HCode{</pre>}}{}{}
% Do not set `indent`/`noindent` classes on paragraphs
\Configure{HtmlPar}
{\EndP\Tg<p>}
{\EndP\Tg<p>}
{\HCode{</p>\Hnewline}}
{\HCode{</p>\Hnewline}}
\begin{document}
\EndPreamble
# Add options to handle UTF-8 charset and to ignore CSS code
htlatex file.tex "html5mathjax,charset=utf-8,-css" " -chunihft -utf8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment