# tex4ht does not handle \ifdefined\HCode in mathjax mode

This is issue with tex4ht only.

This is the first time I found where tex4ht does not process \ifdefined\HCode.

I put \ifdefined\HCode inside an equation, since I need different code for HTML vs. pdf mode.

tex4ht does not process the ifdefined and leave it there. When mathjax sees it, it complains since mathjax offcourse does not know anything about ifdefined\HCode.

When the code is compiled not using mathjax it works OK. But I need to use mathjax.

Here is MWE

\documentclass[12pt]{article}
\usepackage{array}
\begin{document}
$\ifdefined\HCode \left[ \begin{array}{ccc} \else \left[ \begin{array}{@{}cc|c@{}} \fi 3 i&-3 &0\\ 3 &3 i &0 \end{array} \right]$
test
\end{document}

The above compiled OK using lualatex foo3.tex and gives

But compiling using make4ht -ulm default -a debug foo3.tex "mathjax,htm" it gives

This is the raw HTML. it shows the \ifdefined\HCode was not processed and left in there

<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US" >
<meta  charset="utf-8" />
<meta name="generator" content="TeX4ht (https://tug.org/tex4ht/)" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="src" content="foo3.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 type="text/javascript" async="async" id="MathJax-script" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js"></script>
>
<!--l. 23--><p class="noindent" >$\ifdefined \HCode \left [ \begin{array}{ccc} \else \left [ \begin{array}{@{}cc|c@{}} \fi 3 i&amp;-3 &amp;0\\ 3 &amp;3 i &amp;0 \end{array} \right ]$
</p><!--l. 25--><p class="indent" >   test
</p>

</body>
</html>

Compiled using make4ht -ulm default -a debug foo3.tex "htm" works with no problem and gives

The problems seems that tex4ht does not look inside the equation to see that there is something to do, and just passes the whole thing to mathjax.

## UPDATE

I found workaround. Which is to put \ifdefined\HCode outside everything, and duplicate the whole code twice, including the array code. I was hoping to avoid this. But this works for now. For some reason, putting \ifdefined\HCode inside any equation does not work with tex4ht when in mathjax mode.

\documentclass[12pt]{article}
\usepackage{array}
\begin{document}
\ifdefined\HCode %tex4ht version
$\left[ \begin{array}{ccc} 3 i&-3&0\\ 0 &0 &0 \end{array} \right]$
$\left[ \begin{array}{@{}cc|c@{}} 3 i&-3&0\\ 0 &0 &0 \end{array} \right]$
\fi

\end{document}

TL 2020

TeX4ht of course cannot interpret your code inside math environments, because they are passed to HTML code verbatim. They are interpreted by MathJax when you display the HTML code and MathJax of course doesn't know anything about \ifdefined\HCode, so it ignores it. You either need to duplicate your code and let LaTeX interpret your code, or you can try to use a custom environment and pass an alternative definition to MathJax. Which may or may not work.

For example, this seems to work:

\documentclass[12pt]{article}
\usepackage{array}
\newcommand\mathjaxalt[2]{#2}
\begin{document}
$\mathjaxalt{% \left[ \begin{array}{ccc} }{% \left[ \begin{array}{@{}cc|c@{}} }% 3 i&-3 &0\\ 3 &3 i &0 \end{array} \right]$
test
\end{document}

I've defined the \mathjaxalt macro, which takes two parameters, first contains code for MathJax, second contains code for regular LaTeX. You need to provide a definition of this macro for MathJax in a config file:

\Preamble{xhtml}
\Configure{MathJaxConfig}{{
tex: {
tags: "ams",
\detokenize{%
inlineMath: [ ["\\$$","\\$$"] ],
displayMath: [ ['$$','$$'], ["\$","\$"] ]},
processEscapes: true,
processEnvironments: true,
macros: {
mathjaxalt: ["\#1",2],
}
},
}
};
}
\begin{document}
\EndPreamble

This is the definition:

macros: {
mathjaxalt: ["\#1",2],
}

It uses the first parameter, as opposed to the original definition, which uses the second parameter.

This is how it is rendered in HTML: