1

I am trying to compile Latex code generated by Maple. The Latex generated contains things like \textit{\_C1} which works OK in pdf, but when compiled to HTML using tex4ht, in mathjax mode, it does not work, since mathjax does not support nesting inside this macro (reference)

MathJax does not process macros in text-mode, since it only handles math-mode macros. So within \text{} or \hbox{} or other text-mode material embedded within math mode, no macros are performed. That is a feature not a bug. :-)

I tried to use \renewcommand to redefine \textit{\_C1} to {\it \_C1} which works in HTML/mathjax, but not able to do it. The command is not changing in HTML, it still shows as \textit{\_C1}

Here is MWE

\documentclass[12pt]{article}
\usepackage{amsmath}

\ifdefined\HCode 
\renewcommand{\textit}[1]{{\it#1}}
\fi 

\begin{document}

\[
y(t) = \textit{\_C1}
\]

  
\[
y (t) = {\it \_C1}
\]
\end{document}

Compiled to pdf using lualatex, both give same output

enter image description here

But compiled to html

  make4ht -ulm default foo3.tex "mathjax,htm"

enter image description here

The HTML generated is

<!DOCTYPE html> 
<html lang="en-US" xml:lang="en-US" > 
<head><title></title> 
<meta  charset="utf-8" /> 
<meta name="generator" content="TeX4ht (https://tug.org/tex4ht/)" /> 
<meta name="viewport" content="width=device-width,initial-scale=1" /> 
<link rel="stylesheet" type="text/css" href="foo3.css" /> 
<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>  
</head><body 
>
<!--l. 12--><p class="noindent" >\[ y(t) = \textit{\_C1} \]
</p><!--l. 17--><p class="indent" >   \[ y (t) ={\it \_C1} \] </p> 
</body> 
</html>

How to correct my \renewcommand so that \textit{#} gets changed to {\it #} automatically to make mathjax happy? (I can't change Maple Latex code generation code) Or is there a better way to fix this other than using \renewcommand? May be need to add new configuration in tex4ht for this macro for mathjax?

Using TL 2020

3
1

Does your LaTeX generator produce \textit in math mode? I would say that it is an error. It should use \mathit instead. \it shouldn't be used at all, it is a deprecated command. Anyway, as your document is rendered by MathJax and it explicitly says that it doesn't support math content inside \textit, so there is nothing we could do.

This works in both pdflatex and MathJax:

\documentclass[12pt]{article}
\usepackage{amsmath}

\begin{document}


\[
y(t) = \mathit{\_C1}
\]

  
\[
y (t) = {\it \_C1}
\]
\end{document}

enter image description here

4
  • Thanks. Maple just generates Latex for an expression. i.e. given a math expression, it generates only the latex for this expression. Then I copy this latex to my latex document and put it inside \[ \]. So I would say most of the latex generated is meant to be used in math mode, but Mapleitself only generate the latex for the expression, and does not generate \[ ... \] or $...$ with it. – Nasser Sep 1 '20 at 8:10
  • But how can I change \textit{\_C1} automatically to \mathit{\_C1} ? When I tried \renewcommand{\textit}{\mathit} it did not work. it seems \renewcommand has no effect in mathjax mode? I'll try to ask Maplesoft to change \textit to \mathit but for now, I wonder if there is a workaround. – Nasser Sep 1 '20 at 8:13
  • Thanks Michal, got it working ok using the new macro from mathjax. I did not know about it. Thanks for the info about mathit. I do not think Maple will change their code to do that which would be the better solution otherwise. – Nasser Sep 1 '20 at 8:41
  • @Nasser yes, a macro definition for MathJax is probably only solution. – michal.h21 Sep 1 '20 at 8:44

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.