23

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

6
  • Maybe fancyvrb could help you.
    – Sigur
    Commented Oct 28, 2013 at 11:24
  • @Sigur I don't know this package actually.
    – Martingal
    Commented Oct 28, 2013 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
    – cgnieder
    Commented Oct 28, 2013 at 11:37
  • Duplicate of your initial question: tex.stackexchange.com/questions/62202/… Commented Oct 28, 2013 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
    Commented Oct 28, 2013 at 11:59

3 Answers 3

20

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}

8
  • The \cverb command doesn't seem to work for me. Any idea why?
    – user545424
    Commented Apr 16, 2015 at 18:01
  • @user545424 Because the definition somehow slipped out of the code. Fixed.
    – egreg
    Commented Apr 16, 2015 at 19:16
  • How can we \label these customized verbatim environments for referencing? Thank you. Commented Oct 3, 2017 at 20:19
  • @codeaviator Unless you add a number somewhere…
    – egreg
    Commented Oct 3, 2017 at 20:21
  • 1
    @SergioAraujo \makeatletter\renewcommand\verbatim@font{\normalfont\small\ttfamily} (if you don't need it at various sizes.
    – egreg
    Commented Dec 2, 2020 at 23:25
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}
1
  • 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
    Commented Oct 28, 2013 at 11:03
1
\usepackage{framed,color,verbatim}
\definecolor{shadecolor}{rgb}{.9, .9, .9}

\newenvironment{code}%
   {\snugshade\verbatim}%
   {\endverbatim\endsnugshade}

\begin{document}
text

\begin{code}
line one
line two
\end{code}

\end{document}

Looks like this-

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .