# [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 08:11:25 CEST 2016

I am struggling with this. I need to be able to define
an environment, where when Latex file is compiled to HTML
does one thing, and when compiled to pdf does another thing.

This sounds easy enough. The problem is it is for code
listing. For pdf, I can use listing package no problem.

For HTML, I'd like to use special HTML code to make
the code more customized for HTML. The problem is
that one can't define an environment for verbatim
or listing content. So I am stuck. Here is a small
example which shows what I mean:

--------------------------
\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{fancyvrb}

\ifdefined\HCode
\newenvironment{code}
{\HCode{<PRE>}}
{\HCode{<\PRE>}}
\else
\lstnewenvironment{code}
{\lstset{language=matlab}}
{}
\fi
\begin{document}
\begin{code}
X=10;
\end{code}
\end{document}
-----------------------------

The above compiled ok to pdf, but ofcourse it gives
error with tex4ht

-----------------------
(/usr/local/texlive/2015/texmf-dist/tex/generic/tex4ht/html4-math.4ht))
(./foo2.aux)
! Undefined control sequence.
<argument> <\PRE
>
l.19 \end{code}

?
---------------------------

I used PRE above just an example. If I can this method
to work, I wanted to use more complicate HTML code
there for the start of the environment, which uses
outside javascript library for code formatting, much
nicer than what I can get from standard listings.

Listings works ok for both html and pdf. I just wanted
to use direct HTML environment for the code listing
when compiling with tex4ht.

I could of course duplicate the code itself, and use

\ifdefined\HCode
\ScriptEnv{html}
{\NoFonts\hfill\break}
{\EndNoFonts}
\begin{html}
my code here
\end{html}
\else
\begin{lstlisting}
code here
\end{lstlisting}
\fi

But I am trying to avoid this.

If some one has any idea how to do this, that will be great.

I asked on this also at

http://tex.stackexchange.com/questions/313994/defining-alternative-environment-for-code-listing-for-tex4ht-without-code-duplic

Thank you,
--Nasser