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 <jagat...@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 <n...@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
>>
>
>

Reply via email to