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
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:

What it should look like:

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
add a comment |
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:

What it should look like:

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
add a comment |
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:

What it should look like:

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
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:

What it should look like:

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
macros formatting verbatim framed backgrounds
edited Jan 6 at 14:37
finefoot
asked Jan 6 at 14:32
finefootfinefoot
215110
215110
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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}

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.
defVerbatimEnvironment{gdefFV@EnvironName{verbatim}}can be more simply justVerbatimEnvironment.
– egreg
Jan 9 at 23:30
@egreg, answer edited, thanks.
– jfbu
Jan 10 at 8:47
add a comment |
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}
@Jayjayyyetoolboxis 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
add a comment |
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.

add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}

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.
defVerbatimEnvironment{gdefFV@EnvironName{verbatim}}can be more simply justVerbatimEnvironment.
– egreg
Jan 9 at 23:30
@egreg, answer edited, thanks.
– jfbu
Jan 10 at 8:47
add a comment |
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}

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.
defVerbatimEnvironment{gdefFV@EnvironName{verbatim}}can be more simply justVerbatimEnvironment.
– egreg
Jan 9 at 23:30
@egreg, answer edited, thanks.
– jfbu
Jan 10 at 8:47
add a comment |
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}

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

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.
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 justVerbatimEnvironment.
– egreg
Jan 9 at 23:30
@egreg, answer edited, thanks.
– jfbu
Jan 10 at 8:47
add a comment |
defVerbatimEnvironment{gdefFV@EnvironName{verbatim}}can be more simply justVerbatimEnvironment.
– 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
add a comment |
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}
@Jayjayyyetoolboxis 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
add a comment |
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}
@Jayjayyyetoolboxis 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
add a comment |
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}
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}
edited Jan 6 at 15:18
answered Jan 6 at 14:55
Phelype OleinikPhelype Oleinik
21.5k54381
21.5k54381
@Jayjayyyetoolboxis 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
add a comment |
@Jayjayyyetoolboxis 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
add a comment |
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.

add a comment |
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.

add a comment |
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.

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.

answered Jan 9 at 23:49
egregegreg
712k8618913175
712k8618913175
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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