我可以在 Overleaf 中编译以下内容以获得所需的内联代码着色效果:
\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}.
其结果为:
但是我遇到了以下错误:Missing \endcsname inserted.
我Package keyval Error: \color {codeblue} undefined.
需要消除这些错误,这样我才能上传到 arXiv。我该如何纠正这个问题,或者以其他方式给内联代码上色?
答案1
以下补丁\mintinline
将其放置\color
在其中。请注意,这只会更改未被包着色的内容的颜色minted
(您可以在 的输出中看到这一点%
)。补丁不会消除参数的逐字读取。
\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}
为了抑制 所放置的颜色minted
,您可以更改 的定义\mybluecode
:
\makeatletter
\def\@gobble@undeclaredcolor[#1]#2{}
\newcommand*\mybluecode
{%
\ifBlueCode
\global\BlueCodefalse
\color{codeblue}%
\let\@undeclaredcolor\@gobble@undeclaredcolor
\let\@declaredcolor\@gobble
\fi
}
\makeatother
答案2
一种可能的解决方案是编写一个包装器命令,首先更改颜色,然后调用\mintinline
。确保在组内(额外的{}
)进行颜色更改,否则颜色更改可能会影响命令外的文本。
梅威瑟:
\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}