[tex4ht] Non-tabular plain TeX alignments & indentation in HTML

Richard M Kreuter kreuter at progn.net
Sun Feb 4 14:19:53 CET 2024


Hi all,

I have a plain TeX application in which I'd like to try converting some
non-tabular alignments into <pre> elements with text that's (roughly)
the alt text tex4ht would produce for those alignments in display math
mode. But I can't figure out how to produce that alt text directly. Any
advice/pointers/suggestions?

Thanks very much,
Richard

(A detailed explanation and reproduction of what I'm trying to do
follows, in case there's a better approach than what I'm thinking of.)

Background: I use a Literate Programming system that pretty-prints
source code using plain TeX. The plain TeX indents source code using
alignments as explained in chapter 22 of the TeXbook.

Example: for source code to be indented like this (cf. TeXbook exercise
22.2):

if n<r then n:=n+1
       else begin print_totals; n := 0;
       end
while p>0 do
 begin q := link(p); free_node(p); p := q;
 end;

the plain TeX the LP tool produces goes like this:

\vbox{\+if n$<$r \cleartabs& then n:=n+1\cr
\+&else &begin print\_totals; n:=0;\cr
\+&&end;\cr
\+while p$>$0 do&\cr
\+\quad\cleartabs&begin q:= link(p); free\_node(p); p:=q\cr
\+&end;\cr}

Note that the LP tool happens not to use display math mode for these
alignments. (It does delimit source code alignments with other macros,
so it should be possible to hook those delimiter macros to establish
whatever environment I need, though.)

Problem: tex4ht converts the alignment to an HTML table, but the HTML
doesn't render (in Firefox) with indentation resembling the preceding
plain text fragment.

I noticed that when the above TeX is placed inside display math mode,
tex4ht translates the fragment into an <img> with alt text that's
indented as I want.

So for those source code alignments, I'd like to have tex4ht compute the
alt text it would create for display math mode, and use that text inside
a <pre> element, rather than involve HTML tables or images at all. (I
will also need to customize the alt text production at least somewhat:
underscores get turned into spaces, for instance.)

I've tried reading through tex4ht source to figure out how to generate
that alt text, but without any luck so far. So that's where I'm stuck.

And if there's a better way to achieve what I'm after, I'd be happy to
hear about it!

Lastly, for completeness, here's a minimized reproduction of the steps I
use. I'm mostly following guidance found here:

https://tex.stackexchange.com/questions/518970/make4ht-for-plain-tex

plain-4ht.tex contains 1 line:

--
\csname tex4ht\endcsname
--

plain-4ht.lua contains 6 lines:

--
Make:add("httex", 
'${htlatex} --interaction=${interaction} ${latex_par}' ..
"'\\def\\Link#1.a.b.c.{\\expandafter\\def\\csname tex4ht\\endcsname{\\expandafter\\def\\csname tex4ht\\endcsname{#1,html}\\input tex4ht.sty }}" ..
"\\def\\HCode{\\futurelet\\HCode\\HChar}\\def\\HChar{\\ifx\"\\HCode\\def\\HCode\"##1\"{\\Link##1}\\expandafter\\HCode\\else\\expandafter\\Link\\fi}" ..
"\\HCode ${tex4ht_sty_par}.a.b.c.\\input \"\\detokenize{${tex_file}}\"'", 
{htlatex="dviluatex"})
Make:httex {}
--

A complete plain TeX file using the offending alignments would be:

--
\input plain-4ht
\vbox{\+if n$<$r \cleartabs& then n:=n+1\cr
\+&else &begin print\_totals; n:=0;\cr
\+&&end;\cr
\+while p$>$0 do&\cr
\+\quad\cleartabs&begin q:= link(p); free\_node(p); p:=q\cr
\+&end;\cr}
\bye
--

And I convert it to HTML with

--
$ make4ht -e plain-4ht.lua sample.tex
--



More information about the tex4ht mailing list.