michal-h21 / make4ht

Build system for tex4ht
132 stars 15 forks source link

Math environnement in titles #52

Closed danielezambelli closed 2 years ago

danielezambelli commented 2 years ago

I get incorrect results when chapter or section titles contain mathematical formulas.

The toc_math.tex file contains:

`\documentclass[10pt,a4paper]{memoir} \usepackage[T1]{fontenc} \usepackage{textcomp}
\usepackage[utf8]{inputenc} \usepackage[italian]{babel}

\usepackage{amsmath} \usepackage{amssymb} \usepackage{tikz} \usepackage{lipsum}

\newcommand{\N}{\ensuremath{\mathbb{N}}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\round}[1]{\left( #1 \right)} \newcommand{\pair}[2]{\round{#1;~#2}}

\begin{document}

\tableofcontents

\chapter{Problem}

\section{pdflatex} If I compile with pdflatex everything works.

\section{mathml} If I compile in mathml, ``chapter{Natural (\mathbb{N})}'' give me this error:

\begin{verbatim} [STATUS] make4ht: Conversion started [STATUS] make4ht: Input file: toc_math.tex [WARNING] tocid: char-def module not found [WARNING] tocid: cannot fix section id's [ERROR] htlatex: Compilation errors in the htlatex run [ERROR] htlatex: Filename Line Message [ERROR] htlatex: ? ? Incomplete \iffalse; all text was ignored after line 70. [ERROR] htlatex: ? ? Emergency stop. [ERROR] htlatex: ? ? ==> Fatal error occurred, bad output DVI file produced! [FATAL] make4ht-lib: Fatal error. Command htlatex returned exit code 1 \end{verbatim}

Bat the titles with ``\$'' works.

\section{mathjax} If I compile in mathjax:

\begin{verbatim} make4ht toc_math.tex -c md_make4ht -l -u -s -d dist "mathjax" \end{verbatim}

In the table of contents it works correctly, but not in the section title, where I get:

\begin{verbatim} 4.1 Structure: \pair {\Q }{+} \end{verbatim}

In another work, the section title is correct while in the table of contents I get this \LaTeX expression:

\begin{verbatim} ``\PazoBB {Z}: \hskip 1em\relax \left ( \PazoBB {Z};~+ \right )'' \end{verbatim}

\chapter{Natural (\mathbb{N})} \lipsum[1-2]

\chapter{Integer $\mathbb{Z}$} \lipsum[3-4]

\chapter{Rational $\Q$}

\section{Structure: $\pair{\Q}{+}$} \lipsum[3-4]

\end{document} `

Thanks for your work.

danielezambelli commented 2 years ago

The config file for mathjax is:

config.zip

michal-h21 commented 2 years ago

TeX4ht needs that commands used in section titles (respective that goes to TOC), to be robust. You can either use \protect in front of them (like \chapter{Integer $\protect\mathbb{Z}$}), or declare them using \DeclareRobustCommand. I think the latter option is better, as it will work better with MathJax.

\DeclareRobustCommand{\N}{\ensuremath{\mathbb{N}}}
\DeclareRobustCommand{\Z}{\mathbb{Z}}
\DeclareRobustCommand{\Q}{\mathbb{Q}}
\DeclareRobustCommand{\round}[1]{\left( #1 \right)}
\DeclareRobustCommand{\pair}[2]{\round{#1;~#2}}

These definitions work with all TeX4ht modes.

I've also found some issues in your MathJax config, I thing this is the correct definition:

\Preamble{xhtml} 
\catcode`\#=11 
\Configure{MathJaxConfig}{{ 
  tex: { 
    tags: "ams", 
    \detokenize{% 
      macros: { 
        N: "\\mathbb{N}", 
        Z: "\\mathbb{Z}",
        Q: "\\mathbb{Q}",
        round: ["\\left(#1\\right)", 1],
        pair: ["\\round{#1;~#2}", 2],
      } 
    } 
  } 
}} 
\catcode`\#=6 
\begin{document} 
\EndPreamble
danielezambelli commented 2 years ago

Thanks!

With mathjax it works fine now.

Mathml recognizes \( and \) outside titles but not inside titles.

` \chapter{Natural $\mathbb{N}$} % <--- ok

(a^2 = b^2 + c^2) % <--- ok

[5^2 = 4^2 + 3^2] % <--- ok

\chapter{Integer (\mathbb{Z})} % <--- error! \lipsum[3-4] ` Is there any way to adjust this issue?

Thanks ()

michal-h21 commented 2 years ago

Ah, I see. It seems that this is an issue with \protect as well. You can either try \chapter{Integer \protect\(\mathbb{Z}\protect\)}, or the updated version of latex.4ht: latex4ht.zip. I will also update TeX4ht sources, so the fixed solution will be available in TeX Live soon.

danielezambelli commented 2 years ago

Thank you, it works fine!

michal-h21 commented 2 years ago

That's good to hear. I've updated TeX4ht sources, so the fix should be available soon. I hope I can close this issue now.