[Tex/LaTex] Converting LaTeX to HTML but keeping equations as LaTeX

htmlmathjaxtex4ht

I'm using tex4ht for conversion of heavily maths loaded LaTeX files into HTML to be able to serve them in a web app. Can successfully convert all equations to MathML and jsMath. But as equations are not web optimized, some of them are rendered wrongly or dont even get rendered as they are in original PDF's. So I decided to use maths as LaTeX as I think Mathjax can handle LaTeX equations better than jsMath or MathML.

Have already seen tex4ht leaving equations unchanged, tex4ht leaving equations unchanged – including \[…\] and \(…\) and TeX4HT Equation unchanged with newcommand or def command

I can manage to leave inline maths as they are but struggling with aligned standalone maths.

For example:

After a time $t$, the ground state $\ket{g}$ and the excited state $\ket{e}$ will each have accumulated a phase that is proportional to their energies:
\begin{align}
\ket{\psi(0)} \to \ket{\psi(t)} = \frac{e^{-iE_1 t/\hbar}}{\sqrt{2}} \ket{g} + \frac{e^{-iE_2 t/\hbar}}{\sqrt{2}} \ket{e}\, .
\end{align}
We can take out the factor $e^{-iE_1 t/\hbar}$ as a global unobservable phase, and obtain
\begin{align}\label{eq:atomequator}
\ket{\psi(t)} = \frac{1}{\sqrt{2}} \ket{g} + \frac{e^{-i(E_2 - E_1)t/\hbar}}{\sqrt{2}} \ket{e}\, .
\end{align}

So I can get tex4ht not to convert $\ket{g}$ in first line into image but having issues with maths within {align}.

I'm using michal-h21's .cfg file described in this answer: https://tex.stackexchange.com/a/165119/52068

any help would be greatly appreciated.

PS: I'm not a LaTeX expert and did not write the documents myself. So I somehow need to find a way to workout the LaTeX I have without modifying or with minimum amount of modification.

Best Answer

You can use config file from the answer you linked to, \VerbMath command is defined here. This command defines environment to be verbatim, so LaTeX macros inside are not processed. Just add \VerbMath{align} before \EndPreamble:

\usepackage{verbatim}
\Preamble{xhtml}
% Configure for mathjax
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
\Configure{@HEAD}{\HCode{
<script type="text/x-mathjax-config">                                           
  MathJax.Hub.Config({
    TeX: {           
      Macros: {     
        \unexpanded{ ket: ['{\\left|\#1\\right\\rangle}',1]}
      },
      equationNumbers: { autoNumber: "AMS" }         
    },        
    extensions: ["tex2jax.js"], 
    tex2jax: {       
        \unexpanded{
      inlineMath: [ ['\$','\$'], ["\\\\(","\\\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],}
      processEscapes: true
    }                   
  });                  
</script>   
}}
\Configure{@HEAD}{\HCode{<script type="text/javascript"\Hnewline
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"\Hnewline
></script>\Hnewline}}

\newtoks\eqtoks 
\def\AltMath#1${\eqtoks{$#1$}% 
   \HCode{\the\eqtoks}$}
\Configure{$}{}{}{\expandafter\AltMath}  
\def\AltlMathI#1\){\eqtoks{\(#1\)}% 
        \HCode{\the\eqtoks}}
\Configure{()}{\AltlMathI}{}
\def\AltlDisplay#1\]{\eqtoks{\[#1\]}%
        \HCode{\the\eqtoks}}
\Configure{[]}{\AltlDisplay}{}
\def\AltlDisplayI#1$${\eqtoks{$$#1$$}%
  \HCode{\the\eqtoks}$$}           
\Configure{$$}{}{}{\expandafter\AltlDisplayI}  
\begin{document} 
\newcommand\VerbMath[1]{%
\renewenvironment{#1}{%
%\ifvmode \IgnorePar\fi \EndP
\NoFonts%
\char`\\begin\{#1\}%
\verbatim}{\endverbatim\string\end\{#1\}\EndNoFonts}%
}

\VerbMath{equation*}
\VerbMath{align}
% add all used math environments here with \VerbMath
\EndPreamble

note that \keg command is unknown to mathajx so you will need to add some configuration to this section:

TeX: {           
      Macros: {     
        \unexpanded{ ket: ['{\\left|\#1\\right\\rangle}',1]}
      }

because you haven't provided complete example, I added some minimal preamble to the code you provided:

\documentclass{article}
\usepackage{amsmath}
\usepackage{braket}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{document}

After a time $t$, the ground state $\ket{g}$ and the excited state $\ket{e}$ will each have accumulated a phase that is proportional to their energies:

\begin{align}
                \ket{\psi(0)} \to \ket{\psi(t)} = \frac{e^{-iE_1 t/\hbar}}{\sqrt{2}} \ket{g} + \frac{e^{-iE_2 t/\hbar}}{\sqrt{2}} \ket{e}\, .
\end{align}

We can take out the factor $e^{-iE_1 t/\hbar}$ as a global unobservable phase, and obtain

\begin{align}\label{eq:atomequator}
                \ket{\psi(t)} = \frac{1}{\sqrt{2}} \ket{g} + \frac{e^{-i(E_2 - E_1)t/\hbar}}{\sqrt{2}} \ket{e}\, .
\end{align}

\end{document}

resulting web page:

enter image description here

Related Question