14

I heard that LuaTeX provides access to the internals of TeX. So, I have two questions in this regard:

  • Is it possible to modify the output routine with LuaTeX so that HTML is produced instead of DVI or PDF?
  • If possible, are there any projects aiming at this?
3
  • 3
    The answers to your questions are: yes and yes. To be precise: ConTeXt MkIV (which is built on top of LuaTeX) can produce (X)HTML natively from ConTeXt source. I'm not aware of any LaTeX package to do such a thing, though.
    – mbork
    Feb 21, 2014 at 15:19
  • 3
    ConTeXt outputs XML. Most browswers display this XML, which can be styled using CSS, without problems. See Converting ConTeXt document to HTML for a demonstration.
    – Marco
    Feb 21, 2014 at 15:24
  • 1
    To add to what @Marco and @ mbork pointed out in their comments: Context does indeed produce XML out of the box, but it does not actuall need to “modify the output routine”. (That’s actually pretty obvious since HTML doesn’t have the concept of a page like TeX does.) Instead, the relevant structural commands are extended to tag regions of text by means of Luatex attributes, which doesn’t even require much effort since most user-level commands are derived from generic ones. Feb 21, 2014 at 18:04

1 Answer 1

4

As it is possible to access nodes created by \special commands, we can access tags inserted by tex4ht. Small proof of concept is lua4ht package.

Advantage is that it supports fontspec and unicode-math packages, which tex4ht doesn't support otherwise, the downside is that mathml production is broken, so it is produced as normal italic text in the sample. In theory, we can produce mathml directly from LuaTeX's math nodes, I've tried some exercises in this direction and it seemed doable.

Sample:

\documentclass{article}

\usepackage{fontspec}
\usepackage{unicode-math}

\begin{document}

\section{Font support}

Příliš \textit{žluťoučký kůň} úpěl \textsc{ďábelské ódy}. A \textbf{další text}

\section{Math}

$\forall x \in X, \quad \exists y \leq \epsilon$

\section{Tables}

\begin{tabular}{ll}
  hello & world\\
  hello & again\\
\end{tabular}
\end{document}

we need also small config file, hej.cfg:

\Preamble{xhtml}

\Configure{textit}{\HCode{<em>}}{\HCode{</em>}}
\Configure{textbf}{\HCode{<strong>}}{\HCode{</strong>}}
\Configure{textsc}{\HCode{<span class="sc">}}{\HCode{</span>}}
\Configure{$}{\HCode{<span class="inline-math">}}{\HCode{</span>}}{}
\begin{document}
\Css{.sc{font-variant: small-caps;}}
\Css{.inline-math{font-style:italic;}}
\EndPreamble

compile with:

make4ht -b lua4ht -um draft -c hej.cfg  sample.tex  "new-accents"

and the result:

enter image description here

  <h3 class="sectionHead"><span class="titlemark">1 </span> <a 
 id="x1-10001"></a>Font support</h3> <!--l. 10--><p class="noindent" >Příliš <em>žluťoučký kůň</em> úpěl <span class="sc">ďábelské ódy</span>. A <strong>další text</strong>  </p><!--l. 12--><p class="noindent" > </p><h3 class="sectionHead"><span class="titlemark">2 </span> <a 
 id="x1-20002"></a>Math</h3> <!--l. 14--><p class="noindent" ><span class="inline-math">∀𝑥∈𝑋, ∃𝑦≤𝜖</span>  </p><!--l. 16--><p class="noindent" > </p><h3 class="sectionHead"><span class="titlemark">3 </span> <a 
 id="x1-30003"></a>Tables</h3><div class="tabular"> <table id="TBL-1" class="tabular" 
cellspacing="0" cellpadding="0"  
><colgroup id="TBL-1-1g"><col 
id="TBL-1-1" /><col 
id="TBL-1-2" /></colgroup><tr  
 style="vertical-align:baseline;" id="TBL-1-1-"><td  style="white-space:nowrap; text-align:left;" id="TBL-1-1-1"  
class="td11">  hello  </td><td  style="white-space:nowrap; text-align:left;" id="TBL-1-1-2"  
class="td11">  world  </td></tr><tr  
 style="vertical-align:baseline;" id="TBL-1-2-"><td  style="white-space:nowrap; text-align:left;" id="TBL-1-2-1"  
class="td11">  hello  </td><td  style="white-space:nowrap; text-align:left;" id="TBL-1-2-2"  
class="td11">  again  </td></tr><tr  
 style="vertical-align:baseline;" id="TBL-1-3-"><td  style="white-space:nowrap; text-align:left;" id="TBL-1-3-1"  
class="td11">   </td></tr></table> </div>  
4
  • Hi, I tried to compile as instructed and receive: [ERROR] htlatex: ? ? LaTeX Error: File `lua4ht.sty' not found. I have a proper texlive 2020 installation.
    – tatojo
    Jan 28, 2021 at 23:25
  • @tatojo it is not on CTAN and in TeX distributions. It is not in a usable state. Do you have problems with normal LuaTeX support in TeX4ht?
    – michal.h21
    Jan 29, 2021 at 9:38
  • How to get the html file from your tex document with lualatex?
    – tatojo
    Feb 12, 2021 at 15:03
  • @tatojo it is best to use make4ht -l filename.tex. It uses LuaLaTeX for the compilation. It still uses the DVI file for the extraction, but it is the best solution in most cases. you can also try github.com/michal-h21/lua4ht, but I am not sure if it even works with current LuaLaTeX
    – michal.h21
    Feb 12, 2021 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .