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.
Two types of macros are defined. The \plaintt
breaks at line endings (without any additional hyphen character added). The other one \highlighttt
allows breaks only at dots, and colors the text. No break can occur in between dots.
Update: I have made the thing a bit customizable. Let me comment on the difference between the two macros: \highlighttt
has the disadvantage or advantage that breaks occur only at dots. But it has the big advantage that somewhat arbitrary TeX code in-between the dots can be used. See the first paragraph in the example, where I highlighted one sub-piece in blue.
The second macro \plaintt
accepts basically only letters and punctutations, but anything can be escaped from it by just putting it within braces. This is illustrated in the second paragraph.
A hook is provided so that also \plaintt
can do some highlighting, character per character. See the third paragraph where only \plaintt
is used, but the hook has been set to use the yellow box highlighting method.
The highlighting method for \highlighttt
is specified in the command \myhighlightmethod
.
Only the use of the txtt
font is hardcoded in the two macros, but of course this could be changed.
\documentclass{article}
%\usepackage[T1]{fontenc}
\usepackage[textwidth=12cm]{geometry}
%\usepackage{xcolor}
\usepackage{color}
% ``private macros''
\makeatletter
\def\@highlightttpeeknext{\futurelet\@nexttoken\@highlightttaux}
\def\@highlighttt #1.{%
\def\@highlightttaux{\ifx\@nexttoken\egroup
\myhighlightmethod {#1}\else
\myhighlightmethod {#1.}\linebreak[2]%
\expandafter\@highlighttt\fi}%
\@highlightttpeeknext}
\def\@plaintt {\futurelet\@nexttoken\@plainttaux}
\def\@plainttaux {\ifx\@nexttoken\egroup\else
\ifx\@nexttoken\bgroup
\expandafter\expandafter\expandafter\@plaintta\else
\expandafter\expandafter\expandafter\@plainttb\fi\fi}
\def\@plaintta #1{{#1}\@plaintt}
\def\@plainttb #1{\ifcat\@nexttoken a\penalty\hyphenpenalty \plaintthook
#1\else \plaintthook{#1}\linebreak[2]\fi\@plaintt}
% ``commands''
\newcommand{\highlighttt}[1]{{\fontfamily{txtt}\selectfont
\@highlighttt #1.}}
\newcommand\plaintt{\bgroup\fontfamily{txtt}\selectfont
\afterassignment\@plaintt\let\next= }
\makeatother
% ``customization''
\newcommand{\myhighlightmethod}[1]{\fboxsep0pt\colorbox{yellow}{\strut#1}}
\newcommand{\plaintthook}{}
\begin{document}\thispagestyle{empty}
\begin{description}
\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \highlighttt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
extending at the point
\highlighttt{org.eclipse.cdt.core.LanguageSettingsProvider}
\item In \highlighttt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \highlighttt{GCCBuildOutputParser.name}. This is
extending at the point
\plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}
\renewcommand{\plaintthook}[1]{\myhighlightmethod{#1}}
\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
extending at the point
\plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}
\end{description}
\end{document}
Best Answer
Listings allow escaping to TeX, so you can put a box in the background: