3

I'm going to give a talk on OpenMP soon and I'm currently preparing my beamer slides. Since a lot of the OpenMP magic happens inside of #pragma instructions, I'd like to highlight those in some bright color (but still highlight the rest of the code, too). I'm using the minted package for code listings. Currently, the code below will result in the #include statements and the #pragma line being highlighted in the same yellowish color. Let's say I want the #pragma omp parallel in bright red while keeping everything else as is.

\documentclass{beamer}
\usepackage{minted}
\newminted{c}{fontsize=\footnotesize,bgcolor=lightgray}
\begin{document}
\begin{frame}[fragile]
\begin{ccode}
#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);
    }
    return 0;
}
\end{ccode}
\end{frame}
\end{document}

Compile with xelatex -shell-escape test.tex. Result:

enter image description here

  • 1
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – user36296 Apr 18 '16 at 16:35
  • You're absolutely right, of course. Done. – Jonas Greitemann Apr 18 '16 at 17:35
4

Update 2017-01-11: This no longer works with recent versions of minted. There is a more recent, similar solution that currently does work.

It's possible to do this by modifying the underlying Pygments style macros. This will work with recent versions of minted, but since it is modifying the internals, it may not always work in the future. You would need to replace the default in \PYGdefault with a different style name if you want to use a different highlighting style. Also, if you want to use a different style, you would need to have a \usemintedstyle in the preamble.

enter image description here

\documentclass{article}
\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
    {
        int tid = omp_get_thread_num();
        printf("Hello from thread %d\n", tid);
    }
    return 0;
}
\end{ccode}
\end{document}
  • Thank you very much for taking the time to figure this out, and for creating such a powerful package in the first place. – Jonas Greitemann Apr 19 '16 at 13:41

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.