[Tex/LaTex] LaTeX to HTML via Pandoc : handle a custom command

pandoc

I am using Pandoc to convert a .tex document to html/epub. In my text I make frequent use of a custom command,
\subchpbreak with the following definition:

\newcommand{\subchpbreak}{\fancybreak{\rotatebox[origin=c]{90}{\S}}}

I am wondering if there is an easy way to have the custom command (i.e. the string \subchpbreak be interpreted by the converted or regex replaced by something I can style with css, e.g.

<div class='subchpbreak'>&167;</div>

I know that I can do string replacement in my tex file, but I would rather not.

Maybe what I should do is write a bash script to do the string replacement and then run Pandoc? Or is there a better way?


A solution from the comments

In the comments, it was suggested that I use htlatex instead of pandoc. This does solve the custom command issue nicely. However, I am having an issue with “ and '' being lost.

The culprit seems to be \usepackage{paratype}. The below example works as expected without the font package, but ignores the quotes with it.

\documentclass[12pt, oneside]{memoir}
\usepackage[utf8]{ inputenc }
\usepackage{ graphicx }
\usepackage{lipsum}
% \usepackage{paratype}

\usepackage{xcolor,fix-cm}
\definecolor{numbercolor}{gray}{0.7}

\newcommand{\subchpbreak}{\fancybreak{\rotatebox[origin=c]{90}{\S}}}

\begin{document}

    \chapter{A chapter}
    \lipsum[1]

    ``Alas, this is a quote,'' said the idiot.

    \lipsum[2]    

    \subchpbreak

    \lipsum[3]

\end{document}

Thanks for any response!

Best Answer

You can easily support custom commands with tex4ht. Just move declaration of the commands to standalone package, for example mycommands.sty:

\newcommand{\subchpbreak}{\fancybreak{\rotatebox[origin=c]{90}{\S}}}

and create file with same name, but .4ht extension, mycommands.4ht:

\NewConfigure{subchpbreak}{1}
\renewcommand\subchpbreak{\a:subchpbreak}
\Configure{subchpbreak}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class='subchpbreak'>&\#167;</div>}}

\Css{.subchpbreak{font-size:1.2em;}}

To allow configurability, we need to declare some hooks in which will be inserted html code. These hooks are inserted to redefined commands. In this case, we declared new configuration, subchpbreak with one hook, because your command takes no arguments. For commands one parameter, you would need two hooks, to be inserted before and after the parameter, for two parameters you would need three hooks and so on.

The hook is then inserted to redefined command, the hooks are named as \a:hookname, b:hookname, etc. In our case we use only one hook, so it is just \a:subchpbreak.

The hook is configured with

\Configure{subchpbreak}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class='subchpbreak'>&\#167;</div>}}

we need to use

\ifvmode\IgnorePar\fi\EndP

because <p> elements are inserted in each paragraph, but we don't want them around our <div>

You can add stuff to the .css file with the \Css command.

The resulting html:

<!--l. 15--><p class="noindent" >&#8220;Alas, this is a quote,&#8221; said the idiot.
</p>
...
</p>
   <div class='subchpbreak'>§</div>
<!--l. 21--><p class="indent" > 

enter image description here

Regarding your problem with fonts, you should use \usepackage[T1]{fontenc} for best unicode support. When you use paratype fonts, you may see following message in the terminal output:

--- warning --- Couldn't find font `PTSerif-Regular-tlf-t1.htf' 

for each font, .htf file must exist. I recently provided a solution for Droid sans, in your case you may create following PTSerif-Regular-tlf-t1.htf file:

.lm-ec
htfcss: PTSerif-Regular-tlf-t1 font-family: 'PT Serif', serif;

see the linked answer about Droid Sans for details. You may need to create similar files also for italic and bold fonts, if you use them.

Related Question