10

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.

Phantom
  • 103
  • 2
    Do you know the package newverbs? See, for instance, http://tex.stackexchange.com/questions/22416/how-can-i-redefine-the-verb-command and http://tex.stackexchange.com/questions/114997/how-do-i-define-a-custom-verbatim-command – egreg Jun 25 '13 at 09:09
  • I didn't know for this package. Will read this posts and test, if it will work well for me. – Phantom Jun 25 '13 at 12:39

2 Answers2

14

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

egreg
  • 1,121,712
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