minted and different coloured pragmas

I try to see if is possible to use minted to colour #pragma statements differently based on what comes after the pragma, omp, acc.

I am aware of this Highlight #pragma in code listing using minted but this is not fit for my purpose. I have close to little knowledge on how pygmentize works, so any thoughts will be appreciated.

\documentclass{article}
\usepackage{luatex85}
\usepackage{shellesc}
\usepackage{minted}
\newminted{c}{fontsize=\footnotesize,bgcolor=lightgray}

\AtBeginDocument{%
\let\originalpyg\PYGdefault
\renewcommand{\PYGdefault}[2]{%
\ifstrequal{#1}{c+cp}%
{\IfBeginWith{#2}{\PYGZsh{}pragma}%
{\originalpyg{#1}{\color{red}{#2}}}%
{\originalpyg{#1}{#2}}}%
{\originalpyg{#1}{#2}}%
}%
}

\begin{document}

\begin{ccode}
#include <stdio.h>
#include <omp.h>
int main (int argc, char *argv[]) {
#pragma omp parallel
{
}
#pragma acc parallel
{
printf("Hello from acc\n");
}
return 0;
}
\end{ccode}
\end{document}


to build

lualatex -shell-escape color.tex


I have already tried the obvious, adding omp after pragma but seems somehow only pragma is passed to the string.

what will be nice to achieve is #pragma omp and #pragma acc to be coloured differently

Alin

This should do what you want. Just replace what you currently have in the preamble.

\makeatletter
\minted@checkstyle{default}
\let\originalpyg\PYGdefault
\newcommand{\activespace}{\FV@Space}
\renewcommand{\PYGdefault}[2]{%
\ifstrequal{#1}{c+cp}%
{\IfBeginWith{#2}{\FancyVerbBreakStart\PYGZsh{}pragma\activespace omp}%
{\originalpyg{#1}{\color{red}{#2}}}%
{\IfBeginWith{#2}{\FancyVerbBreakStart\PYGZsh{}pragma\activespace acc}%
{\originalpyg{#1}{\color{green}{#2}}}%
{\originalpyg{#1}{#2}}}}%
{\originalpyg{#1}{#2}}%
}%
\makeatother


The old, linked example no longer works with current versions of minted. And since this new code is also hacking the internals, it may also break in the future.

The \minted@checkstyle{default} is required to make sure that the default style, with \PYGdefault, exists. In recent versions of minted, the styles are only created within commands/environments immediately before use, rather than at the end of the preamble. \minted@checkstyle forces style definition immediately.

The \activespace is required to make the comparison recognize spaces, since spaces are treated specially in verbatim contexts defined using fancyvrb.

The \FancyVerbBreakStart is inserted at the start of the second argument of every \PYG<style> command by the fvextra package. It makes line breaking possible.

• thank you @g-poore very much, does indeed what I need and open posibilities for more. tested it with what looks like version 2.4.1 of minted. – Alin Marin Elena Jan 12 '17 at 11:15