2

I have replaced the Verbatim environment to include a vbox and colorbox so that I can apply a background color to it. The problem is that now it does not split at the end of the page.

This is a minimal working code (working meaning that it compiles):

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}

\usepackage{fvextra}

\definecolor{codebgcolor}{cmyk}{0,0.02,0.03,0}

% Note: redefining the Verbatim environment to have a background
% I used `latexdef -E itemize` and `latexdef -E enumerate` to
% discover the internal variables they use to control indentation.
% Then I used `\leftmargin` to know how much each indentation
% level indents, and added a negative horizontal spacing `\hspace`.
% ref: [The definitions of LaTeX commands](https://texfaq.org/FAQ-ltxcmds)
% ref: https://tex.stackexchange.com/questions/62202/change-background-colour-of-verbatim-environment
\makeatletter
\let\oldv\Verbatim
\def\Verbatim{%
    \setbox0\vbox\bgroup\oldv%
}
% == indented nested Verbatim ==
% ref: https://tex.stackexchange.com/questions/426736/addition-multiplication-of-parameter-and-constant-with-unit
\def\unindent{\hspace{-\dimexpr\leftmargin*(\@itemdepth+\@enumdepth)\relax}}
\let\oldendv\endVerbatim
\def\endVerbatim{%
    \oldendv\egroup\fboxsep0pt \colorbox{codebgcolor}{%
        \parbox{\linewidth}{%
            \unindent\usebox0%
        }%
    }%
}
\makeatother

\begin{document}
    
\begin{itemize}
\item Something here:

    \begin{Verbatim}[commandchars=\\\{\}, mathescape]
    
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
    
    \end{Verbatim}

\end{itemize}

\end{document}

I tried to follow the instructions in another question Breakable vboxes without success.

I really didn't understand what is \partialboxdim in the accepted answer, and how am I supposed to set it. Also, I don't know how to use that with \setbox0\vbox\bgroup. I tried to replace it with \setbox\totalbox=\vbox\bgroup and then use it at the redefined endVerbatim replacing \usebox0 with \totalbox... but that didn't work. I don't know how to proceed with this.

  • Of course a vbox does not break between pages (both your \vbox and the later \parbox). – Donald Arseneau Jun 29 at 21:47
  • You should consider tcolorbox – egreg Jun 29 at 22:13
2

You should use one of the packages mdframed, framed, or tcolorbox to make the breakable colorbox. For example

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}
\usepackage{framed}
\usepackage{fvextra}

\definecolor{codebgcolor}{cmyk}{0,0.02,0.03,0}

\let\oldv\Verbatim
\def\Verbatim{%
    \definecolor{shadecolor}{cmyk}{0,0.02,0.03,0}%
    \csname shaded*\endcsname \oldv
}
\let\oldendv\endVerbatim
\def\endVerbatim{%
  \oldendv \csname endshaded*\endcsname
}

\begin{document}

This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.
This is text beforehand to show the margin.

\begin{itemize}
\item Something here:

  \begin{Verbatim}[commandchars=\\\{\}, mathescape]
    
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
    
  \end{Verbatim}

\end{itemize}

This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
This is text afterwards to show the margin.
\end{document}

I wasn't absolutely sure of how you wanted the shading positioned, but thought you were trying to get it at the regular text margins. Within framed.sty there are other variations such as shaded, snugshade, and snugshade*. Using snugshade* makes the shading be indented with the list.

The other package do well too! (But good luck reading the tcolorbox manual).

| improve this answer | |
  • I ended up using this alternative... I tried using tcolorbox, seems very feature complete, but didn't find a way to gobble easily. In the manual there is only a mention to gobble using minted, which required me to use special CLI arguments for latex. – Miguel Angelo Jun 30 at 17:01
2

You should consider tcolorbox.

\documentclass[
a4paper,
]{article}

\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}

\definecolor{codebgcolor}{cmyk}{0,0.05,0.05,0}

\newtcblisting{Verbatim}{
  breakable,enhanced,
  listing only,
  frame hidden,
  colback=codebgcolor,
  grow to left by=6pt,
  grow to right by=6pt,
  left=1.6pt,
  right=1.6pt,
  arc=0pt
}

\begin{document}

\begin{Verbatim}
abc def
ghi
\end{Verbatim}
    
\begin{itemize}
\item Something here:

\begin{Verbatim}
    a a
    a b
    a c
    a d
    a e
    a f
    a g
    a h
    a i
    a j
    a k
    a l
    a m
    a n
    a o
    a p
    a q
    a r
    a s
    a t
    a u
    a v
    a w
    a x
    a y
    b a
    b b
    b c
    b d
    b e
    b f
    b g
    b h
    b i
    b j
    b k
    b l
    b m
    b n
    b o
    b p
    b q
    b r
    b s
    b t
    b u
    b v
    b w
    b x
    b y
    c a
    c b
    c c
    c d
    c e
    c f
    c g
    c h
    c i
    c j
    c k
    c l
    c m
    c n
    c o
    c p
    c q
    c r
    c s
    c t
    c u
    c v
    c w
    c x
    c y
\end{Verbatim}

\end{itemize}

\end{document}

enter image description here

I have slightly changed your color in order to make the background more visible.

| improve this answer | |

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.