1

I‘m using the listings package to style my code snippets in my current project, and I’m running into a problem I haven’t been able to wrap my head around. The programming language I’m using is SCL (PLC programming language) and you address the PLCs in- and outputs like this:  %IX0.2. The IDE I’m using then colors the  %  and the address after it in purple. I would like to do the same in my latex project, but don’t know how I could approach this. I’ve already tried to use the morecomment=[s]{}{} option, but it only accepts non whitespace characters as inputs for the delimiters…

Does anyone have an idea on how I could achieve this?

Here’s a minimal working example of my code:

\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}

Tested with MiKTeX 22.7 on Windows 10

3
  • I have found a way how I can work around this issue in my specific case by adding the entire expression to the literate option like this: {\%QX0.0}{{\textcolor{purple}{\%QX0.0}}}{5} for all the instances it’s used in the code. But if anyone finds a cleaner solution that works for all cases I would greatly appreciate it! Aug 22, 2022 at 8:51
  • You should try : morecomment=[s][\color{purple}]{\%}{\ } Aug 22, 2022 at 10:50
  • @F.Pantigny Thank you very much for this suggestion! It kind of works, the issue was that it still colored the . in blue. I’ve fixed this by removing . from the otherkeywords and adding {.}{{\textcolor{blue}{.}}}{1} to the literate option. Thank you very much! Aug 22, 2022 at 12:37

1 Answer 1

1

I suggest using morecomment=[s][\color{purple}]{\%}{\ }, to remove the . from the otherkeywords and to convert . to a letter with alsoletter={.}.

I would remove completely your morekeywords since that option has effect only with words without characters of the category 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}

Output of the above code

3
  • 1
    Thank you for this answer! The only problem with this for me right now that in other code snippets I have, it doesn’t color the . anymore. Is there a way to color it blue again? I’ve managed to do it with by adding {.}{{\textcolor{blue}{.}}}{1} to the literate option, but I wonder if there’s a cleaner solution to still have it treated as a keyword… Aug 22, 2022 at 14:07
  • 1
    You are right! Since you want . to be blue elsewhere, the best method is probably to use the key literate as you suggest! Aug 22, 2022 at 15:57
  • Ok, thank you very much for your help! I will leave it as it is! Aug 23, 2022 at 6:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .