TransWikia.com

Flatfile comma and semicolon hightlight in lstlisting?

TeX - LaTeX Asked by Petter Östergren on July 28, 2021

How do I create a lstlisting language handling Comman and Semicolon separated list.

Would like to highlight the keywords "STOP, START, ATTEMPT" as well as ", , :" (actual comma and semicolon)

My attempt so far

definecolor{eclipseBlue}{RGB}{42,0.0,255}
definecolor{eclipseGreen}{RGB}{63,127,95}
definecolor{eclipsePurple}{RGB}{127,0,85}

lstdefinelanguage{flatfile}{
  morekeywords={STOP, START, ATTEMPT, INTERMEDIATE},
  morekeywords=[2]{,}, % Actual comma
  morekeywords=[3]{:},
  sensitive=false,
}

lstset{
    language={flatfile},
    [..]                  
    keywordstyle=color{eclipsePurple},
    keywordstyle =[2]color{eclipseGreen},
    keywordstyle =[3]color{eclipseBlue},
}

begin{lstlisting}
STOP,YF08,0x00015F3800000002,1919632,GMT+05:30-Calcutta,04/12/2019,09:57:21.7,0,67,73,04/12/2019,09:57:32.5,2,1006,16,VoIP,IP-TO-IP,DEFAULT,,22223333,44445555,,0,,0,,0,22223333,EGRESS_RL,1,YF08:EGRESS_TG,[..]
end{lstlisting}

One Answer

This seems to be quite tricky and got me a bit confused, because commas, colons and the regular keywords all seemingly need to be treated very differently.

Firstly, comma and colon need to be defined using otherkeywords because they are not letters. However, it is not possible to assign a specific color to them when using otherkeywords. Furthermore, it seems that you can define otherkeywords only if morekeywords is called without optional parameter. Maybe I am wrong, but this is what I found. Therefore, I decided to make eclipseGreen the default color for keywords which is then also applied to the strings defined in otherkeywords.

Secondly, I applied eclipsePurple to the regular keywords.

Finally, I defined the colon once again with morekeywords and the third color, which amazingly worked (see also this answer). This seems to be, however, only possible, if the colon is previously defined by otherkeywords. This trick again did not work for the comma though.

Maybe someone else can explain what's going on. Anyways, the regular keywords are now purple, commas are green and colons are blue, which is, hopefully, what you wanted.

documentclass{article}
usepackage{listings, xcolor}
definecolor{eclipseBlue}{RGB}{42,0.0,255}
definecolor{eclipseGreen}{RGB}{63,127,95}
definecolor{eclipsePurple}{RGB}{127,0,85}

lstdefinelanguage{flatfile}{
  morekeywords={},
  otherkeywords={{,},:},
  morekeywords=[2]{STOP,START,ATTEMPT,INTERMEDIATE},
  morekeywords=[3]{:},
  sensitive=false,
}

lstset{
    language={flatfile},
    keywordstyle=color{eclipseGreen},
    keywordstyle=[2]color{eclipsePurple},
    keywordstyle=[3]color{eclipseBlue},
    breaklines
}

begin{document}

begin{lstlisting}[caption={Partial CDR STOP Record}]
STOP,YF08,0x00015F3800000002,1919632,GMT+05:30-Calcutta,04/12/2019,09:57:21.7,0,67,73,04/12/2019,09:57:32.5,2,1006,16,VoIP,IP-TO-IP,DEFAULT,,22223333,44445555,,0,,0,,0,22223333,EGRESS_RL,1,YF08:EGRESS_TG
end{lstlisting}

end{document}

enter image description here

Correct answer by Jasper Habicht on July 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP