Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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 do not know. The documentation of package listings says clear, that \lstinline do not provide background colors. Perhaps one can redefine \lstinline to include background colors, I can't. –  Kurt Aug 21 '12 at 15:13
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.