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.




#include <stdio.h>
#include <omp.h>
int main (int argc, char *argv[]) {
  #pragma omp parallel
    int tid = omp_get_thread_num();
    printf("Hello from thread %d\n", tid);
  #pragma acc parallel
    printf("Hello from acc\n");
  return 0;

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



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

   {\IfBeginWith{#2}{\FancyVerbBreakStart\PYGZsh{}pragma\activespace omp}%
     {\IfBeginWith{#2}{\FancyVerbBreakStart\PYGZsh{}pragma\activespace acc}%

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.

| improve this answer | |
  • 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

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.