I want to highlight code inline with a gray background. Preferably with listings
.
This question is related to Colored background in inline listings.
But as my MWE shows it doesn't work with line breaks. Is there a way to fix this? I also tried the soul package, but this produces a lot of errors (comments in MWE):
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{soul}
\lstdefinestyle{ShellCmd}{
language=bash,
basicstyle=\small\ttfamily,
backgroundcolor=\color{gray!10},
linewidth=0.9\linewidth,
breaklines=true
}
\def\ownstrut{\vrule height5.5pt depth0.8pt width0pt}
\newcommand\shellcmd[1]{\colorbox{gray!10}{\lstinline[style=ShellCmd,mathescape]`#1`}}
%\newcommand\shelltwo[1]{\hl{\lstinline[style=ShellCmd,mathescape]`#1`}} % this produces error
\begin{document}
\shellcmd{command}, some text \shellcmd{a very long command which should not destroy the whole layout when a line break occurs! Maybe it is possible to make even three or more lines}, and here it continues.
%\shelltwo{a very long command which should not destroy the whole layout when a line break occurs! Maybe it is possible to make even three or more lines}
\end{document}
Best Answer
The problem is that
\colorbox{}
is a box, that means you can't have a newline inside.You can try to use
\parbox
but that will also not do what you want (see\shellthree
).You can solve your problem with package
soul
and\texttt
as shown at the last three lines in the following MWE:I use package
soul
, change the color with\colorlet
, set the color with\sethlcolor
and usetexthl{texttt{}}
to set the text. This works also for line breaks.