3

I'm trying to create a new language definition with operators => and >.

Highlighting both => and > doesn't seem to work.

I have tried putting them in otherkeywords in both orders. Neither seem to work.

In the code below, only the > is highlighted. Is it possible to get them both highlighting?

Thanks

\documentclass[a4]{report}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{listings}

\definecolor{backcolour}{RGB}{242,242,0235}
\definecolor{codegray}{RGB}{128,128,128}
\definecolor{codeOrange}{RGB}{254, 97, 0}
\definecolor{codeBlue}{RGB}{100, 143, 255}

\lstdefinestyle{newStyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codeBlue},
    keywordstyle=[0]\color{codeBlue},
    keywordstyle=[1]\color{codeBlue},
    keywordstyle=[2]\color{codeBlue},
    keywordstyle=[3]\color{codeBlue},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codeOrange},
    basicstyle=\ttfamily,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                                                 
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=4
}

\lstdefinelanguage{newLanguage}{
    keywords={type,def,True,False},
    otherkeywords={!,\$,|,<,[,],::,=>,>},
    otherkeywords={>},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[n]{/*}{*/},
    morestring=[b]",
    morestring=[b]"""
}


\begin{document}
\begin{lstlisting}[language=newLanguage,style=newStyle]
a = [1,>,2]
b => c
\end{lstlisting}

\end{document}
2

Support for the otherkeywords option is "a bit buggy" at the moment if the same character occurs in different keywords (see also this question).

You can check the behavior for different keyword lists:

otherkeywords={!,\$,|,<,[,],::,>}

enter image description here

otherkeywords={!,\$,|,<,[,],::,=>}

enter image description here

Those are correct. But as soon as you add both, things go wrong:

otherkeywords={!,\$,|,<,[,],::,=>,>}

enter image description here

As a workaround you could use the literate option to set the style for all literate occurrences of => explicitly:

otherkeywords={!,\$,|,<,[,],::,>},
literate={=>}{{\textcolor{codeBlue}{=}}{\textcolor{codeBlue}{>}}}{2}

enter image description here

The drawback of this approach is that => will be replaced in all places, so operators like ==> will be apprear incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.