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
{\%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!morecomment=[s][\color{purple}]{\%}{\ }
.
in blue. I’ve fixed this by removing.
from theotherkeywords
and adding{.}{{\textcolor{blue}{.}}}{1}
to theliterate
option. Thank you very much!