Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Here is a MWE:

\documentclass{book}
\usepackage{listings}
\begin{document}

\begin{lstlisting}
var x = 10;

// Some code
if(x == 10) return null;
\end{lstlisting}

\end{document}

When this is processed by htlatex like this: htlatex MyFile.tex it gives me this HTML for the listing:

<div class="lstlisting">
    <!--l. 5-->
    <pre class="listings">&nbsp;<br><span class="label"><a id="x1-2r1"></a></span>var&nbsp;x&nbsp;=&nbsp;10;&nbsp;<br><span class="label"><a id="x1-3r2"></a></span>&nbsp;<br><span class="label"><a id="x1-4r3"></a></span>//&nbsp;Some&nbsp;code&nbsp;<br><span class="label"><a id="x1-5r4"></a></span>if(x&nbsp;==&nbsp;10)&nbsp;return&nbsp;null;
   &nbsp;<br><span class="label"><a id="x1-6r5"></a></span></pre>
    <!--l. 10-->
    <p class="noindent"></p>
</div>

As you can see it's put in a div corresponding to the environment but then it adds a <pre>, can I configure htlatex to not add that?

I tried adding a cfg with this:

\Preamble{html}
\begin{document}
    \ConfigureEnv{listings}{\HCode{<div class="listing">}}{\HCode{</div>}}{}{}
    \ConfigureEnv{lstlisting}{\HCode{<div class="lstlisting">}}{\HCode{</div>}}{}{}
\EndPreamble

But it didn't help.

share|improve this question
add comment

1 Answer

There is special configuration file for listings, it is little bit cryptic, but there is line NewConfigure{listings}{4}, which seems to provide configuration hooks for listings not as environment, but as command. So try this configuration:

\Preamble{xhtml}
\begin{document}
    \Css{.listings{white-space:pre;}}
    \Configure{listings}{\HCode{<div class="listing">}}{\HCode{</div>}}{}{}
\EndPreamble
share|improve this answer
    
Nice! Thanks, but now everything is displayed on one line. Guess it needs some tweaking –  Filip Ekberg Jul 22 '12 at 15:03
    
I want the exact behaviour, except I want the <pre> replaced with <div> –  Filip Ekberg Jul 22 '12 at 15:22
    
@FilipEkberg I added some css to preserve line breaks –  michal.h21 Jul 22 '12 at 15:44
    
Did you test that? Because it doesn't look the same as without the configuration. Without the configuration a double line-break (like in my example) generates <br><span><br> but with this configuration, it only adds <a></a><a></a> and adding pre-behavior with css doesn't give the same result –  Filip Ekberg Jul 22 '12 at 15:49
    
any idea how to mimic the exact behaviour but replace it with div instead of pre? –  Filip Ekberg Jul 23 '12 at 12:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.