# is there a minimal converter of well-defined latex to xhtml?

is there a good clean minimal converter of well-defined latex code into html (preferably perl)?

I have played with htlatex and latex2html, but they are too ambitious and too complex in their goals and insufficient in their results. they and their output are hard to modify and to clean up or process further. I need something simpler. I even looked at writing my documents in md, primarily because markdown can generate both latex and xhtml. but my main motivation is to be able to create new clean documents. going to markdown for my own purposes loses a lot of latex features without gaining me the xhtml features I want.

now, when I looked at some of my old sample documents, I realized that most of what I want a converter to do is to understand simple standard latex documents. I do not need it to understand user-defined macros. I need the quasi-parser to create valid xhtml and to facilitate eventually conversion to epub.

my guess is that this has been written many times, so if someone knows of a (perl-) parser that does this, please let me know. I thought a little about my list of needs:

• intelligence about comments (%), escapes (\%, $, \\), etc., and paragraph starts. it should be reasonably smart, but does not have to be perfectly protected against clever bad latex input. (what does \%\\%$%\% mean? the program can just complain.)

• knowledge of special characters for conversions into html entities (e.g., \"a, \gamma, \cdot)

• font shapes, sizes, weights, colors, underline, overline, strikethrough

• knowledge of direct equivalence environments (e.g., \begin{quote} and <quote>, or \section and h1)

• environments (such as \begin{table} ... \end{table} to <div class="table", or theorem to div class=theorem.)

• tabulars to table, graphicx to img

• tabular intelligence (e.g., & to <td>, \\ to <tr>, multicolumn to rowspan, etc); (if anyone has a standalone perl .pm program to do this intelligently standalone, please let me know.)

• enumerate/itemize/description lists

• passing math into mathjax (or better yet creating the rescaleable html+css-mathjax final output)

• hrefs, both internal and external

• (maybe some spacing, bigskip, medskip, smallskip, \;)

• some pragma features to direct the converter to include bits of raw html, or to ignore parts of the latex.

• some features to decide on unrecognized items. for example, this could be to put these into special divs with classes or into comments and css stylability, with options for complaints, and/or printing rather than in comments. the main idea is that one can then write another unix filter program to hook into the output of this program and do further processing.

the tasks are compact and definable.

is there a good starter perl program for this? my problem is that I have too many other tasks, so I start writing a program and then neglect it for so long that I barely remember it, and then the program suffers from bitrot. keeping it standalone and with this level of understanding as its own command-line program should mean that such a converter is more likely to remain maintainable. tabular to table conversion seems to be the most complex part. many of the above tasks seem to require primarily clever regex's, and not much more.

(PS: for me, it could also serve as a lint-checker for what I deem to be good latex documents, aka usepackage{nag}. if a converter can produce nice html, pandoc can then convert to other formats, too.)

sincerely,

iaw

-
I think it is much simpler to configure and postprocess output of tex4ht, than to create another converter, even for limited LaTeX syntax –  michal.h21 Jan 20 at 8:06
Try pandoc. It can convert between LaTeX and HTML. –  Martin Schröder Jan 20 at 14:47
(pandoc dies on my latex documents.) tex4ht (htlatex) is very ambitious, but there are no good configuration tutorials I could find. can one configure it to pass math to mathjax and leave it inline, instead of generating png files; and can one configure it to produce one and only one output file, rather than many (footnotes, png, etc.) –  ivo Welch Jan 20 at 17:37

I know this has probably been written many times and many times better than what I can write in my two days with modest perl background here, but because I could not find a good starter that is readable and does the basic job, my attempt is at

http://ivo-welch.info/computers/iawltx2html/

I would have posted it here, except that it is too long at 400 perl lines (plus a long entities data base, plus a documentation sample). At 400 lines, the perl program should be reasonably maintainable. It could also be better refactored.

Think of it as a good starter and proof-of-concept. The perl program is not attempting to produce documents that match the latex look. instead, it is trying to convert reasonable subset-latex documents to html documents, to be styled with css and mathjax later. It informs the user of what tags have not been converted (and have been passed through for subsequent processing) and what should be styled.

Alas, I am having problems with tabular's, especially \hline (or \toprule, etc.), as well as with formatting columns. I tried

 <table style="tr td:nth-child(2) { text-align:right }">


and about 20 variations thereon including various versions of table, tr, td, but nothing seemed to work.

A minor nuisance is that Regexp::Common balanced matches include their parentheses, so I have to strip them. It also cannot handle two consecutive arguments, in which case I fall back on {.*?} for matching instead. This is really primarily an issue for commands with optional arguments. This isn't handled well, either.

Another nuisance is that <li> and <p> remain unbalanced. thus, they need to be cleaned up by tidy or pandoc if xhtml is needed. Someone who is more clever than I and who has more knowledge of html (e.g., what other tag can end a paragraph tag?) could probably fix this up fairly easily.

I would love to fix this perl program up further to converge to something useful. For me, this quasi-parser will also help me keep my latex files reasonably sane. I will know what passes and is convertble/recognizable and what is not. It's an itch I have had for a long time.

It's pretty useful already, though.

-
The link gives 404 error... –  Rmano Mar 15 at 21:47
This link works: iawltxhtml.ivo-welch.info But I think Ivo should fix this on his own – either here or introduce again the above linked resource. –  Speravir Mar 15 at 23:07
strange...works for me all the time. it's also evolving quickly, as we use it for our specific purposes. docs are behind...and probably a real release.../iaw –  ivo Welch Mar 16 at 2:14

So far I have found Hevea to be the easiest to use, understand and customize for LaTeX to HTML with lots of custom LaTeX macros to convert. I also spent a lot of time with plasTeX and found it very powerful but with a steep learning curve and hard to master.

-