9

I'm trying to convert some LaTeX into HTML but my code listings don't preserve their code coloring. Why is that and how do I fix it?

This is what it looks like:

enter image description here

Side note: I'm also wondering why the caption on the table was removed.

Edit

Here is an example listing with colors:

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\lstset{
    basicstyle=\footnotesize\ttfamily,
    language=[Sharp]C, 
    keywordstyle=\color[rgb]{0,0,1.0}\bfseries,  
    commentstyle=\color[rgb]{0.133,0.545,0.133}\bfseries,
}

\begin{document}
\begin{lstlisting}
int x = 10;

// This is a comment
string str = "hello";
\end{lstlisting}
\end{document}

This produces the following HTML when doing: htlatex myfile.tex:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  "http://www.w3.org/TR/html4/loose.dtd">  
<html > 
<head><title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)"> 
<meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)"> 
<!-- html --> 
<meta name="src" content="LaTeX4.tex"> 
<meta name="date" content="2012-07-20 12:30:00"> 
<link rel="stylesheet" type="text/css" href="LaTeX4.css"> 
</head><body 
>
   <!--l. 12-->
   <div class="lstlisting" id="listing-1"><span class="label"><a 
 id="x1-2r1"></a></span><span 
class="cmtt-8">int</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">x</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">=</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">10;</span><span 
class="cmtt-8">&#x00A0;</span><br /><span class="label"><a 
 id="x1-3r2"></a></span><span 
class="cmtt-8">&#x00A0;</span><br /><span class="label"><a 
 id="x1-4r3"></a></span><span 
class="cmtt-8">//</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">This</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">is</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">a</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">comment</span><span 
class="cmtt-8">&#x00A0;</span><br /><span class="label"><a 
 id="x1-5r4"></a></span><span 
class="cmtt-8">string</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">str</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">=</span><span 
class="cmtt-8">&#x00A0;</span><span 
class="cmtt-8">"</span><span 
class="cmtt-8">hello</span><span 
class="cmtt-8">"</span><span 
class="cmtt-8">;</span>

   </div>

</body></html>

Which looks like this:

enter image description here

I would want the HTML result to look like this:

enter image description here

Filip Ekberg
  • 2,503

1 Answers1

8

It is perhaps possible to hook somewhere in the listings commands so that they write sensible class instructions. But on the other side listings is very complicated and this could easily break. Imho a more simple approach is with fonts: if every style is connected to a different font then tex4ht surrounds the chars with classes which you can set throught css. Here an example (it runs only with htlatex, if you want to use pdflatex you will have to move the \Css to a configuration file:

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage[T1]{fontenc}

\lstset{
    basicstyle=\ttfamily,
    language=[Sharp]C,
    keywordstyle=\rmfamily\bfseries,
    commentstyle=\sffamily,
}
\def\mystyle{}


\begin{document}

\Css{div.lstlisting .ectt-1000 {font-family: monospace;color:blue}}
\Css{div.lstlisting .ecss-1000 {font-family: monospace;color:green}} 
\Css{div.lstlisting .ecbx-1000 {font-family: monospace;color:red}} 


\section{Test!}

{\ttfamily hello}, {\sffamily Hello} {\bfseries Hello}


\begin{lstlisting}
int x = 10;

// This is a comment
string str = "hello";
\end{lstlisting}
\end{document}
Ulrike Fischer
  • 327,261
  • That looks very interesting! I'll try that, thanks! Another question that just arised from this is, what if I have \newenvironment{myEnv} and I want a specific class for this, is there an easy way to do that? I tried \ConfigureEnv{myEnv}{\HCode{<div class="myEnv">}}{\HCode{</div>}}{}{} but it doesn't seem to wrap around my environment. No worries regarding pdflatex, I'm using 2 different tex-files and just including the "raw" stuff and doing all the configs/definitiouns outside :) – Filip Ekberg Jul 20 '12 at 13:10
  • You must also define the hooks. See http://www.cvr.cc/tex4ht-low-level-commands/ – Ulrike Fischer Jul 20 '12 at 13:30
  • Ok! Do you think you could help me out with an example of what it would looke like for when I do \begin{myEnv}test\end{myEnv}? One last thing that I'm noticing is that I'm getting a lot of errors regarding my own environments and on figures, looks somewhat like this: http://pastebin.com/9eGGbP2g have you got any idea why that is? – Filip Ekberg Jul 20 '12 at 14:09
  • 1
    Actually simply \ConfigureEnv{myEnv}{\HCode{<div class="myEnv">}}{\HCode{</div>}}{}{} (after \begin{document}) works fine for me (I had a typo in my first test). So you should perhaps make an example (and open a new question). Same for your error message. I have no idea where e.g. the \FBafil comes from. – Ulrike Fischer Jul 20 '12 at 14:16
  • I'm not doing any \FBafil myself, so it's probably through some macro or something. It just bothers me that I have to hold my Enter key for 1 minute to accept all errors. I'll open a new question later, thanks. I'll mark your answer as accepted in a couple of days to let more people contribute with answers, if that is OK. – Filip Ekberg Jul 20 '12 at 14:26