答案1
我几乎可以做到这一点,但将 / 改为红色以表示除法和使用 // 开始注释之间存在冲突。也许有人比我更聪明,可以提出解决办法。/* … */ 注释也存在类似的问题。
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
language=C,
classoffset=0,
basicstyle=\ttfamily\small,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue!60!black},
stringstyle=\color{blue},
numbers=left,
numberstyle=\ttfamily,
commentstyle=\color{black!50}\itshape,
morecomment=[l][\color{green!60!black}]{\#},
literate=*{0}{{\textcolor{magenta}{0}}}{1}%
{1}{{\textcolor{magenta}{1}}}{1}%
{2}{{\textcolor{magenta}{2}}}{1}%
{3}{{\textcolor{magenta}{3}}}{1}%
{4}{{\textcolor{magenta}{4}}}{1}%
{5}{{\textcolor{magenta}{5}}}{1}%
{6}{{\textcolor{magenta}{6}}}{1}%
{7}{{\textcolor{magenta}{7}}}{1}%
{8}{{\textcolor{magenta}{8}}}{1}%
{9}{{\textcolor{magenta}{9}}}{1}%
{.0}{{\textcolor{magenta}{.0}}}{2}%
{.1}{{\textcolor{magenta}{.1}}}{2}%
{.2}{{\textcolor{magenta}{.2}}}{2}%
{.3}{{\textcolor{magenta}{.3}}}{2}%
{.4}{{\textcolor{magenta}{.4}}}{2}%
{.5}{{\textcolor{magenta}{.5}}}{2}%
{.6}{{\textcolor{magenta}{.6}}}{2}%
{.7}{{\textcolor{magenta}{.7}}}{2}%
{.8}{{\textcolor{magenta}{.8}}}{2}%
{.9}{{\textcolor{magenta}{.9}}}{2}%
{=}{{\textcolor{red}{=}}}{1}%
{>}{{\textcolor{red}{>}}}{1}%
{<}{{\textcolor{red}{<}}}{1}%
{*}{{\textcolor{red}{*}}}{1}%
%{/}{{\textcolor{red}{/}}}{1}% stuffs up comments
{+}{{\textcolor{red}{+}}}{1}%
{-}{{\textcolor{red}{-}}}{1}%
{\%}{{\textcolor{red}{\%}}}{1}%
{;}{{\textcolor{red}{;}}}{1}%
{,}{{\textcolor{red}{,}}}{1}%
{\&}{{\textcolor{red}{\&}}}{1}%
{(}{{\textcolor{red}{(}}}{1}%
{)}{{\textcolor{red}{)}}}{1}%
{\{}{{\textcolor{red}{\{}}}{1}%
{\}}{{\textcolor{red}{\}}}}{1}%
,
}
\begin{document}
\begin{lstlisting}
#include <stdio.h>
#define ZERO_FAHRENHEIT 32.0
#define SCALTURA 5.0f/9.0f
// il sequente programma converte una temperatura a FAHRENHEIT in CEL.
int main(void)
{
float fahrenheit, celcius;
printf("Immetti temperatura Fahrenheite:");
scanf("%f", &fahrenheit);
celcius=(fahrenheit-ZERO_FAHRENHEIT)*SCALATURA;
printf("L'equivalente Celcius vale: %f", celcius);
return 0;
}
\end{lstlisting}
\end{document}