0

If you could spend a week full-time with Windows looking into various methods for converting LaTeX to HTML, and then actually implement an approach based on what seems to be the most successful/popular method, what would the steps be to attain this goal in an accelerated manner?

1

After toiling with a lot of methods, and sacrificing considerable time on forums, I can finally assemble together a description for what I have learned is the fastest way to obtain a good-looking LaTeX article or book online, based on the tex4ht program of the MiKTeX installation. Don’t worry if you use another LaTeX compiler, since access to tex4ht in MiKTeX has been deeply developed and is kept up to date within reasonable schedules. There are also a lot of Linux users in the TeX community, and on occasion, sometime forum responses default to what worked in Linux, and that is why I am providing this description for the Windows 10 community.

Below is what worked for me, with a lot of help from members of the TeX forum of SE. Within about an hour or two, by following the steps below, you should be able to produce a very nice online document. Compare this time (1-2 hours) with the 4-6 days of full time debugging and trial & error that was required to get this approach working.

  1. Assuming that you already have a book(article) prepared and compiled in LaTeX (let’s assume your book was prepared in the file mybook.tex, and that it’s stored on disk in d:\mylatexfiles), download and install MiKTeX 2.9 (or later, June 2019)

  2. Download pdf2svg from github and extract the file to any directory (best to place it e.g. in a folder in C:\Users\username\AppData\Local\pdf2svg-windows-master). Make sure you copy all of the .dlls (files) and pdf2svg.exe file back into this folder, since they may be extracted in a subfolder with the same name. In other words, the folder C:\Users\username\AppData\Local\pdf2svg-windows-master\ needs to have the file pdf2svg.exe in it along with all the .dll library files.

  3. Using the command-prompt (type “command” in the windows search box, then hit return -- FYI do not use administrator rights), and go to the folder where pdf2svg.exe and all the .dll files were extracted from the .zip file. In this folder type when in DOS (black screen):

    set PATH=%PATH%;C:\Users\username\AppData\Local\pdf2svg-windows-master\

    (I installed pdf2svg in the C:\Users\username\AppData\Local\pdf2svg-windows-master folder). Note: you need the slash at the end of PATH command.

  4. In the “mylatexfiles” folder that contains mybook.tex, create a new text file and rename it to myfile.cfg. Paste the following “preamble” code into myfile.cfg:

\Preamble{xhtml}
\DeclareGraphicsExtensions{.svg,.png,.jpg}
%Use SVG graphic outputs for inline And display equations, as well as all images
\Configure{Picture}{.svg}
\Configure{graphics*}
   {eps}
   {\openin15=\csname Gin@base\endcsname\PictExt\relax%
   \ifeof15%
   \Needs{"epstopdf \csname Gin@base\endcsname.eps && pdf2svg \csname Gin@base\endcsname.pdf \csname Gin@base\endcsname.svg"}
   %\Needs{"magick convert -trim -density 110x110 -transparent \#FFFFFF \csname Gin@base\endcsname.eps \csname Gin@base\endcsname\PictExt"}%
   \fi%
   \closein15%
   {\Configure{Needs}{File: \csname Gin@base\endcsname\PictExt}\Needs{}}%
   \Picture[\csname a:GraphicsAlt\ endcsname]{{\csname Gin@base\endcsname\PictExt} \csname a:Gin-dim\endcsname
}}
\ifdefined\HCode\else .... \fi
%Clear the ugly lines that separate sections, tables, figures from text (by default)
\DeclareMathSizes{12}{14}{10}{8}
\ConfigureEnv{tabular}
    {\HCode{}}{\HCode{}}{}{}
\ConfigureEnv{figure} 
    {\HCode{}}{\HCode{}}{}{}    
\Configure{float}
   {\ifOption{refcaption}{}{\csname par\endcsname\ShowPar \leavevmode}}
   {\HCode{}}
{\ifvmode \IgnorePar \fi\EndP \HCode{}\csname par\endcsname\ShowPar}
%Center text on page, use wide margins on left And right
\Css {body {margin-top: 100px;
                 Margin-Right:  400px;
                 Margin-Bottom:  10px;
                 Margin-Left:  400px;
                }
         }
%Use Helvetica fonts throughout:
\Css{body{ font-family "Helvetica Neue", Helvetica, "Nimbus Sans", Arial, sans-serif;}}
%Use 1.5 line spacing: 
\Css{body{ line-height 17pt;}}
\begin{document}
%Format section colors:
\def\sechead#1#2{.#1Head #2,.like#1Head #2}
\def\sections#1{\sechead{chapter}{#1}, \sechead{section}{#1}, \sechead{subsection}{#1}, \sechead{subsubsection}{#1}, \sechead{paragraph}{#1} }
\let\oldchaptername\chaptername 
\renewcommand{\chaptername}{\Tg<span class="chapname">\oldchaptername\Tg</span>}
\Css{\sections{}{color:green;}}
\Css{\sections{br}{display:none;}}
\Css{\sections{.titlemark}{color:green;}}
\Css{\sections{.titlemark:after}{content:" ";}}
\Css{\sechead{chapter}{.titlemark}{
display: block;
Font-Size: .8em;
Margin-Bottom: 1.6em;
border-Bottom: 1px solid black;
Text-align: Right;
}}
\Css{\sechead{chapter}{}{
Margin-Top: 2em; margin-bottom:1em;
Text-align: center;
}}
\Css{\sechead{subsection}{}{
Font-Size: 1.1em;
Text-align: Left;
}}
\Css{\sechead{subsubsection}{}{
Font-Size: 1.1em;
Text-align: Left;
}}
\Css{\sechead{paragraph}{}{
color:  navy;
Font-Size: 1.1em;
Text-align: Left;
}}
\EndPreamble
  1. Reboot your computer.
  2. Edit the mybook.tex file and modify every \includegraphics command throughout the book(article), so that only width=? is used and the image filename contains the “.eps” extension, as in the following example:

\includegraphics[bb=38 185 721 543,width=6in,keepaspectratio]{correlation.eps}

(if you leave out the .eps suffix, you may not see the image in the html output!)

Also in mybook.tex add the following commands in the “preamble” at the top of your mybook.tex file before the \begin{documents} command:

\usepackage{titlesec}
\usepackage{titletoc}
\ifx\HCode\undefined
\titleformat{\section}[block]{}{\thesection.}{50pt}{}
\fi

We will also be specifying the Helvetica san serif typeface instead of the default serif roman typeface, by placing the following commands in the “preamble” at the top of your mybook.tex file before the \begin{documents} command:

\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault} 
\usepackage[T1]{fontenc} 
  1. Save mybook.tex
  2. Next, type “command” into the Windows search box (lower left of screen) to open the DOS command (black) window.
  3. While in DOS, go to the d:\mylatexfiles folder. Paste the following text into the command prompt window (then hit return):
 c:
 cd Users
 cd username(you)
 cd AppData
 cd Local
 cd pdf2svg-windows-master
 set PATH=%PATH%;C:\Users\username\AppData\Local\pdf2svg-windows-master\
  1. This will reset the PATH so that tex4ht of MiKTeX “sees” the pdf2svg program from the folder where your book(article) .tex file is.

  2. After running the above code in Step 6, run the following to compile your book(article) to html:

d:
cd mylatexfiles
htlatex mybook.tex "myfile.cfg,charset=utf-8,pic-m" " -utf8 -cunihft"

(In the command line to compile the mybook.tex file, “htlatex” runs the tex4ht algorithm of MiKTeX. The tex4ht preamble code is specified by including “myfile.cfg” as a parameter in quotes. If this is not done, then you likely won’t produce very much in the mybook.html output file. Leave all of the utf-8 coding that is provided, as these are defaults.)

  1. Wait for processing to complete, and if there are any errors, then follow the direction on the screen. If there is an error at \tableofcontents, then just type “s” and override (ignore) the error – I do this 3 times during each compile, since the compiler makes 3 runs through the LaTeX code. To see the results, double-click on mybook.html to view in your browser.

Every time you open & close the DOS command prompt, or reboot, you have to copy and paste the DOS commands in Step 6 into the command prompt (black DOS screen) so that MiKTeX and tex4ht see the pdf2svg program. After the DOS prompt is open (black screen and you have already run the commands in Step 6), you can compile your book(article) by pasting the DOS commands in Step 9 into the DOS command prompt window.

  1. To change image (picture) sizes in the html output, I have learned that you only need to specify the width in the \includegraphics line, for example to double the size of an output image:

    simply change (in mybook.tex):

