1

I can't find an acceptable way to convert LaTeX input to HTML output. Ideally, one should be able to say pdflatex my.tex to produce a PDF file, and htmllatex my.tex to get HTML output.

I've tried a long list of semi-solutions: texmath, LateXMathML, Texvc, pdf2htmlEX, htlatex, pandoc, and some others, and the output doesn't measure up.

It seems that what's needed is a TeX engine that is HTML-aware. In principle, this seems feasible. Is anyone working on a project like this? Can someone who's current on the state of the code indicate how hard would it be? It's been a long time since I read TeX: The Program, but TeX ultimately outputs a series of vboxes and hboxes, and most of these boxes wouldn't be necessary under HTML. Straight text would pass through and emerge unchanged, while anything in an environment would require "boxing." It seems like the main difficulty would be on the HTML end since HTML does not make it easy to place individual glyphs at a given location.


(Feb, 2022) This just passed under my nose: www.swiftlatex.com. It's a TeX compiler that, they say, runs entirely in a browser.
6
  • 6
    note that htlatex is obsolete, use make4ht instead. You haven't specified what issues you have, so it is quite hard to answer your question. Each converter works in a different way and they have different goals. TeX4ht uses LaTeX for the conversion, so it supports most packages and commands. But it can clash with some packages, which may result in a failed conversion. It tries to produce semantic HTML output, but it can keep the basic font information, so you will still get some formatting even for unsupported commands.
    – michal.h21
    Sep 7, 2020 at 14:28
  • 2
    while the Tex engine can help you usually need to convert at a higher level. so \section gets converted to <h2> not just the tex engine view that just has fonts and boxes Sep 7, 2020 at 15:28
  • I'd be quite happy if \section{Part One} came through in a way that loses any information about intent, and merely indicated a larger font size and left-alignment. Sep 7, 2020 at 16:26
  • I developed github.com/loopspace/latex-to-internet for this. My website is written in LaTeX and exported to HTML using this package. Feb 19, 2021 at 21:48
  • @AndrewStacey Do you have an example page of your website, so that one can see the TeX source (if available) and the resulting the HTML output? Or else, can you add a representative example to the GitHub repo? Feb 21, 2021 at 7:56

1 Answer 1

2

You can do this without "engines". Here is a TeX example

\newwrite\html
\parindent=0pt
\immediate\openout\html\jobname.html
\immediate\write\html{<!DOCTYPE html>
<html>
  <head><h1>From TeX to PDF and HTML</h1></head>}
\def\section#1{{\bf #1}\vskip\baselineskip \immediate\write\html{<h2>#1</h2>}}
\def\subsection#1{{\bf #1}\vskip\baselineskip \immediate\write\html{<h3>#1</h3>}}
\def\text#1{#1 \vskip\baselineskip \immediate\write\html{#1 <br>}}
\section{Section}
\subsection{Subsection}
\def\hyperlink#1{#1\vskip\baselineskip\immediate\write\html{ <a href="https://tex.stackexchange.com/questions/561733/html-aware-tex-latex">#1</a>}}

\text{This is some text. This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text.This is some text. }\hyperlink{Tex-Exchange}
\immediate\write\html{</body></html>}
\immediate\closeout\html
\bye
2
  • 1
    One of the nice suggestion....
    – MadyYuvi
    Feb 20, 2021 at 8:16
  • Your suggestion works fine for \section{...}, is it possible to get the text as is, e.g., \begin{text}....\end{text} to <text>...</text> please suggest
    – MadyYuvi
    Apr 29, 2021 at 4:22

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.