3

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
\renewcommand{\texttt}[1]{\colorbox{shadecolor}{\oldtexttt{#1}}}

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}
\end{Shaded}

by changing the preamble only?

Screenshots:

What it looks like:

screenshot

What it should look like:

screenshot

MCVE:

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

\definecolor{shadecolor}{RGB}{200,200,0}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}

\begin{document}

\begin{verbatim}
text
\end{verbatim}

\end{document}

3 Answers 3

3

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}
\definecolor{shadecolor}{named}{Goldenrod}

\renewenvironment{verbatim}{%
  \VerbatimEnvironment
  \begin{shaded}
  \begin{Verbatim}%
}{%
  \end{Verbatim}%
  \end{shaded}%
}

\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$
\end{verbatim}
\end{document}

enter image description here

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.

1
  • \def\VerbatimEnvironment{\gdef\FV@EnvironName{verbatim}} can be more simply just \VerbatimEnvironment.
    – egreg
    Jan 9, 2019 at 23:30
2

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}
1
  • @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. Jan 6, 2019 at 15:19
1

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$
\end{verbatim}
\end{document}

As far as I can see, there is no way to specify that you want to highlight all lines, so I set 10000 as the last line, which should be sufficient.

enter image description here

You must log in to answer this question.

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