20

Given a verbatim as on below,

\begin{verbatim}
This is an awesome verbatim
\end{verbatim}

which is displayed with no background on the compiled pdf page :

This is an awesome verbatim

I would like to have a soft grey background on this verbatim instead. The result on the compiled pdf page would be :

This is an awesome verbatim

Is there a simple way doing it, still using the verbatim (ie. not using listings)? Thank you in advance.

ADDITIONAL QUESTION :

Is this possible to do the same with \texttt{not usual verbatim but still code quote} and getting also a grey background for all \texttt{*} elements ? It would also display :

not verbatim but still code quote

  • Maybe fancyvrb could help you. – Sigur Oct 28 '13 at 11:24
  • @Sigur I don't know this package actually. – Martingal Oct 28 '13 at 11:31
  • 1
    @Martingal It's on CTAN ctan.org/pkg/fancyvrb. You should also be able to look at the documentation by calling texdoc fancyvrb from the command line. Maybe the listings package is also worth to be checked out – clemens Oct 28 '13 at 11:37
  • Duplicate of your initial question: tex.stackexchange.com/questions/62202/… – Torbjørn T. Oct 28 '13 at 11:53
  • 2
    @TorbjørnT. It's not a duplicate. Check the content and the request, I also took a look at this post, however it didn't answered to my need. – Martingal Oct 28 '13 at 11:59
13

You can use fancyvrb; I present two solutions, one with the background only below the actual contents, the other one going all the way to the line width.

For the additional question you can use newverbs:

\documentclass{article}
\usepackage{fancyvrb,newverbs,xcolor}
\usepackage{lipsum}% just for this example

\definecolor{cverbbg}{gray}{0.93}

\newenvironment{cverbatim}
 {\SaveVerbatim{cverb}}
 {\endSaveVerbatim
  \flushleft\fboxrule=0pt\fboxsep=.5em
  \colorbox{cverbbg}{\BUseVerbatim{cverb}}%
  \endflushleft
}
\newenvironment{lcverbatim}
 {\SaveVerbatim{cverb}}
 {\endSaveVerbatim
  \flushleft\fboxrule=0pt\fboxsep=.5em
  \colorbox{cverbbg}{%
    \makebox[\dimexpr\linewidth-2\fboxsep][l]{\BUseVerbatim{cverb}}%
  }
  \endflushleft
}

\newcommand{\ctexttt}[1]{\colorbox{cverbbg}{\texttt{#1}}}
\newverbcommand{\cverb}
  {\setbox\verbbox\hbox\bgroup}
  {\egroup\colorbox{cverbbg}{\box\verbbox}}

\begin{document}
Some verbatim mid line \cverb|$a^{x+y}=a^xa^y$| and other words. Also
a simple \ctexttt{typewriter text} with its background.

\lipsum[1]
\begin{cverbatim}
Something 
verbatim
\foo
\end{cverbatim}
\lipsum[2]
\begin{lcverbatim}
Something 
verbatim
\foo
\end{lcverbatim}
\lipsum[3]
\end{document}

  • The \cverb command doesn't seem to work for me. Any idea why? – user545424 Apr 16 '15 at 18:01
  • @user545424 Because the definition somehow slipped out of the code. Fixed. – egreg Apr 16 '15 at 19:16
  • How can we \label these customized verbatim environments for referencing? Thank you. – codeaviator Oct 3 '17 at 20:19
  • @codeaviator Unless you add a number somewhere… – egreg Oct 3 '17 at 20:21
  • @egreg Sorry but I don't understand what you mean. – codeaviator Oct 3 '17 at 20:48
4

Here's one way, with the verbatimbox package.

The answer has been edited to allow the verbatim to optionally take up the full text width, by stuffing \theverbbox into a full width \parbox. One downside to this verbbox approach is that \theverbbox cannot extend across page boundaries

\documentclass{article}
\usepackage{verbatimbox}
\usepackage{xcolor}
\begin{document}
Not verbatim
\begin{verbbox}[\scriptsize]
Inline \scriptsize \verbatim
\end{verbbox}
\colorbox{lightgray}{\theverbbox}
Not vermatim\par
Not verbatim
\begin{verbbox}[\itshape]
This is multi-line \itshape \verbatim
with variable width, &%^@
that can be presented on separate lines with the use of \par
\end{verbbox}
\par\colorbox{lightgray}{\theverbbox}\par
Not verbatim
\begin{verbbox}
This is \verbatim
made to look full width
with the use of \par's and a full width \parbox
\end{verbbox}
\par\colorbox{lightgray}{\parbox{\textwidth}{\theverbbox}}\par
Not verbatim
\end{document}

enter image description here

If you wanted that last setup as its own envirnment, the define it as

\newenvironment{fullgrayverb}
{\verbbox}
{\endverbbox\par\colorbox{lightgray}{\parbox{\textwidth}{\theverbbox}}\par}

and employ it as follows

\begin{fullgrayverb}
\verbatim
full width
gray background
in its own \newenvironment
\end{fullgrayverb}
  • 2
    I see, but I would like to have this grey on all my verbatims and not verbbox. I mean, the verbbox display is not the same than verbatim (ie. ends of line). – Martingal Oct 28 '13 at 11:03

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.