renewing the \mintinline command to disable italics (or any other alternative)

For the minted environment, you can disable italic comments / preprocessor includes etc by using etoolbox and then doing \AtBeginEnvironment{minted}{\let\itshape\relax}.

Using \let\itshape\relax directly can work as expected

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}

\begin{document}
Regular style: \mintinline[]{cpp}{#include <type_traits>}

Hacky style: {\let\itshape\relax\mintinline[]{cpp}{#include <type_traits>}}

Not italic afterward xD
\end{document}


which produces the expected non-italic version:

However, I want to create a way to basically inject {\let\itshape\relax before and } after the \mintinline calls. It's a complicated command, though, and I think I don't know what I don't know. This is what I believe was my most successful attempt, trying to follow this guide, I added this in the preamble:

\usepackage{letltxmacro}
\makeatletter
\LetLtxMacro{\NewMintinline}{\mintinline}
\let\OldMintinline\mintinline
% not even adding itshape relax, just trying to redefine...
\renewcommand{\mintinline}[2][\newdef]{\OldMintinline[{#1}]{#2}{#3}}
\makeatother


I tried many different variants, but at the end of the day I'm totally lost. I think it's supposed to be 2 arguments, because \mintinline[optional]{lexer}{code} gives lexer and code, but maybe one of those is optional?

Is it possible to in the preamble somehow do some magic to \let\itshape\relax for all the \mintinline? I understand that I could transform every \mintinline in the code to be {\let\itshape\relax\mintinline[]}...but this is less than ideal ;)

Thanks!

Inject the instruction in \mintinline:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}
\usepackage{xpatch}

\xpatchcmd{\mintinline}{\begingroup}{\begingroup\let\itshape\relax}{}{}

\begin{document}

Hacky style: \mintinline[]{cpp}{#include <type_traits>}

Not italic afterward xD

\end{document}


Use a similar idea for minted:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}
\usepackage{xpatch}

\xpatchcmd{\mintinline}{\begingroup}{\begingroup\let\itshape\relax}{}{}
\xpatchcmd{\minted}{\VerbatimEnvironment}{\VerbatimEnvironment\let\itshape\relax}{}{}

\begin{document}

Hacky style: \mintinline[]{cpp}{#include <type_traits>}

\begin{minted}{cpp}
#include <type_traits>
\end{minted}

\end{document}


• Wow, awesome -- thanks! Is there a reason why you suggest this over \AtBeginEnvironment for minted? Or is it just because we're already bringing it in for \mintinline? – svenevs Jan 11 at 14:42
• @svenevs The latter. – egreg Jan 11 at 15:50