14

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.

1 Answers1

11

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

michal.h21
  • 50,697
  • thanks for your answer @michal.h21 I did try \VerbMath{align} and it wasnt working. Now I realised \usepackage[T1]{fontenc} was missing in preamble. Thanks {align} works perfectly now. Also you are right I need to define custom macros in MathJax, thanks for /ket example. – tarikakyol Jun 23 '14 at 11:23
  • I got another issue with double dolar signs. In some LaTeX files I have author used $$ instead {align} so I have equations like:
    $$
    \IP{\phi}{\psi}=\int \! \ud x \, \phi^*(x)\psi(x).
    $$
    
    

    along with $(q(t), p(t))$ inline equations in same LaTeX. I've tried to add \def\AltMath2#1${\eqtoks{$$#1$$}% \HCode{\the\eqtoks}$} \Configure{$$}{}{}{\expandafter\AltMath2} into configuration file but no luck. Thanks in advance for your help

    – tarikakyol Jun 23 '14 at 11:27
  • @trkaky I've added configuration for $$ – michal.h21 Jun 23 '14 at 12:58
  • thanks @michal.h21 getting "! File ended while scanning definition of \AltlDisplayI" error with your cfg – tarikakyol Jun 23 '14 at 14:01
  • got it working, there is a typo (an extra %) in your answer that needs to be fixed as: \HCode{\the\eqtoks}$$} – tarikakyol Jun 23 '14 at 15:07
  • @trkaky thanks for noticing this, I updated the answer – michal.h21 Jun 23 '14 at 15:36
  • got another issue with equations, it's giving "TeX capacity exceeded, sorry [input stack size=30000]" error when an $equation$ is in caption of the graphics. what would you reckon about it? many thanks – tarikakyol Jul 09 '14 at 12:22
  • @trkaky could you post it as separate question with sample code? I tried to add \caption command with equation inside figure and it compiled correctly – michal.h21 Jul 09 '14 at 12:50
  • here is the new question http://tex.stackexchange.com/q/188709/52068 thanks – tarikakyol Jul 09 '14 at 13:07
  • I just figured out equation references got lost. For example: (13) in this equation http://i.imgur.com/TMM4nFD.png Is there anyway I can do to put them back? thanks – tarikakyol Jul 15 '14 at 07:41
  • this is one of the original tex files: http://pastie.org/9392445 and HTML output: http://codepen.io/anon/pen/LCxHG on line where reads "Equations ?? and ?? are the same" question marks should have been equation numbers as well. – tarikakyol Jul 15 '14 at 09:02
  • @trkaky equation numbering must be configured in mathjax, I've edited the config file to incorporate this. see http://docs.mathjax.org/en/latest/tex.html#automatic-equation-numbering for more information – michal.h21 Jul 15 '14 at 10:16
  • thanks for your response, equation numbering now appears with the setting but issue is at the bottom, while converting LaTeX to HTML. Because HTML output of "/ref" is "??". So LaTeX "Equations \ref{eq:hamg} and \ref{eq:hame}" outputs as "Equations ?? and ??" after convertion. – tarikakyol Jul 15 '14 at 10:34
  • think I need to add "$" to every single "/ref". So MathJax can read it as LaTeX "$/ref$". Its going to be a mission there are thousands. – tarikakyol Jul 15 '14 at 10:52
  • @trkaky do you always use \ref{eq:labelname} for referencing equations? if yes, it should be doable without breaking support for other types of references (ie. for sections, which aren't supported by mathjax. maybe post this as new question – michal.h21 Jul 15 '14 at 11:05
  • written a regex to replace them all, re-converting now.. thanks for your effort Michal – tarikakyol Jul 15 '14 at 13:08
  • I think there may be a mistake here with nesting of braces in the MathJax.Hub.Config({ block. Should the brace pair following TeX: encase the extensions as well? (but not tex2jax?) – alex.jordan Oct 04 '14 at 19:57
  • @alex.jordan I am not an expert on mathjax but I think it shouldn't enclose extensions – michal.h21 Oct 04 '14 at 20:33
  • @michal.h21 I'm still working on my issues with the document you helped with earlier (and thanks!). Not everything is working yet. But until I grouped extensions inside the Tex block, a lot more wasn't working. I think that this indicates it should be encased. – alex.jordan Oct 05 '14 at 01:43
  • @alex.jordan it seems there are two different types of extensions. in my code sample it is used to load input processing extension tex2jax. extensions used inside of TeX block will load extensions for LaTeX macro support – michal.h21 Oct 05 '14 at 08:51
  • Oh, this could help with my lingering issues. Thank you for all your help! – alex.jordan Oct 05 '14 at 16:21
  • I would like to know if there's an alternate way of defining VerbMath, or something like it, without using the verbatim package. (I'm using exsheets, which doesn't allow verbatim text in its "question" environment; see http://tex.stackexchange.com/questions/265815/converting-latex-to-html-using-exsheets-and-keeping-equations-as-latex ) – user59959 Sep 05 '15 at 01:16