2

I use \inputminted to include snippets of C code in my document. These snippets are nicely coloured.

I know I can write e.g. \textcolor{red}{foo} but instead of red I want to say "the same colour which inputminted uses for a variable/type/macro/comment".

How can I colour text to match what \inputminted does?

  • just look in the _minted directory and see the generated latex for the file and use the same colours – David Carlisle Jan 17 '17 at 17:06
2
\documentclass{article}

\usepackage{minted}

\begin{document}
\begin{minted}{C}
if(!foo){
 for(zz=0;zz<-1;zz++) {
    a=b;
 }
}
\end{minted}
\end{document}

produces

enter image description here

If you look in the directory _minted-jobname then you will see the latex version of the file (or environment in this case) in a file with a very long unique name.

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k}{if}\PYG{p}{(}\PYG{o}{!}\PYG{n}{foo}\PYG{p}{)\PYGZob{}}
 \PYG{k}{for}\PYG{p}{(}\PYG{n}{zz}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{n}{zz}\PYG{o}{\PYGZlt{}\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{n}{zz}\PYG{o}{++}\PYG{p}{)} \PYG{p}{\PYGZob{}}
    \PYG{n}{a}\PYG{o}{=}\PYG{n}{b}\PYG{p}{;}
 \PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

Looking at that you can see for eaxmple that keywords such as if are set with \PYG{k} and operators such as = are set with \PYG{o}.

In default-pyg-prefix.pygstyle in the same directory:

\expandafter\def\csname PYG@tok@o\endcsname
   {\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}

and

\expandafter\def\csname PYG@tok@k\endcsname
   {\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}

so the colours we need are \textcolor[rgb]{0.40,0.40,0.40} and \textcolor[rgb]{0.00,0.50,0.00}

enter image description here

\documentclass{article}

\usepackage{minted}

\begin{document}
\begin{minted}{C}
if(!foo){
 for(zz=0;zz<-1;zz++) {
    a=b;
 }
}
\end{minted}

roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue},
keywords are \textcolor[rgb]{0.00,0.50,0.00}{keyword colour} and
operators are \textcolor[rgb]{0.40,0.40,0.40}{operator grey}.

\end{document}

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.