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












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



letoldtexttttexttt
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}









share|improve this question





























    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



    letoldtexttttexttt
    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}









    share|improve this question



























      3












      3








      3


      0






      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



      letoldtexttttexttt
      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}









      share|improve this question
















      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



      letoldtexttttexttt
      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}






      macros formatting verbatim framed backgrounds






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 6 at 14:37







      finefoot

















      asked Jan 6 at 14:32









      finefootfinefoot

      215110




      215110






















          3 Answers
          3






          active

          oldest

          votes


















          2














          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.






          share|improve this answer


























          • defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

            – egreg
            Jan 9 at 23:30











          • @egreg, answer edited, thanks.

            – jfbu
            Jan 10 at 8:47



















          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
          defbegin#1{%
          csname @beforebegin@#1@hookendcsname% <<-- Added this
          @ifundefined{#1}%
          {defreserved@a{@latex@error{Environment #1 undefined}@eha}}%
          {defreserved@a{def@currenvir{#1}%
          edef@currenvline{on@line}%
          csname #1endcsname}}%
          @ignorefalse
          begingroup@endpefalsereserved@a}
          defend#1{%
          csname end#1endcsname@checkend{#1}%
          expandafterendgroupif@endpe@doendpefi
          csname @afterend@#1@hookendcsname% <<-- Added this
          if@ignore@ignorefalseignorespacesfi}
          % Copied from etoolbox.sty
          DeclareRobustCommand{BeforeBeginEnvironment}[1]{%
          expandaftergapptocsname @beforebegin@#1@hookendcsname}
          DeclareRobustCommand{AfterEndEnvironment}[1]{%
          expandaftergapptocsname @afterend@#1@hookendcsname}
          newcommand{ifundef}[1]{%
          ifdefined#1%
          ifx#1relax
          expandafterexpandafter
          expandafter@firstoftwo
          else
          expandafterexpandafter
          expandafter@secondoftwo
          fi
          else
          expandafter@firstoftwo
          fi}
          DeclareRobustCommand{gappto}[2]{%
          ifundef{#1}
          {xdef#1{unexpanded{#2}}}
          {xdef#1{unexpandedexpandafter{#1}unexpanded{#2}}}}
          makeatother

          BeforeBeginEnvironment{verbatim}{begin{Shaded}}
          AfterEndEnvironment{verbatim}{end{Shaded}}

          begin{document}

          begin{verbatim}
          text
          end{verbatim}

          end{document}





          share|improve this answer


























          • @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



















          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






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468824%2fhow-can-i-put-every-verbatim-environment-inside-a-shaded-environment-by-chan%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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.






            share|improve this answer


























            • defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

              – egreg
              Jan 9 at 23:30











            • @egreg, answer edited, thanks.

              – jfbu
              Jan 10 at 8:47
















            2














            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.






            share|improve this answer


























            • defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

              – egreg
              Jan 9 at 23:30











            • @egreg, answer edited, thanks.

              – jfbu
              Jan 10 at 8:47














            2












            2








            2







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 10 at 8:33

























            answered Jan 6 at 18:08









            jfbujfbu

            46.4k66148




            46.4k66148













            • defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

              – egreg
              Jan 9 at 23:30











            • @egreg, answer edited, thanks.

              – jfbu
              Jan 10 at 8:47



















            • defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

              – egreg
              Jan 9 at 23:30











            • @egreg, answer edited, thanks.

              – jfbu
              Jan 10 at 8:47

















            defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

            – egreg
            Jan 9 at 23:30





            defVerbatimEnvironment{gdefFV@EnvironName{verbatim}} can be more simply just VerbatimEnvironment.

            – egreg
            Jan 9 at 23:30













            @egreg, answer edited, thanks.

            – jfbu
            Jan 10 at 8:47





            @egreg, answer edited, thanks.

            – jfbu
            Jan 10 at 8:47











            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
            defbegin#1{%
            csname @beforebegin@#1@hookendcsname% <<-- Added this
            @ifundefined{#1}%
            {defreserved@a{@latex@error{Environment #1 undefined}@eha}}%
            {defreserved@a{def@currenvir{#1}%
            edef@currenvline{on@line}%
            csname #1endcsname}}%
            @ignorefalse
            begingroup@endpefalsereserved@a}
            defend#1{%
            csname end#1endcsname@checkend{#1}%
            expandafterendgroupif@endpe@doendpefi
            csname @afterend@#1@hookendcsname% <<-- Added this
            if@ignore@ignorefalseignorespacesfi}
            % Copied from etoolbox.sty
            DeclareRobustCommand{BeforeBeginEnvironment}[1]{%
            expandaftergapptocsname @beforebegin@#1@hookendcsname}
            DeclareRobustCommand{AfterEndEnvironment}[1]{%
            expandaftergapptocsname @afterend@#1@hookendcsname}
            newcommand{ifundef}[1]{%
            ifdefined#1%
            ifx#1relax
            expandafterexpandafter
            expandafter@firstoftwo
            else
            expandafterexpandafter
            expandafter@secondoftwo
            fi
            else
            expandafter@firstoftwo
            fi}
            DeclareRobustCommand{gappto}[2]{%
            ifundef{#1}
            {xdef#1{unexpanded{#2}}}
            {xdef#1{unexpandedexpandafter{#1}unexpanded{#2}}}}
            makeatother

            BeforeBeginEnvironment{verbatim}{begin{Shaded}}
            AfterEndEnvironment{verbatim}{end{Shaded}}

            begin{document}

            begin{verbatim}
            text
            end{verbatim}

            end{document}





            share|improve this answer


























            • @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
















            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
            defbegin#1{%
            csname @beforebegin@#1@hookendcsname% <<-- Added this
            @ifundefined{#1}%
            {defreserved@a{@latex@error{Environment #1 undefined}@eha}}%
            {defreserved@a{def@currenvir{#1}%
            edef@currenvline{on@line}%
            csname #1endcsname}}%
            @ignorefalse
            begingroup@endpefalsereserved@a}
            defend#1{%
            csname end#1endcsname@checkend{#1}%
            expandafterendgroupif@endpe@doendpefi
            csname @afterend@#1@hookendcsname% <<-- Added this
            if@ignore@ignorefalseignorespacesfi}
            % Copied from etoolbox.sty
            DeclareRobustCommand{BeforeBeginEnvironment}[1]{%
            expandaftergapptocsname @beforebegin@#1@hookendcsname}
            DeclareRobustCommand{AfterEndEnvironment}[1]{%
            expandaftergapptocsname @afterend@#1@hookendcsname}
            newcommand{ifundef}[1]{%
            ifdefined#1%
            ifx#1relax
            expandafterexpandafter
            expandafter@firstoftwo
            else
            expandafterexpandafter
            expandafter@secondoftwo
            fi
            else
            expandafter@firstoftwo
            fi}
            DeclareRobustCommand{gappto}[2]{%
            ifundef{#1}
            {xdef#1{unexpanded{#2}}}
            {xdef#1{unexpandedexpandafter{#1}unexpanded{#2}}}}
            makeatother

            BeforeBeginEnvironment{verbatim}{begin{Shaded}}
            AfterEndEnvironment{verbatim}{end{Shaded}}

            begin{document}

            begin{verbatim}
            text
            end{verbatim}

            end{document}





            share|improve this answer


























            • @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














            2












            2








            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
            defbegin#1{%
            csname @beforebegin@#1@hookendcsname% <<-- Added this
            @ifundefined{#1}%
            {defreserved@a{@latex@error{Environment #1 undefined}@eha}}%
            {defreserved@a{def@currenvir{#1}%
            edef@currenvline{on@line}%
            csname #1endcsname}}%
            @ignorefalse
            begingroup@endpefalsereserved@a}
            defend#1{%
            csname end#1endcsname@checkend{#1}%
            expandafterendgroupif@endpe@doendpefi
            csname @afterend@#1@hookendcsname% <<-- Added this
            if@ignore@ignorefalseignorespacesfi}
            % Copied from etoolbox.sty
            DeclareRobustCommand{BeforeBeginEnvironment}[1]{%
            expandaftergapptocsname @beforebegin@#1@hookendcsname}
            DeclareRobustCommand{AfterEndEnvironment}[1]{%
            expandaftergapptocsname @afterend@#1@hookendcsname}
            newcommand{ifundef}[1]{%
            ifdefined#1%
            ifx#1relax
            expandafterexpandafter
            expandafter@firstoftwo
            else
            expandafterexpandafter
            expandafter@secondoftwo
            fi
            else
            expandafter@firstoftwo
            fi}
            DeclareRobustCommand{gappto}[2]{%
            ifundef{#1}
            {xdef#1{unexpanded{#2}}}
            {xdef#1{unexpandedexpandafter{#1}unexpanded{#2}}}}
            makeatother

            BeforeBeginEnvironment{verbatim}{begin{Shaded}}
            AfterEndEnvironment{verbatim}{end{Shaded}}

            begin{document}

            begin{verbatim}
            text
            end{verbatim}

            end{document}





            share|improve this answer















            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
            defbegin#1{%
            csname @beforebegin@#1@hookendcsname% <<-- Added this
            @ifundefined{#1}%
            {defreserved@a{@latex@error{Environment #1 undefined}@eha}}%
            {defreserved@a{def@currenvir{#1}%
            edef@currenvline{on@line}%
            csname #1endcsname}}%
            @ignorefalse
            begingroup@endpefalsereserved@a}
            defend#1{%
            csname end#1endcsname@checkend{#1}%
            expandafterendgroupif@endpe@doendpefi
            csname @afterend@#1@hookendcsname% <<-- Added this
            if@ignore@ignorefalseignorespacesfi}
            % Copied from etoolbox.sty
            DeclareRobustCommand{BeforeBeginEnvironment}[1]{%
            expandaftergapptocsname @beforebegin@#1@hookendcsname}
            DeclareRobustCommand{AfterEndEnvironment}[1]{%
            expandaftergapptocsname @afterend@#1@hookendcsname}
            newcommand{ifundef}[1]{%
            ifdefined#1%
            ifx#1relax
            expandafterexpandafter
            expandafter@firstoftwo
            else
            expandafterexpandafter
            expandafter@secondoftwo
            fi
            else
            expandafter@firstoftwo
            fi}
            DeclareRobustCommand{gappto}[2]{%
            ifundef{#1}
            {xdef#1{unexpanded{#2}}}
            {xdef#1{unexpandedexpandafter{#1}unexpanded{#2}}}}
            makeatother

            BeforeBeginEnvironment{verbatim}{begin{Shaded}}
            AfterEndEnvironment{verbatim}{end{Shaded}}

            begin{document}

            begin{verbatim}
            text
            end{verbatim}

            end{document}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 6 at 15:18

























            answered Jan 6 at 14:55









            Phelype OleinikPhelype Oleinik

            21.5k54381




            21.5k54381













            • @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



















            • @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

















            @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





            @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











            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






            share|improve this answer




























              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






              share|improve this answer


























                1












                1








                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 9 at 23:49









                egregegreg

                712k8618913175




                712k8618913175






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468824%2fhow-can-i-put-every-verbatim-environment-inside-a-shaded-environment-by-chan%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents