Listings 包:从特定字符到下一个空格的颜色代码

Listings 包:从特定字符到下一个空格的颜色代码

我正在使用 listings 包来设置当前项目中的代码片段样式,但遇到了一个我无法理解的问题。我使用的编程语言是 SCL(PLC 编程语言),您可以像这样处理 PLC 的输入和输出:   %IX0.2。然后,我使用的 IDE 将 % 及其后的地址涂成紫色。我想在我的 latex 项目中做同样的事情,但不知道该如何处理。我已经尝试过使用该morecomment=[s]{}{}选项,但它只接受非空白字符作为分隔符的输入……

有人知道我该如何实现这个目标吗?

这是我的代码的一个最小工作示例:

\documentclass{report}

\usepackage{listings}
\usepackage{color}

\lstdefinelanguage{scl}{
    keywords = {AND,ANY,ARRAY,AT,BEGIN,BLOCK_DB,BLOCK_FB,BLOCK_FC,BLOCK_SDB,BOOL,BY,BYTE,CASE,CHAR,CONST,CONTINUE,COUNTER,DATA_BLOCK,DATE,DATE_AND_TIME,DINT,DIV,DO,DT,DWORD,ELSE,ELSIF,EN,ENO,END_CASE,END_CONST,END_DATA_BLOCK,END_FOR,END_FUNCTION,END_FUNCTION_BLOCK,END_IF,END_LABEL,END_TYPE,END_ORGANIZATION_BLOCK,END_REPEAT,END_STRUCT,END_VAR,END_WHILE,EXIT,FOR,FUNCTION,FUNCTION_BLOCK,GOTO,IF,INT,LABEL,MOD,NIL,NOT,OF,OK,OR,ORGANIZATION_BLOCK,POINTER,REAL,REPEAT,RETURN,STRING,STRUCT,THEN,TIME,TIMER,TIME_OF_DAY,TO,TOD,TYPE,UNTIL,VAR,VAR_INPUT,VAR_IN_OUT,VAR_OUTPUT,VAR_TEMP,WHILE,WORD,VOID},
    morecomment=[l]{//},
    alsoletter={/},
    morestring=[b]',
    morekeywords={/,*,(,),:=,;,[,],.,+,-,<,>,<=,>=,=,<>},
    otherkeywords={*,(,),:=,;,[,],.,+,-,<,>,<=,>=,=,<>},
    sensitive=false,
    tabsize=4,
    literate=*
    {0}{{\textcolor{brown}{0}}}{1}
    {1}{{\textcolor{brown}{1}}}{1}
    {2}{{\textcolor{brown}{2}}}{1}
    {3}{{\textcolor{brown}{3}}}{1}
    {4}{{\textcolor{brown}{4}}}{1}
    {5}{{\textcolor{brown}{5}}}{1}
    {6}{{\textcolor{brown}{6}}}{1}
    {7}{{\textcolor{brown}{7}}}{1}
    {8}{{\textcolor{brown}{8}}}{1}
    {9}{{\textcolor{brown}{9}}}{1}
    {T\#}{{\textcolor{brown}{T\#}}}{2}
    {MS}{{\textcolor{brown}{MS}}}{2}
    {TRUE}{{\textcolor{brown}{TRUE}}}{4}
    {FALSE}{{\textcolor{brown}{FALSE}}}{5}
}

\definecolor{grey}{rgb}{0.9,0.9,0.9}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{brown}{rgb}{0.7,0.5,0}
\definecolor{darkgreen}{rgb}{0,0.6,0.2}
\definecolor{purple}{rgb}{0.75,0,1}

\lstset{
    backgroundcolor=\color{white},
    basicstyle=\ttfamily,
    keywordstyle=\color{blue},
    stringstyle=\color{brown},
    commentstyle=\color{darkgreen},
    breakatwhitespace=false,
    frame=single,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    tabsize=4
}
\begin{document}

\begin{lstlisting}[basicstyle=\footnotesize\ttfamily,language=scl]
dout_startButtonLED     AT %QX0.0   : BOOL; // Start button LED
dout_stopButtonLED      AT %QX0.1   : BOOL; // Stop button LED
dout_homingButtonLED    AT %QX0.2   : BOOL; // Homing button LED
\end{lstlisting}

\end{document}

在 Windows 10 上使用 MiKTeX 22.7 进行了测试

答案1

我建议使用,从中morecomment=[s][\color{purple}]{\%}{\ }删除,并使用将其转换为字母。.otherkeywords.alsoletter={.}

我会完全删除你的morekeywords因为该选项只对文字有效没有类别 的 特征other.

\documentclass{report}

\usepackage{listings}
\usepackage{color}


\lstdefinelanguage{scl}{
    keywords = {AND,ANY,ARRAY,AT,BEGIN,BLOCK_DB,BLOCK_FB,BLOCK_FC,BLOCK_SDB,BOOL,BY,BYTE,CASE,CHAR,CONST,CONTINUE,COUNTER,DATA_BLOCK,DATE,DATE_AND_TIME,DINT,DIV,DO,DT,DWORD,ELSE,ELSIF,EN,ENO,END_CASE,END_CONST,END_DATA_BLOCK,END_FOR,END_FUNCTION,END_FUNCTION_BLOCK,END_IF,END_LABEL,END_TYPE,END_ORGANIZATION_BLOCK,END_REPEAT,END_STRUCT,END_VAR,END_WHILE,EXIT,FOR,FUNCTION,FUNCTION_BLOCK,GOTO,IF,INT,LABEL,MOD,NIL,NOT,OF,OK,OR,ORGANIZATION_BLOCK,POINTER,REAL,REPEAT,RETURN,STRING,STRUCT,THEN,TIME,TIMER,TIME_OF_DAY,TO,TOD,TYPE,UNTIL,VAR,VAR_INPUT,VAR_IN_OUT,VAR_OUTPUT,VAR_TEMP,WHILE,WORD,VOID},
    morecomment=[l]{//},
    morecomment=[s][\color{purple}]{\%}{\ } ,
    alsoletter={/.},
    morestring=[b]',
    otherkeywords={*,(,),:=,;,[,],+,-,<,>,<=,>=,=,<>},
    sensitive=false,
    tabsize=4,
    literate=*
    {0}{{\textcolor{brown}{0}}}{1}
    {1}{{\textcolor{brown}{1}}}{1}
    {2}{{\textcolor{brown}{2}}}{1}
    {3}{{\textcolor{brown}{3}}}{1}
    {4}{{\textcolor{brown}{4}}}{1}
    {5}{{\textcolor{brown}{5}}}{1}
    {6}{{\textcolor{brown}{6}}}{1}
    {7}{{\textcolor{brown}{7}}}{1}
    {8}{{\textcolor{brown}{8}}}{1}
    {9}{{\textcolor{brown}{9}}}{1}
    {T\#}{{\textcolor{brown}{T\#}}}{2}
    {MS}{{\textcolor{brown}{MS}}}{2}
    {TRUE}{{\textcolor{brown}{TRUE}}}{4}
    {FALSE}{{\textcolor{brown}{FALSE}}}{5}
}

\definecolor{grey}{rgb}{0.9,0.9,0.9}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{brown}{rgb}{0.7,0.5,0}
\definecolor{darkgreen}{rgb}{0,0.6,0.2}
\definecolor{purple}{rgb}{0.75,0,1}

\lstset{
    backgroundcolor=\color{white},
    basicstyle=\ttfamily,
    keywordstyle=\color{blue},
    stringstyle=\color{brown},
    commentstyle=\color{darkgreen},
    breakatwhitespace=false,
    frame=single,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    tabsize=4
}
\begin{document}

\begin{lstlisting}[basicstyle=\footnotesize\ttfamily,language=scl]
dout_startButtonLED     AT %QX0.0   : BOOL; // Start button LED
dout_stopButtonLED      AT %QX0.1   : BOOL; // Stop button LED
dout_homingButtonLED    AT %QX0.2   : BOOL; // Homing button LED
\end{lstlisting}

\end{document}

上述代码的输出

相关内容