\documentclass[border=2]{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{fancyvrb}
\makeatletter
\newif\ifFV@bgcolor
\newbox\FV@bgbox
\define@key{FV}{bgcolor}{\FV@bgcolortrue\def\FV@bgcolor{#1}}
\def\FV@BeginVBox{%
\leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi
\hbox\ifx\FV@boxwidth\relax\else to\FV@boxwidth\fi\bgroup
\ifcase\FV@baseline\vbox\or\vtop\or$\vcenter\fi\bgroup}
\def\FV@EndVBox{\egroup\ifmmode$\fi\hfil\egroup
\ifFV@bgcolor\colorbox{\FV@bgcolor}{\box\FV@bgbox}\fi}
\makeatother
\begin{document}
\begin{BVerbatim}[bgcolor=LightSteelBlue]
abc
def
\end{BVerbatim}
\end{document}
Here's the result:
Add options to BVerbatim
to have a fixed width instead of the minimum one.
Change the code into
\makeatletter
\newif\ifFV@bgcolor
\newbox\FV@bgbox
\define@key{FV}{bgcolor}{\FV@bgcolortrue\def\FV@bgcolor{#1}}
\define@key{FV}{framecolor}{\FV@bgcolortrue\def\FV@framecolor{#1}}
\def\FV@framecolor{white}
\def\FV@BeginVBox{%
\leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi
\hbox\ifx\FV@boxwidth\relax\else to\FV@boxwidth\fi\bgroup
\ifcase\FV@baseline\vbox\or\vtop\or$\vcenter\fi\bgroup}
\def\FV@EndVBox{\egroup\ifmmode$\fi\hfil\egroup
\ifFV@bgcolor\fcolorbox{\FV@framecolor}{\FV@bgcolor}{\box\FV@bgbox}\fi}
\makeatother
to get a new key framecolor
(default white, so no frame):
\begin{BVerbatim}[bgcolor=LightSteelBlue,framecolor=black]
abc
def
\end{BVerbatim}
Comments on the code
Let's look at the code for adding a background color (the extension for the frame color is very similar).
I add a conditional (\ifFV@bgcolor
) and a box register (\FV@bgbox
) and a key for the FV
family; when the user says bgcolor=somecolor
, the conditional will be set to true and somecolor
is stored in \FV@bgcolor
.
Then I redefine \FV@BeginVBox
which is responsible for starting the box for a BVerbatim
environment; its definition starts with
\leavevmode\hbox...
so what I do is to change it into
\leavevmode\ifFV@bgcolor\setbox\FV@bgbox=\fi\hbox...
In this way, when the conditional is true, the box will be stored in our new register, instead that typeset immediately.
Now the end part: \FV@EndVBox
usually ends with
...\hfil\egroup}
where this \egroup
closes the \hbox
construction; if the conditional is true, this wouldn't typeset the box, so I make it into
...\hfil\egroup
\ifFV@bgcolor\colorbox{\FV@bgcolor}{\box\FV@bgbox}\fi}
which will typeset the box over a suitably colored background.
You can patch the part
command to add the page color instead of adding it manually. But since a newpage
is issued at the end of \part
internally (when openright
option is used (default)) the page color is applied to the next page also. This can be avoided by patching the \@endpart
command. You can do it the hard way or the smart way as shown in the following code.
\documentclass{book}
\usepackage{xcolor}
\makeatletter
%%-----------------Hard coding----------------------
%\renewcommand\part{%
% \if@openright
% \cleardoublepage
% \else
% \clearpage
% \fi
% \pagecolor{green} %% Added line here
% \thispagestyle{plain}%
% \if@twocolumn
% \onecolumn
% \@tempswatrue
% \else
% \@tempswafalse
% \fi
% \null\vfil
% \secdef\@part\@spart}
%\renewcommand\@endpart{\vfil\newpage
% \pagecolor{white} %%% Added line here
% \if@twoside
% \if@openright
% \null
% \thispagestyle{empty}%
% \newpage
% \fi
% \fi
% \if@tempswa
% \twocolumn
% \fi}
%%--------- end of hard coding ---------------------
%%---smart coding ----------------------------------
\usepackage{xpatch}
%
\xpatchcmd{\part}{\thispagestyle{plain}}
{\pagecolor{green}\thispagestyle{plain}}{}{}
\xpatchcmd{\@endpart}{\vfil\newpage}{\vfil\newpage
\pagecolor{white}}{}{}
%%--------- end of smart coding ---------------------
\makeatother
\begin{document}
\chapter{One}
First page
\cleardoublepage
\part{Example for pagecolor}
Last page
\end{document}
Best Answer
Something like this?
Unrelated: as I have not installed
anysize
(which is considered obsolete), I replaced it withgeometry
, but I'm not sure the word ‘margin’ denotes exactly the same lengths, so the parameters I copied from your code might have to be adapted. Also, since version 3.10, the language options for babel should be loaded with the document class, so that all language-dependent packages be informed.Edit:
In case a section title requires several lines, the above code does not work anymore. A slightly longer code settles the problem. It requires the
explicit
option oftitlesec
and theblock
shape. Here a demo: