4

I want to convert a plain tex document to html using make4ht. If I use this command line make4ht document.tex the program calls the compiler "latex" and blocks the compilation. The make4ht --help command does not explain how to compile the plain tex.

What is the exact command line to compile a plain tex to html document?

1
5

It is not so straightforward to get TeX4ht working with Plain TeX. Ulrike already pointed to my older answer. We can use that as a starting point. Save the following code as plain-4ht.tex:

% file plain-4ht.tex
% the following macro needs to be executed in order to include all .4ht files
\csname tex4ht\endcsname

I've simplified the code to the bare minimum. The \csname tex4ht\endcsname command needs to be executed in order to insert the basic HTML structure.

This file just needs to be inserted after all macro definitions:

\input plain-4ht

Hello world
\bye

The next issue is how to actually execute plain TeX using make4ht. We need to use a build file for that. Save the following file as mybuild.lua:

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 {}

It uses the Make:add command to create new command Make:httex which executes Plain TeX. The used engine can be configured using htlatex parameter (the default engine is htlatex="dviluatex")

You can compile the document using

make4ht -e mybuild.lua filename.tex

The last issue is how to configure your custom macros. TeX4ht can load configuration files for particular files thanks to patched \usepackage command, but it doesn't work in Plain.

Let's say that you have a file mymacros.tex:

\def\mytitle#1{\bgroup\bf #1\egroup\par}

We want to use the <h1> element for \mytitle in the HTML document. Create file mymacros.4ht:

\NewConfigure{mytitle}{2}
\def\mytitle#1{\a:mytitle#1\b:mytitle\par}

\Configure{mytitle}{\ifvmode\IgnorePar\fi\EndP\HCode{<h1>}}{\HCode{</h1>}}

A sample document:

\input mymacros
\input plain-4ht

\mytitle{My title}

Some text.

\bye

The mymacros.4ht file can be included in the .cfg file:

\Preamble{xhtml}
\catcode`\:=11
\input mymacros.4ht
\catcode`\:=12
\EndPreamble

We need to use correct catcodes as : characters are used in the .4ht files. This is all done automatically in LaTeX.

You can now compile the document using:

 make4ht -e mybuild.lua -c config.cfg filename.tex

This is the result:

enter image description here

<!DOCTYPE html> 
<html lang='en-US' xml:lang='en-US'> 
<head>

<title>sample.html</title> 
<meta charset='utf-8' /> 
<meta name='generator' content='TeX4ht (http://www.tug.org/tex4ht/)' /> 
<meta name='viewport' content='width=device-width,initial-scale=1' /> 
<link rel='stylesheet' href='sample.css' type='text/css' /> 
<meta name='src' content='sample.tex' /> 
</head><body>
<h1>My title</h1>
<!-- l. 6 --><p class='indent'>    Some text.

</p>    

</body> 
</html>

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.