8

I'm looking for a way to write a "decorator" (in Python terminology) or a "wrapper" for LaTeX environments: a way to give addition behaviour with save of the notation. For example, before every \begin{quote} execute a code that will add a \hrule.

I could make it for a single command using \let:

\let\oldCommand\commandIWantToDecorate
\def\commandIWantToDecorate#1{<addition code>\oldCommand{#1}}

That will execute <addition code> without the need to change the command throughout the document.

Is there a similar way to decorate the whole environment? Maybe LaTeX creates some inner macros I can work with?

UPD the question Can I redefine a command to contain itself? doesn't solve my problem, because I'm interested in redefining environments, not solo macros.

The etoolbox package partly solve the problem, thanks @samcarter!

But in "education" reasons I'm interesting is there a way to do decorate without additional packages, using just (La)TeX pre-build commands?

11

The LaTeX kernel provides \g@addto@macro which works for the environment starter command as well, i.e. for the environment quote the start macro is \quote, so say \g@addto@macro\quote{foo} etc.

No extra packages are needed for this, apart from the specific additions that are to be made, but that depends on personal choices of the O.P, but in general 'anything' can be placed in \g@addto@macro\quote{...}.

\let\oldquote\quote
\renewcommand{\quote}{fooaddition\oldquote}

would work as well, but this may fail for other environments that have optional arguments. In this case, \LetLtxMacro from letltxmacro package is needed, which means another package, however.

\documentclass{book}

\makeatletter
\g@addto@macro\quote{\hrule
  \medskip

  \textit{be careful}

  \medskip

  Now the real stuff begins\dots

}
\makeatother    

\begin{document}

\begin{quote}
test
\end{quote}

\end{document}

enter image description here

10

Have a look at the etoolbox package, in particular to its \AtBeginEnvironment command. This allows you to add aditional code to the begin of environment.

\documentclass{book}

\usepackage{etoolbox}
\AtBeginEnvironment{quote}{\hrule}

\begin{document}

\begin{quote}
test
\end{quote}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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