5

Is there a possibility to preserve the colours of minted's syntax highlighting, even if I load the xcolor package with the option gray?

Consider the following MWE (requires -shell-escape to compile due to minted):

\documentclass{article}
\usepackage[gray]{xcolor}
%\usepackage[cmyk]{xcolor}
\usepackage{minted}
\begin{document}

\textcolor{blue}{This text shall be blue.}

\begin{minted}{python}
def foo(a):
    return a
\end{minted}
\end{document}

If I comment out/remove the \usepackage[gray]{xcolor} but instead remove the comment before \usepackage[cmyk]{xcolor} I get the following result (as expected):

Screenshot of the PDF result when xcolor is loaded with cmyk mode

Similarly, if I use the aforementioned MWE as provided, I get the result:

Screenshot of the PDF result when xcolor is loaded with gray mode

While this is totally expected by the way minted works, I would still like to have the syntax highlighting of the code snippet as in the first screenshot while all other colours in the document are printed in gray scales.

1 Answer 1

8

you can change the color model locally. But you then need to consider color conversion. If you load with the gray model, the standard colors like red or blue will be defined as gray, and so can't be converted back to rgb or cmyk later. With the option xcolor also sets \convertcolorsUtrue and \convertcolorsDtrue, so it will convert colors both at definition time and usage time to the active model. That means that mycolorA is defined as a gray color, and so can't be converted back to cmyk, but mycolorB is in cmyk (due to the ungrouped \selectcolormodel{cmyk} in the cmd/mintinline/before hook) and can switch.

\documentclass{article}
\usepackage[gray]{xcolor}
\usepackage{minted}
\AddToHook{env/minted/begin}{\selectcolormodel{cmyk}}
\AddToHook{cmd/mintinline/before}{\selectcolormodel{cmyk}}
\begin{document}

\textcolor{blue}{This text shall be blue.}

\begin{minted}{python}
def foo(a):
    return a
\end{minted}

\definecolor{mycolorA}{rgb}{1,0,0}
\textcolor{mycolorA}{This text shall be in mycolorA.}

\mintinline{python}{def foo (a):}

\textcolor{mycolorA}{This text shall be mycolorA.}

\definecolor{mycolorB}{rgb}{1,0,0}
\textcolor{mycolorB}{This text shall be in mycolorB.}

\end{document}

If you need the standard colors also in cmyk, consider to use

\usepackage[cmyk]{xcolor}
\selectcolormodel{gray}

enter image description here

3
  • Thanks for the quick answer @Ulrike. For documentation reasons, I shall add that \AddToHook{cmd/mintinline/begin}{\selectcolormodel{cmyk}} achieves the same for \mintinline. Commented May 24 at 8:14
  • 1
    well no. There is no cmd/mintinline/begin hook, the name is cmd/mintinline/before but your error shows a flaw in my answer. I will adjust. Commented May 24 at 8:48
  • My bad, I did it correctly when I was trying the thing out but failed to bring the correct code to the browser window—too many windows open on this machine... Commented May 24 at 9:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .