我正在尝试将一些 LaTeX 转换为 HTML,但我的代码列表没有保留其代码颜色。这是为什么?我该如何修复?
它看起来是这样的:
附注:我也想知道为什么表格上的标题被删除了。
编辑
以下是带有颜色的示例列表:
\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}
执行下列操作后将生成以下 HTML 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>
看起来像这样:
我希望 HTML 结果看起来像这样:
答案1
也许可以将listings
命令中的某个地方挂钩,以便它们编写合理的类指令。但另一方面,listings
这非常复杂,而且很容易中断。我认为更简单的方法是使用字体:如果每种样式都连接到不同的字体,那么用tex4ht
您可以通过 css 设置的类包围字符。这里有一个例子(它只能在 上运行htlatex
,如果您想使用,pdflatex
您必须将 移动\Css
到配置文件:
\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}