1

I use cprotect package so I can use math in section titles. It works OK in pdf, but tex4ht generates strange characters at end of section title.

If I do not use cprotect, then it will not compile in tex4ht.

Is there a way around this? MWE

\documentclass[12pt,notitlepage]{book}
\usepackage{cprotect}
\usepackage{amsmath}
\begin{document}
\title{my book}
\chapter{Listing of integrals}
\cprotect\section{\quad $\int e^x \left(1-x^3+x^4-x^5+x^6\right) \, dx$}

stuff

\cprotect\section{\quad $\int \frac{e^{\frac{x}{2+x^2}} \left(2-x^2\right)}{2 x+x^3} \, dx$}
text
\end{document}

Compiled using

 make4ht -ulm default -a debug t.tex "htm,3,mathjax,notoc*,p-width"

gives

enter image description here

If I do not use \cprotect then it gives compile error.

(/usr/local/texlive/2019/texmf-dist/tex/generic/tex4ht/html5.4ht))
(./report.aux) (/usr/local/texlive/2019/texmf-dist/tex/latex/base/ts1cmr.fd)
[1] [2]
Chapter 1.
(./report.4ct (./report-1.cpt) (./report-2.cpt))
(/usr/local/texlive/2019/texmf-dist/tex/latex/lm/ot1lmtt.fd)
! Argument of \im:g has an extra }.
<inserted text>
\par
l.9 ...nt e^x \left(1-x^3+x^4-x^5+x^6\right) \, dx$}

?

Important Please note that the math itself used in section titles in auto-generated by computer algebra system and I did not write the math. A large program runs which read these math equations and use each for section titles. So editing the math is not possible or practical.

TL 2019

9
  • Unrelated there no need for left/right in that header
    – daleif
    Apr 4 '20 at 17:50
  • the math markup seems sub-optimal but you can remove the cprotect package and your example runs without error in pdflatex, so it doesn't seem to be doing anything in recent latex releases. So I would simplify the markup not to use that and then look to get tex4ht to understand if that fails Apr 4 '20 at 18:08
  • @DavidCarlisle sorry, I am not following you when you say I can remove the cprotect package? tex4ht will give error in that case as I mentioned above. The issue is with tex4ht and not compiling to pdf. Yes, I need a workaround to use math in section titles with tex4ht. I now only know about the cprotect package.
    – Nasser
    Apr 4 '20 at 18:11
  • remove \cprotect \left and \right, then it seems to work OK, and be more correct markup. Apr 4 '20 at 18:14
  • @DavidCarlisle the math itself is autogenerated by CAS systems I am afraid. I did not write the math. A program is run which read the math generated by Maple and Mathematica and uses this math to generate section titles. So I can't edit the math itself. I will add this to the question.
    – Nasser
    Apr 4 '20 at 18:15
1

I think your original problem is the same as here: https://tug.org/pipermail/tex4ht/2018q4/002114.html

My solution for this was to create \fixmathjaxtoc command, which prevents commands at the moment when they are written to TOC from expansion. It seems this command don't work for \left and \right, because they fail at the moment they are used in the section. As a workaround, you can disable their expansion completely. As math is handled by MathJax, it doesn't cause any harm:

\Preamble{xhtml}

\def\right{\detokenize{\right}}
\def\left{\detokenize{\left}}
\begin{document}
\EndPreamble

enter image description here

3
  • Thanks. But I needed to use cprotect somewhere else for verbatim in some titles and section titles and so on. After much debugging, I found just loading the cprotect package causes these ^^F^^LL to show up in section header, even if \left and \right are not there. So I ended up just removing cprotect package when using tex4ht only and removed the verbatim stuff from section headers. Now it works. all ^^F are gone. So there is a problem with using cprotect period with tex4ht. So avoiding it all together with tex4ht seems the only way for now.
    – Nasser
    Apr 4 '20 at 20:35
  • @Nasser yes, it seems to be catcode conflicts. I've looked at cprotect sources and couldn't understand what's going on, so it is unlikely that I will be able to fix that. Anyway, I think we already tried to fix verbatims in sections in the past, try to search in your mail :) There should be a solution that doesn't need cprotect
    – michal.h21
    Apr 4 '20 at 21:00
  • No problem, I am trying to change my code to avoid verbatim in titles so I do not have to use cprotect in tex4ht mode. I still need to use it for latex, but I do not load it in tex4ht mode any more. Thanks for looking into this.
    – Nasser
    Apr 4 '20 at 21:03
1

If you correct the markup, removing \left\right then it works in htlatex (cprotect seems unnecessary here in htlatex or pdflatex)

I get html rendering of

enter image description here

\documentclass[12pt,notitlepage]{book}

\usepackage{amsmath}
\begin{document}
\title{my book}
\tableofcontents
\chapter{Listing of integrals}
\section{\quad $\int e^x (1-x^3+x^4-x^5+x^6) \, dx$}

stuff

\section{\quad $\int \frac{e^{\frac{x}{2+x^2}} (2-x^2)}{2 x+x^3} \, dx$}
text
\end{document}
2
  • Thanks for the suggestion. Will add code in my program to remove the \left and \right in math just for section\subsection headers as a workaround until this bug is fixed.
    – Nasser
    Apr 4 '20 at 18:24
  • hi David, I found a conflict with this. i.e. when I followed what you suggested and removed the \eft and \right from math for section header, I found an error in different area due to other issues (when using different font and hyperref). Will post separate question on that now.
    – Nasser
    Apr 4 '20 at 19:15

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.