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:
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"> </span><span
class="cmtt-8">x</span><span
class="cmtt-8"> </span><span
class="cmtt-8">=</span><span
class="cmtt-8"> </span><span
class="cmtt-8">10;</span><span
class="cmtt-8"> </span><br /><span class="label"><a
id="x1-3r2"></a></span><span
class="cmtt-8"> </span><br /><span class="label"><a
id="x1-4r3"></a></span><span
class="cmtt-8">//</span><span
class="cmtt-8"> </span><span
class="cmtt-8">This</span><span
class="cmtt-8"> </span><span
class="cmtt-8">is</span><span
class="cmtt-8"> </span><span
class="cmtt-8">a</span><span
class="cmtt-8"> </span><span
class="cmtt-8">comment</span><span
class="cmtt-8"> </span><br /><span class="label"><a
id="x1-5r4"></a></span><span
class="cmtt-8">string</span><span
class="cmtt-8"> </span><span
class="cmtt-8">str</span><span
class="cmtt-8"> </span><span
class="cmtt-8">=</span><span
class="cmtt-8"> </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:
I would want the HTML result to look like this:
Best Answer
It is perhaps possible to hook somewhere in the
listings
commands so that they write sensible class instructions. But on the other sidelistings
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 thentex4ht
surrounds the chars with classes which you can set throught css. Here an example (it runs only withhtlatex
, if you want to usepdflatex
you will have to move the\Css
to a configuration file: