[Tex/LaTex] LaTeX to HTML preserving code coloring from listings

htlatexlistingstex4ht

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

Best Answer

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}
Related Question