# How can I put every verbatim environment inside a Shaded environment by changing the preamble only?

Unfortunately, I have a lot of TeX code which should look different that it does not, but I cannot modify its code body. However, I can modify the preamble and for example, I have learned that I can redefine \texttt to make every

\texttt{text}


into

\colorbox{shadecolor}{\texttt{text}}


by running

\let\oldtexttt\texttt


in the preamble. That might not be perfect, but it works great. However, now I want to put every verbatim environment inside a Shaded environment. Obviously I cannot use \renewcommand for this again. Is there something else I can use to make every

\begin{verbatim}
text
\end{verbatim}


into

\begin{Shaded}
\begin{verbatim}
text
\end{verbatim}


by changing the preamble only?

Screenshots:

What it looks like:

What it should look like:

MCVE:

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{framed}

\begin{document}

\begin{verbatim}
text
\end{verbatim}

\end{document}


We can use fancyvrb package, using similar technique as https://tex.stackexchange.com/a/468250/4686

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{framed}
\usepackage[dvipsnames]{color}

\renewenvironment{verbatim}{%
\VerbatimEnvironment
\begin{Verbatim}%
}{%
\end{Verbatim}%
la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$
la$la$ la$la$ la$la$ la$la$ la$la$ la$\end{verbatim} \end{document}  If your fancyvrb version is in the 3.x series but before 3.2 of 2019/01/08 you may need to use this preamble instead: \PassOptionsToPackage{dvipsnames}{xcolor} \usepackage{fancyvrb}% loads xcolor hence option clash hence the above \usepackage{framed} \definecolor{shadecolor}{named}{Goldenrod}  (or load xcolor with dvipsnames option before loading fancyvrb), because for these releases fancyvrb loaded xcolor. • \def\VerbatimEnvironment{\gdef\FV@EnvironName{verbatim}} can be more simply just \VerbatimEnvironment. – egreg Jan 9 at 23:30 With etoolbox it's quite easy. You can use the \BeforeBeginEnvironment and \AfterEndEnvironment hooks: \documentclass[12pt]{article} \usepackage{xcolor} \usepackage{framed} \usepackage{etoolbox} \definecolor{shadecolor}{RGB}{200,200,0} \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} \BeforeBeginEnvironment{verbatim}{\begin{Shaded}} \AfterEndEnvironment{verbatim}{\end{Shaded}} \begin{document} \begin{verbatim} text \end{verbatim} \end{document}  or, if you can't add more packages (although, chances are etoolbox was already loaded by another package), you can copy the relevant parts of the code: \documentclass[12pt]{article} \usepackage{xcolor} \usepackage{framed} \definecolor{shadecolor}{RGB}{200,200,0} \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} \makeatletter % Copied from latex.ltx \def\begin#1{% \csname @beforebegin@#1@hook\endcsname% <<-- Added this \@ifundefined{#1}% {\def\reserved@a{\@latex@error{Environment #1 undefined}\@eha}}% {\def\reserved@a{\def\@currenvir{#1}% \edef\@currenvline{\on@line}% \csname #1\endcsname}}% \@ignorefalse \begingroup\@endpefalse\reserved@a} \def\end#1{% \csname end#1\endcsname\@checkend{#1}% \expandafter\endgroup\if@endpe\@doendpe\fi \csname @afterend@#1@hook\endcsname% <<-- Added this \if@ignore\@ignorefalse\ignorespaces\fi} % Copied from etoolbox.sty \DeclareRobustCommand{\BeforeBeginEnvironment}[1]{% \expandafter\gappto\csname @beforebegin@#1@hook\endcsname} \DeclareRobustCommand{\AfterEndEnvironment}[1]{% \expandafter\gappto\csname @afterend@#1@hook\endcsname} \newcommand{\ifundef}[1]{% \ifdefined#1% \ifx#1\relax \expandafter\expandafter \expandafter\@firstoftwo \else \expandafter\expandafter \expandafter\@secondoftwo \fi \else \expandafter\@firstoftwo \fi} \DeclareRobustCommand{\gappto}[2]{% \ifundef{#1} {\xdef#1{\unexpanded{#2}}} {\xdef#1{\unexpanded\expandafter{#1}\unexpanded{#2}}}} \makeatother \BeforeBeginEnvironment{verbatim}{\begin{Shaded}} \AfterEndEnvironment{verbatim}{\end{Shaded}} \begin{document} \begin{verbatim} text \end{verbatim} \end{document}  • @Jayjayyy etoolbox is widely used, so chances are it's already loaded by some other package. Either way I copied the relevant parts of the code. – Phelype Oleinik Jan 6 at 15:19 You can use fancyvrb and fvextra: \documentclass{article} \usepackage{fancyvrb,fvextra} \definecolor{shadecolor}{RGB}{200,200,0} \RecustomVerbatimEnvironment{verbatim}{Verbatim} {highlightlines=1-10000,highlightcolor=shadecolor} \begin{document} \begin{verbatim} la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$
la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$ la$la$