Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Let's say I am using some macro \foo that almost does what I want, but I actually want to add space either side of the output of \foo. I can do the following:

\let\tempfoo\foo
\renewcommand\foo{\hspace{1em}\tempfoo\hspace{1em}}

This redefines \foo to do what it did before, plus some extra space either side. Is there a way to "patch" environments in the same way? I guess the question is really how do I \let with environments?

share|improve this question
1  
I was searching for a question like this, but almost missed it because of the slightly verbose title. Would you consider changing it to something like Is there a \let for environments?? This would also make it more general – my actual use case is not a sneaky redefinition, but plain aliasing. –  doncherry Apr 10 '13 at 0:22
add comment

4 Answers

up vote 13 down vote accepted

An environment foo defines the two macros \foo and \endfoo. You can redefine both in the same as you already did:

\let\origfoo\foo
\let\endorigfoo\endfoo
\renewenvironment{foo}
    {<your stuff>\origfoo<your stuff>}
    {<your stuff>\endorigfoo<your stuff>}

However, using the package etoolbox with its macros makes life easier.

share|improve this answer
3  
Indeed, like this: \let\origfooenv\fooenv \let\endorigfooenv\endfooenv \renewenvironment{fooenv}{<your stuff>\origfooenv<your stuff>}{<your stuff>\endorigfooenv<your stuff>} –  Martin Scharrer Jun 29 '11 at 13:30
    
@MartinScharrer I took the liberty of adding your code to the answer; it was kind of hard to read as a comment. I hope that’s ok and feel free to delete both of our comments. –  doncherry Apr 10 '13 at 0:25
1  
@doncherry I had to adapt a bit for fitting to question and original answer of Herbert. –  Speravir Apr 10 '13 at 0:54
add comment

The most practical way is to exploit etoolbox's utilities:

\preto\foo{\hspace{1em}}
\appto\foo{\hspace{1em}}

The problem arises for commands with "apparent arguments", for example \section. If you look at the output of \show\section, you'll see that it has no argument whatsoever. It's a technique frequently employed in LaTeX, because it's more efficient. Actually \section is transformed into

\@startsection{...}{...}{...}{...}{...}

and \@startsection has six arguments; you see that \section supplied the first five, the sixth is what comes after \section (there's more to this, because there can be a * or an optional argument, but let's keep it simple).

Using the trick with \appto for a command like \section would result in a disaster, because the thing added will provide the sixth argument for \@startsection, which is clearly not what is wanted.

As Herbert says, you can use \preto and \appto also for environments, using \foo and \endfoo if the environment is foo, provided the environment has no argument. If the argument is part of the command (i.e. it is not "apparent") one can use \pretocmd and \apptocmd; if the argument is apparent (it's the case of \tabular, for example) \appto and \apptocmd must not be used, but \preto and \pretocmd are quite safe.

share|improve this answer
    
But the sneaky \let thing will work with \section won't it? Isn't that a point in favour of using that trick, rather than etoolbox? –  Seamus Jun 29 '11 at 14:08
    
@Seamus: Not for things after \tempsection. –  egreg Jun 29 '11 at 14:11
    
Ah right. I see. I only tried with something before it... –  Seamus Jun 29 '11 at 14:12
add comment

For environments, you can use \AtBeginEnvironment, \AtEndEnvironment, \BeforeBeginEnvironment, and \AfterEndEnvironment provided by etoolbox. etoolbox add some hooks for environments. You may also use \appto and \preto to patch \foo and \endfoo. The two methods work differently.

An example:

\documentclass{article}
\usepackage{etoolbox}

\newenvironment{foo}{(}{)}

\begin{document}
\begin{foo}foo\end{foo}

\BeforeBeginEnvironment{foo}{[}
\AfterEndEnvironment{foo}{]}
\begin{foo}foo\end{foo}

\appto\foo{\{}
\preto\endfoo{\}}
\begin{foo}foo\end{foo}

\end{document}
share|improve this answer
add comment

Another sneaky way is to add something at the beginning or the end of macros. LaTeX provides the command \g@addto@macro to append to an existing macro, whereas etoolbox provides commands both for prepending as well as appending. The following code modifies the \begin{document}.

\documentclass{article}
\makeatletter
\g@addto@macro{\document}{ Oh! I see!}
\makeatother
\begin{document}
\end{document}
share|improve this answer
2  
\g@addto@macro is almost the same as \appto, with the difference that \appto acts locally (its effect will disappear at the end of the group where it's issued), while \g@addto@macro acts always globally; etoolbox has \gappto for this. –  egreg Jun 29 '11 at 14:41
    
@egreg Thanks. I am aware the beauty though of \g@addto@macro is that it is built into the LaTeX kernel. –  Yiannis Lazarides Jun 29 '11 at 15:54
    
of course! I use it quite frequently, when etoolbox would be overkill. –  egreg Jun 29 '11 at 16:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.