1

I am trying to type set some OPL (Optimization Programming Language) code using the listingspackage. It turns out, that it looks a lot like Java, so I use Java like a "base". However, some keywords, which I need are not present in Java. I figured out that the keyword dvar could be added using

\lstset{
  morekeywords={dvar}
}

However, I also need to add keywords int+ and float+ where the "+" is important as well. If I do

\lstset{
      morekeywords={dvar,int+}
    }

I do not achieve anything as it seems that the "+" is ignored. From the following

\begin{lstlisting}
int anInt = 25; // Declaration of an integer storing the value 25
dvar int+ hej;  // Declaration of a non-negative integer valued decision variable
\end{lstlisting}

I get the output

enter image description here

where it seems the "+" is typeset like an ordinary operator in code and not together with int as in int+. How can I add a new keyword that includes a ``+''?


For completeness, here goes a (minimal) working example

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings,lstautogobble}
\definecolor{napiergreen}{rgb}{0.16, 0.5, 0.0}
\lstset{language=Java,
    keywordstyle=\color{blue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\color{napiergreen},
    stringstyle=\ttfamily,
    rulecolor=\color{black},
    autogobble=true,    
    morekeywords={dvar,int+}
}
\begin{document}
    \begin{lstlisting}
        int+ anInt = 25; // Declaration of an integer storing the value 25
        dvar int+ hej;  // Declaration of a non-negative integer valued decision variable
    \end{lstlisting}
\end{document}
2
  • This is a good question, but it would be really helpful if you could put your code fragments into a complete compilable document that people could play with.
    – Alan Munn
    Aug 5, 2020 at 13:52
  • @Alan Munn thank you. I will add a MWE asap
    – Sune
    Aug 5, 2020 at 16:24

1 Answer 1

2

You can tell listings to treat + as a letter for the purposes of the language definition, by using the alsoletter key. As you can see by the examples below, only + in the defined keywords is highlighted.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}
\begin{lstlisting}[language=java,alsoletter={+},morekeywords={int+,dvar}, keywordstyle={\color{blue}},]
int+ anInt = 25; // Declaration of an integer storing the value 25
dvar int+ hej;  // Declaration of a non-negative integer valued decision variable
dvar+  foo+; 
int + foo;
int+ + foo+;
\end{lstlisting}

\end{document}

output of code

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.