8

I'm new in LaTeX and I'm searching for solution to highlight verbatim text inside paragraph (for example, it is like on this page, where code is highlighted). Can anyone help me with this?

Let say, that I have a paragraph and inside is some words where I use \verb|| to show some code parts, but I'd like that this is highlighted or it has background color, juts like it is highlighted in this page. For normal text I can use \newcommand{\hilight}[1]{\colorbox{gray!10}{some text}}, but this command doesn't work with \verb, so I can't use it. I'm trying to find solution and searching over the internet, but I didn't fid so far.

Thanks for your help.

11

It's as simple as

\documentclass{article}
\usepackage{xcolor}
\usepackage{newverbs}
\newverbcommand{\cverb}{\color{red}}{}

\begin{document}
Text \cverb|&*}| and more text
\end{document}

enter image description here

Version with a macro for having a background text:

\documentclass{article}
\usepackage{xcolor}
\usepackage{newverbs}

\newverbcommand{\cverb}{\color{red}}{}
\newverbcommand{\bverb}
  {\begin{lrbox}{\verbbox}}
  {\end{lrbox}\colorbox{gray!30}{\box\verbbox}}

\begin{document}
Text \cverb|&*}| and more text

Text \bverb|&*}| and more text
\end{document}

enter image description here

  • This is good for changing color. How do I change background color (grey)? – Phantom Jun 25 '13 at 12:37
  • @Phantom Added new macro – egreg Jun 25 '13 at 13:01
2
\documentclass{article}
\usepackage{color}
\usepackage{verbatimbox}
\begin{document}
This is a
\color{red}
\begin{verbbox}
\verbatim text
\end{verbbox}
\theverbbox
\color{black}
~that is inline

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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