2

I'm able to compile the following in Overleaf for the desired inline code-coloring effect:

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\definecolor{codeblue}{rgb}{0,0.3,0.6}
\newmintinline[bluecode]{c++}{\color{codeblue}}

\begin{document}

This is a blue \bluecode{keyword} and this is a normal \mintinline[]{C++}{keyword}.

Which produces:

enter image description here

Yet I have the following errors: Missing \endcsname inserted. and Package keyval Error: \color {codeblue} undefined. I need these errors to go away so I can upload to arXiv. How can I correct this, or otherwise color inline code?

4
  • Background color is straightforward (for example) but this is not what I'm looking for. Dec 10, 2018 at 0:35
  • The last argument of \newmintinline isn't supposed to store arbitrary code but to pass in key=value pairs defined by the minted package.
    – Skillmon
    Dec 10, 2018 at 0:48
  • Any reason why you're defining something for c++ and then using C++?
    – Werner
    Dec 10, 2018 at 1:03
  • @Skillmon correct. Yet this produces the desired color effect, and there is not a color key. Dec 10, 2018 at 1:48

2 Answers 2

2

The following patches \mintinline to place the \color inside of it. Note that this only changes the colour of things that are not coloured by the minted package (you can see this in the output of %). The patch doesn't kill the verbatim reading of the argument.

\documentclass[]{article}

\usepackage{xcolor}
\definecolor{codeblue}{rgb}{0,0.3,0.6}
\usepackage{minted}
\usepackage{etoolbox}
\newif\ifBlueCode
\BlueCodetrue
\newcommand*\mybluecode
  {%
    \ifBlueCode
      \global\BlueCodefalse
      \color{codeblue}%
    \fi
  }
\newrobustcmd*\bluecode[1][]
  {%
    \BlueCodetrue
    \mintinline[#1]{C++}%
  }
\expandafter\patchcmd\csname\string\mintinline\endcsname
  {\begingroup}{\begingroup\mybluecode}{}
  {\GenericError{}{Patching of \string\mintinline\space failed!}{}{}}

\begin{document}
This is a blue \bluecode{keyw%rd} and this is a normal
\mintinline[]{C++}{keyw%rd}.
\end{document}

enter image description here

To also suppress the colours placed by minted, you can change the definition of \mybluecode:

\makeatletter
\def\@gobble@undeclaredcolor[#1]#2{}
\newcommand*\mybluecode
  {%
    \ifBlueCode
      \global\BlueCodefalse
      \color{codeblue}%
      \let\@undeclaredcolor\@gobble@undeclaredcolor
      \let\@declaredcolor\@gobble
    \fi
  }
\makeatother
5
  • Works great, and thank you for the details! Dec 10, 2018 at 16:14
  • 2
    However compiling seems to fail when I try to use the minted frozencache option: p12 of the minted docs. The error is ! Package minted Error: Missing style definition for with frozencache. and points to the first instance of \bluecode in my document. Any ideas? Dec 10, 2018 at 18:40
  • 1
    @BoltzmannBrain never used that option and currently I'm busy with other things. I'll take a look at it sometime later this day (if I don't forget about it, feel free to ping me with a comment if you get the feeling I've forgot you)
    – Skillmon
    Dec 11, 2018 at 8:41
  • @BoltzmannBrain can you provide a MWE in which you have a setup that works without my patch? If I do the idiomatic thing I get errors with and without my patch.
    – Skillmon
    Dec 11, 2018 at 15:57
  • 1
    @BoltzmannBrain Ok, just tried again, this time actually reading the manual. You have to use the finalizecache option and compile once before you use the frozencache option. It works fine with my patch applied.
    – Skillmon
    Dec 12, 2018 at 11:34
0

A possible solution may be to write a wrapper command that first changes the color and then calls \mintinline. Make sure to do the color change within a group (extra {}) otherwise the color change can affect the text outside of the command.

MWE:

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}
\definecolor{codeblue}{rgb}{0,0.3,0.6}
\newcommand{\mybluecode}[1]{%
{\color{codeblue}%
\mintinline[]{C++}{#1}}%
}
%\newmintinline[bluecode]{c++}{\color{codeblue}}

\begin{document}

This is a blue \mybluecode{keyword} and this is a normal \mintinline[]{C++}{keyword}.
\end{document}
1
  • 1
    This changes the effect of \mintinline to be almost useless. You're preventing it from changing any catcodes of #1.
    – Skillmon
    Dec 10, 2018 at 9:52

You must log in to answer this question.

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