列表包:格式化语言定义中的所有数字?

列表包:格式化语言定义中的所有数字?

已显示如何格式化代码中的所有数字这里。我对其进行了修改,将其包含在语言定义中:

\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);
    }
\end{lstlisting}
\end{document}

产生以下错误:

! Argument of \lst@FillFixed@ has an extra }.

看起来好像它不喜欢我的单身{}

如何正确纳入它?

答案1

在第二个参数中使用一对额外的括号:

\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);
    }
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容