5

I have the following snippet of code:

\documentclass{beamer}

% Solarized colors
\definecolor{sbase03}{HTML}{002B36}
\definecolor{sbase02}{HTML}{073642}
\definecolor{sbase01}{HTML}{586E75}
\definecolor{sbase00}{HTML}{657B83}
\definecolor{sbase0}{HTML}{839496}
\definecolor{sbase1}{HTML}{93A1A1}
\definecolor{sbase2}{HTML}{EEE8D5}
\definecolor{sbase3}{HTML}{FDF6E3}
\definecolor{syellow}{HTML}{B58900}
\definecolor{sorange}{HTML}{CB4B16}
\definecolor{sred}{HTML}{DC322F}
\definecolor{smagenta}{HTML}{D33682}
\definecolor{sviolet}{HTML}{6C71C4}
\definecolor{sblue}{HTML}{268BD2}
\definecolor{scyan}{HTML}{2AA198}
\definecolor{sgreen}{HTML}{859900}
\definecolor{myolive}{rgb}{0.51, 0.59, 0.0}

\usepackage{listings}

\lstset{
    % How/what to match
    sensitive=true,
    % Border (above and below)
    frame=tlbr,
    framesep=2pt, % frame border width
    framerule=0pt,% frame border line width
    % Extra margin on line (align with paragraph)
    xleftmargin=\parindent,
    % Put extra space under caption
    belowcaptionskip=1\baselineskip,
    % Colors
    backgroundcolor=\color{sbase3},
    basicstyle=\color{sbase00}\ttfamily,
    keywordstyle=\color{myolive},
    commentstyle=\color{sbase1},
    stringstyle=\color{sblue},
    numberstyle=\color{sviolet}\tiny,
    identifierstyle=\color{sbase02},
    % Break long lines into multiple lines?
    breaklines=true,
    % Show a character for spaces?
    showstringspaces=false,
    tabsize=2
}

\usepackage{fontspec}
\usepackage{microtype}
% we need to define this because otherwise listings won't
% pick up style changes
\newfontface\UMono[Scale=MatchUppercase]{UbuntuMono}

\makeatletter
\lst@InstallKeywords k{attributes}{attributestyle}\slshape{attributestyle}{}ld
\makeatother

\lstdefinestyle{cpp}{
  language=C++,
  basicstyle=\fontsize{7}{7}\UMono,
  alsoletter={\#}, % doesn't work
  moreattributes={main,\#include}, % #include doesn't work
  attributestyle = \color{sblue},
}

\begin{document}

\begin{lstlisting}[style=cpp]
#include <iostream>

int add(int x, int y){
  return x + y;
}

int main(){
  int a = add(1,3);
  std::cout << a << std::endl;
}
\end{lstlisting}

\end{document}

Which renders the following:

enter image description here

I would like to make #include <stuff> (and #include "stuff.h") to be of different color/treated as different type of keyword.

The closest I have come to this is to use:

literate={\#include}{{\textcolor{sorange}{\#include}}}7,

in my style definition but it only colors #include.

enter image description here

How can I achieve that? I use luatex if that matters.

7

You can define a new line comment style for all lines starting with #. The style format then is applied up to the end of the line. The relevant changes to your document are:

\lstdefinestyle{cpp}{
  language=C++,
  basicstyle=\fontsize{7}{7}\UMono,
  alsoletter={\#}, % doesn't work
  moreattributes={main},              % <-- Here
  morecomment=[l][\color{scyan}]{\#}, % <-- and here
  attributestyle = \color{sblue},
}

In a slightly modified sample code it looks like this (please ignore the fact that I don't have your font installed on my system):

enter image description here

Also note that using # as an operator in your code (not sure this is even allowed in C++) would make the rest of the line be highlighted as if it were a preprocessor pragma. I don't know a workaround for that other than using a different symbol and making it look like a # via the literate key.

  • I don't think you can use # as an operator in C++. Anyway, if that were the case and someone is using it, they will deserve the consequences... – Rmano Nov 7 '16 at 11: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.