\includegraphics[bb=38 185 721 543,width=6in,keepaspectratio]{correlation.eps}

to

\includegraphics[bb=38 185 721 543,width=12in,keepaspectratio]{correlation.eps}

Some other special commands that are used in the preamble command (in myfile.cfg) are as follows.

\Preamble{xhtml} specifies that you want xhtml. If you change this to {html} or {xhtml,mathml} then the higher resolution equations will not be provided in the output. In fact, if you specify html or mathml, a lot of nice math features in the html output get turned off, so leave the {xhtml} by itself.

\Configure{Picture}{.svg} specifies that you want higher resolution .svg images for all of the pictures & equations.

Also, the preamble command:

   \Needs{"epstopdf \csname Gin@base\endcsname.eps && pdf2svg \csname Gin@base\endcsname.pdf \csname Gin@base\endcsname.svg"}

will convert all your EPS images to PDF, and then to SVG via the epstopdf and pdf2svg programs. Tex4ht will typically use ImageMagick for image conversion, but if you check on some forums, ImageMagick is known not to do a good job on vector graphics (EPS, PDF) files. I tried on numerous occasions to get the magick convert command going to invoke ImageMagick for transforming EPS files into SVG and the results were not good, and always resulted in low resolution images that were too small. Using the approach we are taking with epstopdf and then pdf2svg, you can actually have EPS files in the converted_graphics folder that were originally generated from GIFs, PNGs, etc. with a variety of pixel dimensions and dpi values (resolution) , and the image results will still come out very good – much better than using ImageMagick. However, the magick convert line is in the preamble should you want to remove the % commenting and give it a try – but I wouldn’t waste my time.

The \ConfigureEnv{tabular}, \ConfigureEnv{figure}, and \Configure{float} commands in the preamble remove the ugly rules (lines) in the default html output, which by default separate tables, figures, sections, subsections, etc. from each other.

The first \Css command in the preamble (myfile.cfg) instructs to tex4ht that you want the text centered on the web page with about a 400 pixel left and right margin. These commands prevent the book(article) text width from being as wide as your browser window, which is not appealing. You want your online material to look like a github wiki page, or lecture like https://cs231n.github.io/convolutional-networks/#overview

The second \Css command in the preamble (myfile.cfg) instructs tex4ht to ensure the document wide typeface style is Helvetica. Without this command some environments such as e.g. tables will default to Times Roman.

The third \Css command in the preamble (myfile.cfg) increase line spacing a little since the default is too crunched together.

The last series of \Css command change the section header colors and bump up the font size in sections, subsections, and paragraphs, as these are a litter small by default – and don’t look correct.

Another parameter used in the command line is pic-m, which instructs tex4ht that you want higher resolution images for every math symbol, inline equation, and display equation in your book(article). Without using pic-m in the command line for compiling, the equations will have lower resolution and look hazy or a little blurred, but with the command, the equations will be very clear with high resolution.

  • 2
    Why don't you add the path to pdf2svg permantly to your environment variable? – Ulrike Fischer Jun 12 at 20:45
  • 1
    Your description seems to be assuming EPS as the original format for images, these days it's perhaps more likely (especially if the original was being used with pdflatex rather than latex) that they are in png or jpg formats, in which case they can be used directly in the html version and no conversion is necessary. For mathematics it is (almost) always better to keep the mathematics as text and not convert to images – David Carlisle Jun 12 at 21:47
1

Your question asks how to get LaTeX to HTML and there are many very good routes for you to investigate during your "week" Many will provide some attempts at fidelity and thus allow for roundtripping, such that you could capture the html and attempt to put it back into a blank TeX file. but consider where could the tex primitives come from which html space = #em where could a \includegraphics directive come from. In short its always a one way trip.

One way ==> LaTeX > PDF > HTML

The aim of pdfxxTeX is to create a fixed typeset rendering of good enough for High Quality view and print. the aim of HTML is to allow Universal browsing including reflow and zoom. The best current bridge is either embed the static PDF pages in the viewer (very common) or convert the PDF to HTML with SVG either of those can be done in seconds with half decent results so here is page16.htm on left compared with page16.pdf on right. Apart from source there is no need for TeX the Commandline converter can be run on any machine without TeX it only needs to be a basic PDF 2 HTM task at this stage.

enter image description here

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.