# [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:46:17 CEST 2016

On 6/10/2016 1:23 AM, Jagath AR wrote:
> Hi Nasser,
> The error in your MWE case is due to undefined "\PRE" command, which should
> be "/PRE".
>
> Regards
> Jagath
>

Opps, sorry about that. Too much latex \\\\ and for second forgot about
HTML uses //// ;)  Ok, so PRE do not give error. But PRE is not really
what I wanted, I meant that as an example. I still can't get
the actual real HTML environment itself setup to work. This is what
I am trying to do:

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

\ifdefined\HCode
\newenvironment{code}
{ %something is not working here
\ScriptEnv{html}
{\NoFonts\hfill\break}
{\EndNoFonts}
\begin{html}
}
{
\end{html}   %should this be \HCode{\end{html}}?
}
\else
\lstnewenvironment{code}
{\lstset{language=matlab}}
{}
\fi

\begin{document}
\begin{code}
X=10;
y=5;
\end{code}
\end{document}
---------------------

I am using CVR method here
http://tug.org/pipermail/tex4ht/2013q3/000823.html

On how to insert raw HTML directly. The idea is what, when
compiled to HTML, I'll get the source code in HTML. I still
need to add more HTML code to the above to make it work,
get the HTML to even generate, with the code inside it,
then the rest is easy. The above does not compile now,
I think because newenvironment does not like what it sees there
and I am getting the syntax right or it simply can't be
done like this.

---------------------------------------
(/usr/local/texlive/2015/texmf-dist/tex/generic/tex4ht/html4.4ht)
(/usr/local/texlive/2015/texmf-dist/tex/generic/tex4ht/html4-math.4ht))
(./foo2.aux) (/usr/local/texlive/2015/texmf-dist/tex/latex/base/t1cmtt.fd))
Runaway argument?
^^MX=10;^^M\end{code}^^M\end{document}^^M
! File ended while scanning use of \:temp.
<inserted text>
\par
<*> ...}\makeatother\HCode .a.b.c.\input  foo2.tex

? x
No pages of output.
----------------------

Thank you,
Nasser

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