2

Compiling this simple example with mk4ht oolatex (which I then headlessly convert to a docx file), I find myself a little confused by the result I get. Note, everything is as expected with the pdf file compiled with xelatex.

\documentclass{article}

\newcommand{\KDP}{$KH_2 \cdot PO_4$}
%\newcommand{\KDP}{\expandafter{$KH_2 \cdot PO_4$}}
%\protected\def\KDP{\expandafter$KH_2 \cdot PO_4$}

\begin{document}
    $KH_2 \cdot PO_4$

    \KDP
\end{document}

The pure math mode expression type sets just fine, and even shows up in equation mode in MS word. But if I nest that expression inside a new command, it looses it's subscript formatting (the equation still shows up, it just lost the sub script instruction).

This MWE is what I ended up with after I tried some chemical formula typesetting packages as well, but \ce{} and \ch{} didn't work either. Text based subscripts weren't working either, but I'm not exactly sure whether to even assume these share the same underlying issue, so I'll start with the basics, with nice simple math mode.

I tried to adjust some expansions or protections, mimicked from some other answers around, but my understanding of this particular concept is quite poor - I wasn't expecting them to really work.

I'm inferring that my problem is that the _ isn't robustly preserved and expanded at the proper time given the extra layer of interpretation / expansion that the new command insertion might be requiring.

3

It is better to use \sp instead of ^ and \sb instead of _ in custom commands (basically everything defined in the preamble and packages). This is noted in the tex4ht MathML troubleshooting page:

Use of the operators ‘^’ and ‘’, instead of ‘\sb’ and ‘\sp’, outside the presence of TeX4ht. (TeX4ht becomes active only at the ‘\begin{document}’ command. The ‘early’ and ‘early^’ options extend this awareness to the preambles of the source latex documents.)

Your command can be rewritten in this way:

\documentclass{article}

\newcommand{\KDP}{\ensuremath{KH\sb{2} \cdot PO\sb{4}}}

\begin{document}
    $KH_2 \cdot PO_4$

    \KDP
\end{document}

This produces the expected formatting:

enter image description here

  • Thanks, this works great, and thanks for the link I'll read more and try out those command line compile options. I don't think I really understand what "outside the presence of TeX4ht" means, but I guess it must have something to do with how the macro is interpreted ahead of the final compile. – EngBIRD Mar 21 '18 at 21:56
  • @EngBIRD I think that it means that all 4ht files are processed at \begin{document}, so it cannot recognize subscripts and superscripts used before that. – michal.h21 Mar 21 '18 at 22: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.