I want to make several words with a grey background like this
within normal text? Like with \colorbox{yellow}{\lstinline{some text here}}
but grey instead of yellow. How to do that?
[Tex/LaTex] Add grey background to inline text
highlighting
Related Solutions
put the xcolor package in the preamble,
\usepackage{xcolor}
then use a command like this:
\fcolorbox{gray}{gray}{TEXT}
Change 'gray' to the shade that suits your gods liking.
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
As you write in your question you use
\colorbox
. As the first argument is simply a color name you need to exchangeyellow
forgray
or one of the other defined colors fromxcolor
(first column is name):Example: