# Background color in inline code listings with linebreak

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}


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:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor,showframe}
\usepackage{soul}

\lstdefinestyle{ShellCmd}{
language=bash,
basicstyle=\small\ttfamily,
backgroundcolor=\color{gray!10},
linewidth=0.9\linewidth,
breaklines=true
}

\newcommand\shellthree[1]{\colorbox{gray!10}{\parbox[t]{\textwidth}{\lstinline[style=ShellCmd,mathescape]#1}}}

\begin{document}

\lstinline[{backgroundcolor=\color{gray!50}}]{Testzeile testzeile testzeile}
\lstinline[style=ShellCmd]{Testzeile testzeile testzeile}

Command some text \shellthree{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. and here it continues and here it continues and here it continues

\colorlet{hellgrau}{gray!10}
\sethlcolor{hellgrau}
Text Text Text Text Text Text Text Text Text Text Text Text \texthl{\texttt {text highlight with soul}}.

\end{document}


I use package soul, change the color with \colorlet, set the color with \sethlcolor and use texthl{texttt{}} to set the text. This works also for line breaks.

• no shellthree doesn't look much better ;) so there isn't a way to combine soul and lstinline? – Thomas Rebele Aug 21 '12 at 14:59
• I think thats way beyond my capabilities in TeX ^^ Maybe I wait some days an then accept your answer if it is still the only one :) – Thomas Rebele Aug 21 '12 at 15:30