macros – Write a Macro Called with \begin and \end

macros

I would like to write a macro scaleblock which takes 2 arguments title and content. It can be called like:

\begin{scaleblock}{a_title}
   a_content
\end{scaleblock}

And it is equivalent to the follows:

\begin{block}{\scalebox{0.8}{a_title}}
  \scalebox{0.8}{\vbox{a_content}}
\end{block}

Does anyone know how to write this kind of macro (with \begin and \end)?

Best Answer

You can define an environment just as you would define a macro. Only instead of saying \newcommand\mycmd[1]{...#1...} you must write

\newenvironment{myenv}[1]{Begin...#1...}{End}

Here the last two parameters say what should be put before and after the contents.

This means the first try would be to write

\newenvironment{scaleblock}[1]{\begin{block}{\scalebox{0.4}{#1}}
    \scalebox{0.4}{\vbox{}%
  {}}
\end{block}}

But this does not reflect the intended nesting of parameters and you actually only give the begin part of your environment, while the end part is the newline character in the fourth line.

So in your case it is probably better to first collect everything inside the environments body and then use it. This can be done with the environ package. It provides the command \NewEnviron where you can use \BODY to access those content. Assuming block is the one from beamer, you can do the following:

\usepackage{environ}
\NewEnviron{scaleblock}[1]{\begin{block}{\scalebox{0.8}{#1}}
  \scalebox{0.8}{\vbox{\BODY}}
\end{block}}
Related Question