我想使用该listings
包在 LaTeX 中编写 C++ 代码。问题是我想更改代码中的颜色数字(整数、双精度数……)。我找到的唯一解决方案是:
\lstdefinestyle{customc}{
belowcaptionskip=1\baselineskip,
breaklines=true,
frame=L,
xleftmargin=\parindent,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
language=C++,
morekeywords={Mat, cvtColor,one},
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{purple!40!black},
stringstyle=\color{orange},
}
\lstset{literate=%
{0}{{{\color{red}0}}}1
{1}{{{\color{red}1}}}1
{2}{{{\color{red}2}}}1
{3}{{{\color{red}3}}}1
{4}{{{\color{red}4}}}1
{5}{{{\color{red}5}}}1
{6}{{{\color{red}6}}}1
{7}{{{\color{red}7}}}1
{8}{{{\color{red}8}}}1
{9}{{{\color{red}9}}}1
}
但是这个解决方案给了我这个:
变量中的一些数字也被赋予了颜色,但我不想这样。有人知道在代码中放入不同颜色数字的另一种形式吗?
答案1
我只知道一些带有转义数字的棘手代码:
\documentclass{report}
\usepackage{listings}
\usepackage{xcolor}
\lstdefinestyle{customc}{
belowcaptionskip=1\baselineskip,
breaklines=true,
frame=L,
xleftmargin=\parindent,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
language=C++,
morekeywords={Mat, cvtColor,one},
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{purple!40!black},
stringstyle=\color{orange},
escapechar=\%,
}
\def\N#1{\textcolor{red}{#1}}
\begin{document}
\begin{lstlisting}[style=customc]
area = moment.m00
Point2f(x,y), %\N1%, %\N1,%
\end{lstlisting}
\begingroup
\catcode`\0=13 \def0{\textcolor{red}{\string0}}
\catcode`\1=13 \def1{\textcolor{red}{\string1}}
\begin{lstlisting}[style=customc]
area = moment.m00
Point2f(x,y), %1%, %1%, %0%,
\end{lstlisting}
\endgroup
\end{document}