[tex4ht] is it possible to define a Latex environment, where one can issue direct HTML code in it for tex4ht only?

Nasser M. Abbasi nma at 12000.org
Fri Jun 10 11:46:46 CEST 2016

On 6/10/2016 3:53 AM, Michal Hoftich wrote:
> ""Nasser,
> this is of course typical example of case when you should put your
> macros to .sty file and redefinitions for tex4ht into .4ht file.
> mycode.sty:
> %%%%%%%%%%%%
> \ProvidesPackage{mycode}
> \RequirePackage{listings}
>   \lstnewenvironment{code}
>   {\lstset{language=matlab}}
>   {}
> \endinput
> %%%%%%%%%%%%
> tex4ht configurations in mycode.4ht:
> %%%%%%%%%%%%
> \ScriptEnv{code}
> {\ifvmode\IgnorePar\fi\EndP\NoFonts\hfill\break\HCode{<pre>\Hnewline}}
> {\HCode{</pre>}\EndNoFonts\endgraf}
> \endinput
> %%%%%%%%%%%%
> and the modified TeX file:
> %%%%%%%%%%%%
> \documentclass[12pt]{book}%
> \usepackage[T1]{fontenc}
> \usepackage{fancyvrb}
> \usepackage{mycode}
> \begin{document}
> some text
> \begin{code}
> X=10;
> y=5;
> \end{code}
> another text
> \end{document}
> %%%%%%%%%%%%
> tex4ht definitions are little bit more involved, in order to handle
> the paragraphs correctly ("\ifvmode\IgnorePar\fi\EndP" and "\endgraf),
> it contains also some minimal HTML insertions. There is no need to
> define other environments, \ScriptEnv can be used to handle existing
> environment and it can also be used to insert any HTML code.
> Best regards,
> Michal

Thanks  Michal;

This is really neat. I never knew how to do all the above before.
never used .sty and .4ht before and how they are used. (is
there a basic user-guide tutorial on these things?)

But I have 2 small questions:

I see the first line of the listing showing up in HTML shifted
to the right from the rest of the code. But it should be PRE,
so it should not get indented in any way?

If you run  make4ht --lua foo.tex on :

% !TeX program = lualatex

some text
another text

With your supplied mycode.4ht and mycode.sty in the same folder,
you'll see the HTML shows this:


i.e. first line is shifted. Is it possible to fix this?
This happens on firefox and chrom. The actual HTML is this:

<!--l. 8--><p class="noindent" >some text </p>
<!--l. 14--><p class="indent" >   another text </p>

The second question: I see no \ifdefined\HCode anywhere. How
does it know to use listings package, when compiled with
pdflatex or lualatex, and to use the HTML ScriptEnv/HTML part
when  compiled with make4ht? I am confused on this part. Is there
some implicit \ifdefined\HCode going on under the hood here?

Thanks again, these files really help.


More information about the tex4ht mailing list