3

Consider the following code:

\documentclass{article}
\usepackage{amssymb,xcolor,listings}
\begin{document}
\lstset{
  mathescape=true,
  basicstyle=\small,
  escapeinside={(*@}{@*)},
  keywordstyle=\color{black}\bfseries\sffamily,
  identifierstyle=\slshape,
  stringstyle=\ttfamily,
  commentstyle=\color{red},
  emphstyle={[1]\bfseries\sffamily},
  emphstyle={[2]\sffamily},
  emphstyle={[3]\color{blue}\sffamily},
  keywords={fun,let,in,if,then,else,fi,elif},
  emph={[1]public,protected,private},
  emph={[2]Tree,FinSet},
  emph={[3]true,false},
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/},
  morecomment=[s]{<<}{>>},
  columns=flexible,
  frame=tb
  %%% ... and a whole bunch of other customization ...
}
\begin{lstlisting}[literate={{α}{$\mathsf{\alpha}$}1},moreemph={[3]all',add,root,left,right,isetree}]
fun all'  =  (s: FinSet α, t: Tree α) FinSet α:
             if isetree(t) then  s
                           else  all'(all'(add(s, root(t)), left(t)), right(t))
             fi(*@\hfill\(\Box\)@*)
\end{lstlisting}
\end{document}

Compiled with pdflatex, the output is great except that all' is not highlighted in blue:

output

How to highlight all' (similar to add, root, ..., i.e., in blue) without changing the contents of the lstlisting environment but changing only the optional argument?

(Yes, of course one can use the escaping mechanism, which, however, would make your LaTeX code slightly less readable each time you use it. Of course, one can put all' into a special-type comment, which would make the LaTeX source code slightly less readable again. I wish to maintain the source-code readabily as much as possible, though: for real-life examples larger than this MWE, the source-code readability significantly pays off, of course, as far as my personal experience is concerned.)

Off-topic: would typesetting such junk get any easier in XeLaTeX, i.e., would I be able to typeset alpha and the QED-box directly without cumbersome options?

4

The problem is that ' is not treated as a letter and thus cannot be part of keywords or emphasized words. This is easy to fix by adding alsoletter={'} to the list of options:

\begin{lstlisting}[
    literate={{α}{$\mathsf{\alpha}$}1},
    moreemph={[3]all',add,root,left,right,isetree},
    alsoletter={'} ]
fun all'  =  (s: FinSet α, t: Tree α) FinSet α:
             if isetree(t) then  s
                           else  all'(all'(add(s, root(t)), left(t)), right(t))
             fi(*@\hfill\(\Box\)@*)
\end{lstlisting}

enter image description here

  • @user49915 Well, if you need both a and α identifiers in your code, you have to add a new comment/escape environment. There's no way to make listings guess correctly which of these identifiers are meant in what place of the code – siracusa Sep 20 '18 at 22:28
  • @user49915 Oops, my text editor messed up the Unicode letters here and replaced each α by a. That's why I thought you were trying to replace a single ASCII a by \alpha whereas you just wanted to replace α. Sorry for the confusion, I removed that part of the answer. – siracusa Sep 20 '18 at 23:12

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.