Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am, once again, trying to achieve what seems like a trivial goal. I want to use \newenvironment to define a verbatim environment with a background color. I need to define my own environment because I need to run extra commands when opening the environment.

Here's what I have so far:

  % here, run extra commands, mostly related to \tikzmark and other extra legwork

This works fine. I tried the following to add a background color.

  • Define a background color for FancyVrb (Background color for fancyvrb), but then if I put \begin{Verbatim} and \end{Verbatim} in lieu of \verbatim and \endverbatim, this no longer works,
  • Playing myself with \color@setgroup and \color@endroup but these do not seem to be exposed by the xcolor package.
  • Using an lrbox, but again, any pair of \begin{foo} and \end{foo} that go across the environment are rejected (possibly because of \verbatim?)

I'm quite puzzled now and would love to have any pointers. Thanks!

UPDATE: the first comment has the solution, namely my failing to use \VerbatimEnvironment!

share|improve this question
For fancyvrb, you need to use \VerbatimEnvironment before the \begin{Verbatim}. That tells fancyvrb to detect the name of the current environment, and look for the end of that environment, rather than looking for a literal \end{Verbatim}. –  G. Poore Apr 15 '14 at 20:10
Thanks, this indeed solves my use case. –  Jonathan Protzenko Apr 15 '14 at 20:33
I'd recommend trying tcolorbox –  egreg Apr 15 '14 at 20:37

2 Answers 2

up vote 4 down vote accepted

For fancyvrb, you need to use \VerbatimEnvironment before the \begin{Verbatim}. That tells fancyvrb to detect the name of the current environment, and look for the end of that environment, rather than looking for a literal \end{Verbatim}.

You will probably want to use a proper framing package to provide the background color...that way you won't have to worry about coming up with your own solutions for pagebreaks and will have access to a lot more features. You may want to take a look at mdframed or tcolorbox. An example with mdframed is below. tcolorbox has built-in solutions for a lot of use cases; one of those might give you most or all of what you need.






share|improve this answer
Thanks. That is much simpler than whatever I had come up with. Your solution does add quite some extra blank space before and after the environment, though. (Trying to figure out why right now.) –  Jonathan Protzenko Apr 15 '14 at 20:50
Found it. It was the skipabove and skipbelow settings of mdframed. –  Jonathan Protzenko Apr 15 '14 at 20:52

First, here is a solution that uses only the xcolor package and the standard verbatim environment. But it is not a new environment, per se, and so does not directly answer the OP's question (for that, see below).

\verbatim <Text>
%$#@&^* \macros

enter image description here

To create an environment as desired by the OP, I adapted color into the syntax employed by the verbatimbox package, and created the cverbbox environment, which stores your verbatim content in a user-named box that is exactly as wide as the content (plus boxsep and boxrule). It comes with an optional argument and 4 mandatory arguments:

\begin{cverbbox}[options]{boxname}{text color}{box color}{frame color}

where the options are the same as already work in verbatimbox environments (in this MWE, I used the option to make the verbatim \tiny and added a bullet before each row). It uses \fboxsep and \fboxrule in the standard manner to set frame offset and rule thickness.

    \hsize=\wd0 \the\verbatim@line\par}}%
  \@ifundefined{\sv@name content}{%
    \expandafter\newsavebox\expandafter{\csname\sv@name content\endcsname}%
  \csname\sv@name content\endcsname}}%
  \setbox0=\vbox\bgroup\color{#3} \verbatim
  \unskip\setbox0=\lastbox %
  \global\sbox{\csname\sv@name content\endcsname}%
\verbatim <Text>
%$#@&^* \macros

enter image description here

share|improve this answer
Thanks for the in-depth answer! –  Jonathan Protzenko Apr 16 '14 at 12:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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