4

I have been successfully using the configuration described in the excellent answer to keeping equations as latex, so as to use MathJax. Now I wish to also use the exsheets package (for creating questions with solutions), which is compatible with tex4ht except that displayed equations don't work inside a question environment (provided one wishes to keep equations as latex, as I do).

Specifically, the tex file

\documentclass{article}

\usepackage[]{amsmath}
\usepackage{exsheets}
\usepackage[T1]{fontenc}

\begin{document}

This first question compiles fine:
\begin{question}
Here's an inline equation: \( a=0 \).  
\end{question}

This next question produces desired output, but gives error ``Missing \} inserted'':
\begin{question}
Here's a displayed equation (using brackets)
\[ x=1 \]
\end{question}

This last question gives errors ''Argument of verbatim has an extra \}'' and ''Paragraph ended before verbatim was complete'':
\begin{question}
Here's a displayed equation (using equation*)
\begin{equation}
  y=2
\end{equation}
\end{question}
\end{document}

with configuration file

\usepackage{verbatim}
\Preamble{html}

\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
%\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 
\Configure{@HEAD}{\HCode{
  <script type="text/x-mathjax-config">                                           
  MathJax.Hub.Config({
    TeX: {           
      extensions: ["color.js"], 
      equationNumbers: { autoNumber: "AMS" }         
    },        
    extensions: ["tex2jax.js"], 
    tex2jax: {       
      \unexpanded{
      inlineMath: [ ['\$','\$'], ["\\\(","\\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],}
      processEscapes: true,
      processEnvironments: true
    }                   
  });                  
  </script>\Hnewline
}}
\Configure{@HEAD}{\HCode{ <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> \Hnewline}}

\newtoks\eqtoks
\def\AltMathOne#1${\eqtoks{$#1$}% 
\HCode{\the\eqtoks}$}
\Configure{$}{}{}{\expandafter\AltMathOne} 
\def\AltlMath#1\){\eqtoks{\(#1\)}% 
\HCode{\the\eqtoks}}
\Configure{()}{\AltlMath}{}
\def\AltlDisplay#1\]{\eqtoks{\[#1\]}%
\HCode{\the\eqtoks}}
\Configure{[]}{\AltlDisplay}{}

\begin{document}

\newcommand\VerbMath[1]{%
  \renewenvironment{#1}{%
    \NoFonts%
    \string\begin\{#1\}%
  \verbatim}{\endverbatim\string\end\{#1\}\EndNoFonts}%
}

\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}

\EndPreamble

produces the output

enter image description here

Inside of the question environment, inline equations compile just fine, while displayed equations with brackets output ok but compile with an error, and displayed equations using equation do not produce the desired output.


Update: I realize now this issue is caused by the fact that verbatim material is not allowed inside exsheets environments; see this question. So I'm wondering if a command similar to VerbMath can be defined without using the verbatim package, i.e. a command that will keep the entirety of \begin{equation} EQUATION HERE \end{equation} unchanged in the html output, so as to be processed by MathJax.

3

I've slightly corrected the configurations, \detokenize instead of token register is used now. I've moved all configurations from the .cfg file to standalone package, which is now part of helpers4ht bundle. It is named mathjax-latex-4ht.sty:

\RequirePackage{verbatim}
\AtBeginDocument{%
  \Configure{VERSION}{}
  \Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
  \Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
  %\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 
  \Configure{@HEAD}{\HCode{
      <script type="text/x-mathjax-config">                                           
      MathJax.Hub.Config({
        TeX: {           
          extensions: ["color.js"], 
          equationNumbers: { autoNumber: "AMS" }         
        },        
        extensions: ["tex2jax.js"], 
        tex2jax: {       
          \unexpanded{
            inlineMath: [ ['\$','\$'], ["\\\(","\\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],}
          processEscapes: true,
          processEnvironments: true
        }                   
      });                  
      </script>\Hnewline
    }}
  \Configure{@HEAD}{\HCode{ <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> \Hnewline}}
  \newtoks\eqtoks
  \def\AltMathOne#1${\HCode{\detokenize{$#1$}}$}
  \Configure{$}{}{}{\expandafter\AltMathOne} 
  \def\AltlMath#1\){\HCode{\detokenize{\(#1\)}}\)}
  \Configure{()}{\AltlMath}{}
  \def\AltlDisplay#1\]{\HCode{\detokenize{\[#1\]}}\]}
  \Configure{[]}{\AltlDisplay}{}
}
% 
\newcommand\VerbMath[1]{%
  \renewenvironment{#1}{%
    \NoFonts%
    \string\begin\{#1\}%
      \verbatim}{\endverbatim\string\end\{#1\}\EndNoFonts}%
}

\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}

What changed are declarations of math scrapping commands:

  \def\AltMathOne#1${\HCode{\detokenize{$#1$}}$}
  \def\AltlMath#1\){\HCode{\detokenize{\(#1\)}}\)}
  \def\AltlDisplay#1\]{\HCode{\detokenize{\[#1\]}}\]}

You can simplify the .cfg file now:

\RequirePackage{mathjax-latex-4ht}
\Preamble{xhtml}
\begin{document}
\EndPreamble

You still need it in order to load the mathjax-latex-4ht package, but you don't need any custom configurations.

The issue with verbatim is more serious, easiest solution which I can think of unfortunately means to modify the document and use command which will output an environment when compiled with latex and verbatim code with tex4ht.

We need to provide a package for this command, verbtexmath.sty:

\ProvidesPackage{verbtexmath}
\newcommand\myverbenv[2]{%
  \begin{#1}%
    #2%
  \end{#1}%
}

\endinput

and tex4ht configuration, verbtexmath.4ht:

\renewcommand\myverbenv[2]{%
  \string\begin\{#1\}%
  \HCode{\Hnewline\detokenize{#2}\Hnewline}
  \string\end\{#1\}
}

As I said, you have to modify the document slightly:

\documentclass{article}

\usepackage[]{amsmath}
\usepackage{exsheets}
\usepackage[T1]{fontenc}
\usepackage{verbtexmath}

\begin{document}

This first question compiles fine:


\begin{question}
Here's an inline equation:\( a=0 \) 

and $b = 1$
\end{question}


Outside question \(a = 0\) and $b=1$ 

\[a=3\]

This next question produces desired output, but gives error ``Missing \} inserted'':

\begin{question}
Here's a displayed equation (using brackets)

\[c=4\]
\end{question}

This last question gives errors ''Argument of verbatim has an extra \}'' and ''Paragraph ended before verbatim was complete'':

\begin{question}
Here's a displayed equation (using equation*)

\myverbenv{equation}{y=2}
\end{question}
\end{document}

Important line is:

 \myverbenv{equation}{y=2}

this will create equation environment with y=2 content.

The resulting document:

enter image description here

  • This looks great, thank you very much. I may not get to test it in my context until next week. It seems like this solution would be easily adaptable to other equation environments, such as align, am I correct? – user59959 Sep 5 '15 at 13:19
  • @user59959 yes, you can use any environment name you need as a first parameter – michal.h21 Sep 5 '15 at 14:25
  • This works perfectly, and I'm perfectly content with modifying the document with the new command. – user59959 Sep 8 '15 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.