列出包:彩色数字,但变量名没有彩色

列出包:彩色数字,但变量名没有彩色

如何用不同颜色格式化所有数字已在邮政。

但是,这也会为变量名中的数字着色。有没有办法避免这种情况?

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    literate={0}{{\textcolor{blue}{0}}}{1}%
         {1}{{\textcolor{blue}{1}}}{1}%
         {2}{{\textcolor{blue}{2}}}{1}%
         {3}{{\textcolor{blue}{3}}}{1}%
         {4}{{\textcolor{blue}{4}}}{1}%
         {5}{{\textcolor{blue}{5}}}{1}%
         {6}{{\textcolor{blue}{6}}}{1}%
         {7}{{\textcolor{blue}{7}}}{1}%
         {8}{{\textcolor{blue}{8}}}{1}%
         {9}{{\textcolor{blue}{9}}}{1}%
         {.0}{{\textcolor{blue}{.0}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.2}{{\textcolor{blue}{.2}}}{1}%
         {.3}{{\textcolor{blue}{.3}}}{1}%
         {.4}{{\textcolor{blue}{.4}}}{1}%
         {.5}{{\textcolor{blue}{.5}}}{1}%
         {.6}{{\textcolor{blue}{.6}}}{1}%
         {.7}{{\textcolor{blue}{.7}}}{1}%
         {.8}{{\textcolor{blue}{.8}}}{1}%
         {.9}{{\textcolor{blue}{.9}}}{1}%
         {\ }{{ }}{1}% handle the space
         ,%
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName1=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

答案1

发生这种情况是因为您的语言定义目前太“差”;您没有声明字符串、关键字、注释……根据您语言的特性,有几种选择:您可以使用带星号的 literate 版本,这样您声明的替换就不会在字符串和注释上进行,并定义一些适当的字符串。在下面的例子中,_(和之间的所有内容_=将被视为字符串:

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    literate=*{0}{{\textcolor{blue}{0}}}{1}%
         {1}{{\textcolor{blue}{1}}}{1}%
         {2}{{\textcolor{blue}{2}}}{1}%
         {3}{{\textcolor{blue}{3}}}{1}%
         {4}{{\textcolor{blue}{4}}}{1}%
         {5}{{\textcolor{blue}{5}}}{1}%
         {6}{{\textcolor{blue}{6}}}{1}%
         {7}{{\textcolor{blue}{7}}}{1}%
         {8}{{\textcolor{blue}{8}}}{1}%
         {9}{{\textcolor{blue}{9}}}{1}%
         {.0}{{\textcolor{blue}{.0}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.2}{{\textcolor{blue}{.2}}}{1}%
         {.3}{{\textcolor{blue}{.3}}}{1}%
         {.4}{{\textcolor{blue}{.4}}}{1}%
         {.5}{{\textcolor{blue}{.5}}}{1}%
         {.6}{{\textcolor{blue}{.6}}}{1}%
         {.7}{{\textcolor{blue}{.7}}}{1}%
         {.8}{{\textcolor{blue}{.8}}}{1}%
         {.9}{{\textcolor{blue}{.9}}}{1}%
         {\ }{{ }}{1}% handle the space
         ,%
    string=[s]{_}{=},
    string=[s]{_}{(},
    stringstyle=\ttfamily,
    showstringspaces=false
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName1=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

在此处输入图片描述

但这可能不是最好的方法,这取决于您尝试定义的语言(也许这种方法会涉及考虑很多不可行的情况,或者某些不应被视为字符串的对象会被错误地视为字符串)。

另一种选择是转义那些您不想替换的数字;例如,假设!在您的语言中没有任何特定含义,您可以说:

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    literate=*{0}{{\textcolor{blue}{0}}}{1}%
         {1}{{\textcolor{blue}{1}}}{1}%
         {2}{{\textcolor{blue}{2}}}{1}%
         {3}{{\textcolor{blue}{3}}}{1}%
         {4}{{\textcolor{blue}{4}}}{1}%
         {5}{{\textcolor{blue}{5}}}{1}%
         {6}{{\textcolor{blue}{6}}}{1}%
         {7}{{\textcolor{blue}{7}}}{1}%
         {8}{{\textcolor{blue}{8}}}{1}%
         {9}{{\textcolor{blue}{9}}}{1}%
         {.0}{{\textcolor{blue}{.0}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.2}{{\textcolor{blue}{.2}}}{1}%
         {.3}{{\textcolor{blue}{.3}}}{1}%
         {.4}{{\textcolor{blue}{.4}}}{1}%
         {.5}{{\textcolor{blue}{.5}}}{1}%
         {.6}{{\textcolor{blue}{.6}}}{1}%
         {.7}{{\textcolor{blue}{.7}}}{1}%
         {.8}{{\textcolor{blue}{.8}}}{1}%
         {.9}{{\textcolor{blue}{.9}}}{1}%
         {\ }{{ }}{1}% handle the space
         ,%
    escapeinside={!!}
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName!1!=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

在此处输入图片描述

另一个选项(与上一个类似)是使用以下方式设置mathescape=true变量名中的数字并将其括起来$...$

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    literate=*{0}{{\textcolor{blue}{0}}}{1}%
         {1}{{\textcolor{blue}{1}}}{1}%
         {2}{{\textcolor{blue}{2}}}{1}%
         {3}{{\textcolor{blue}{3}}}{1}%
         {4}{{\textcolor{blue}{4}}}{1}%
         {5}{{\textcolor{blue}{5}}}{1}%
         {6}{{\textcolor{blue}{6}}}{1}%
         {7}{{\textcolor{blue}{7}}}{1}%
         {8}{{\textcolor{blue}{8}}}{1}%
         {9}{{\textcolor{blue}{9}}}{1}%
         {.0}{{\textcolor{blue}{.0}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.2}{{\textcolor{blue}{.2}}}{1}%
         {.3}{{\textcolor{blue}{.3}}}{1}%
         {.4}{{\textcolor{blue}{.4}}}{1}%
         {.5}{{\textcolor{blue}{.5}}}{1}%
         {.6}{{\textcolor{blue}{.6}}}{1}%
         {.7}{{\textcolor{blue}{.7}}}{1}%
         {.8}{{\textcolor{blue}{.8}}}{1}%
         {.9}{{\textcolor{blue}{.9}}}{1}%
         {\ }{{ }}{1}% handle the space
         ,%
    mathescape=true
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName$1$=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

答案2

使用escapeinside是一个很好的快速技巧,但对我来说,它会弄乱字符的宽度,因此行的其余部分会错位。对我有用的是使用moredelim此处描述的技巧:

如何使用 columns=fixed 和 color 来修复 lstlisting 中的列对齐?

本质上你将以下内容添加到你的语言定义中:

moredelim=[is][\color{black}]{[*}{*]},

然后,您可以用任何错误颜色的东西包围它[*,并*]确保它被涂成黑色。

相关内容