I'd like to be able to use inline verbatim text with the following features:
- Line breaks, as provided for example by package
spverbatim
, command\spverb
; - Colored font, as provide by package
color
; - Highlighting (text with colored background), like
soul
's package\hl
command.
By "inline" I mean that the verbatim part is within the normal text (not an environment with its own paragraph), and by "verbatim" I mean that special symbols like \
, _
etc are not be interpreted by LaTeX.
What I'd like to achieve looks like this (using blue as font color and grey as highlighting color), but it should allow a line break if the verbatim part requires one.
I have tried packages color
, newverbs
, spverbatim
and soul
, but I haven't been able to combine all the above features. The closest I've got is:
Tweaking
spverbatim
's definition ofspverb
to include colored font. That way I get features 1 and 2, but not 3:\gdef\myVerb{% \bgroup \color{fgColor}% \let\spverb@ve=\verb@egroup \def\verb@egroup{\spverb@ve\egroup}% \def\@xobeysp{\mbox{}\space}% \verb }
Apparently
\myVerb
defined this way can't be chained withsoul
's packagehl
to use\hl{\myVerb{verbatim text}}
(LaTeX complainsLaTeX Error: \verb illegal in command argument.
). Is there any way to combine them? For example, is there something analogous to\hl
that can be added after\color{fgColor}
in the code above to achieve highlighting?Using the
newverbs
package to define\newverbcommand{\myVerb} {\color{fgColor}\begin{lrbox}{\verbbox}} {\end{lrbox}\colorbox{bgColor}{\usebox{\verbbox}}}
This produces the font colored in
fgColor
and the highlighting withbgColor
, so I get 2 and 3; but not 1: it doesn't allow line breaks. I guess that's because oflrbox
or\colorbox
, but I don't know how to modify this to allow line breaks. Is there any way to do it?I can of course use
\newcommand{\myVerb}[1]{{\ttfamily\color{fgcolor}{\hl{#1}}}}
with
hl
as persoul
package. With this I get 1, 2 and 3 but it's not verbatim, so I have to escape all special characters.
So, is there any way to achieve what I want? Either by solving what's missing in any of the above approaches, or by using a different approach? Perhaps there's a package that supports all the features I want off the shelf?