# How can I patch minted to use souls higlighting

I'd like to highlight code with a gray background like StackExchange does. I know that I can set bgcolor in minted to put a solid background color on inline code.

However, this makes minted use colorbox which doesn't wrap across long lines like markdown does:

Here is a normal sentence that is short. here_is_a_very_long + code_snippet + that_ends_up + wrapped. The box does not extend into the right margin.

My understanding is that the soul package provides exactly this type of highlighting. My naive attempt to use it, building off the approach in https://tex.stackexchange.com/a/276750/41112, was something like:

\documentclass{article}
\usepackage{xpatch}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{soul}

\definecolor{bg}{rgb}{0.95,0.95,0.95}
\setminted{breaklines=true}
\newmintinline[python]{python}{bgcolor=bg}

% this is the bit of code in question
\makeatletter
\xpatchcmd{\minted@inputpyg@inline}{%
\colorbox%
}{%
\long\def\color@b@x##1##2##3%
{\sethlcolor{##2}\hl{##3}}%
\colorbox%
}{\typeout{Success}}{\typeout{Failure}}
\makeatother

\begin{document}
Here is a normal sentence that is short.
\python{here_is_a_very_long + code_snippet + that_ends_up + wrapped}.
The box should not extend into the right margin.
\end{document}


Unfortunately, this appears to send LaTeX into an infinite loop; presumably because hl is using colorbox under the hood complains about a missing.

Does this approach look possible? Is there a way to restore the old meaning of \color@b@x inside its replacement, so that this recursion is avoided?

Unfortunately, this complains about an extra }. Does this approach look possible? What is going wrong here?

• you should show a complete example, that makes it much easier to test. But generally I would avoid soul, it is rather fragile, and try with lualatex and the lua-ul package. Apr 25 at 12:10
• @gusbrs: The breakable library for tcolorbox doesn't appear to handle breaking across lines of text, but only breaking vertically across columns. Is there another feature of tcolorbox I'm missing?
– Eric
Apr 25 at 12:25
• @UlrikeFischer: Updated with a full example, seems the error is different to what it was in my full document.
– Eric
Apr 25 at 12:37
• @Eric Yes, indeed, I had deleted the comment 'cause I saw I had misread the question. Sorry. :) Apr 25 at 16:56

I don't think that it can work with soul, but with lualatex and lua-ul this seems to work (but be aware that code often can't break well over lines):

\documentclass{article}
\usepackage{xpatch}
\usepackage{minted}
\usepackage{xcolor}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\usepackage{iftex}

\usepackage{luacolor}
\usepackage{lua-ul}

\setminted{breaklines=true}
\newmintinline[python]{python}{bgcolor=bg}

% this is the bit of code in question
\makeatletter
\newcommand\myhl[2]{\highLight[#1]{#2}}

\xpatchcmd{\minted@inputpyg@inline}{%
\colorbox%
}{%
\myhl%
}{\typeout{Success}}{\fail}
\makeatother

\begin{document}

Here is a normal sentence that is short.
\python{here_is_a_very_long + code_snippet + that_ends_up + wrapped}.
The box should not extend into the right margin.
\end{document}