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

Jagath AR jagathar at gmail.com
Fri Jun 10 08:42:38 CEST 2016

Hi Nasser,
Will this work for you:

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

\def\putVerb{%
\HCode{<PRE>}%
\verbatiminput{a.tex}%
\HCode{</PRE>}%
}

\ifdefined\HCode
\newenvironment{code}
{\verbatimwrite{a.tex}}
{\endverbatimwrite
\aftergroup\putVerb}
\else
\lstnewenvironment{code}
{\lstset{language=matlab}}
{}
\fi

\begin{document}
\begin{code}
X=10;
Y=10;
\end{code}
or
\begin{code}
X=100;
Y=200;
\end{code}

\end{document}

Regards
Jagath

On 10 June 2016 at 11:53, Jagath AR <jagathar at gmail.com> wrote:

> Hi Nasser,
> The error in your MWE case is due to undefined "\PRE" command, which
> should be "/PRE".
>
> Regards
> Jagath
>
> On 10 June 2016 at 11:41, Nasser M. Abbasi <nma at 12000.org> wrote:
>
>> 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
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/tex4ht/attachments/20160610/8e4eec5d/attachment.html>