2

I have just started working with the package empheq as a way to create a box around equations. I would like to create a pair of short new commands for entering and exiting the empheq environment, but I am running into an error. For reference, here is a minimal example of code that works and that I would like to modify:

\documentclass{minimal} 
\usepackage{empheq}

\newcommand*\widefbox[1]{\fbox{\hspace{2em}#1\hspace{2em}}}

\begin{document}

\begin{empheq}[box=\widefbox]{align}
Y & =Ak^{\alpha}L^{1-\beta}\\
C & =(1-s)Y\\
Y & =C + I + G\\
\Delta K & = I -\delta K
\end{empheq}

\end{document}

Which produces:

minimal working example of boxed equation

I would like to create the same output but without having to type \begin{empheq}[box=\widefbox]{align} and \end{empheq} every time. So I have tried defining two new commands:

\newcommand{\eqnboxbegin}{\begin{empheq}[box=\widefbox]{align}}
\newcommand{\eqnboxend}{\end{empheq}}

The first command works as expected, but I have problems with second. That is, this code works:

\documentclass{minimal} 
\usepackage{empheq}

\newcommand*\widefbox[1]{\fbox{\hspace{2em}#1\hspace{2em}}}
\newcommand{\eqnboxbegin}{\begin{empheq}[box=\widefbox]{align}}

\begin{document}

\eqnboxbegin
Y & =Ak^{\alpha}L^{1-\beta}\\
C & =(1-s)Y\\
Y & =C + I + G\\
\Delta K & = I -\delta K
\end{empheq}

\end{document}

But this code produces an error:

\documentclass{minimal} 
\usepackage{empheq}

\newcommand*\widefbox[1]{\fbox{\hspace{2em}#1\hspace{2em}}}
\newcommand{\eqnboxbegin}{\begin{empheq}[box=\widefbox]{align}}
\newcommand{\eqnboxend}{\end{empheq}}

\begin{document}

\eqnboxbegin
Y & =Ak^{\alpha}L^{1-\beta}\\
C & =(1-s)Y\\
Y & =C + I + G\\
\Delta K & = I -\delta K
\eqnboxend

\end{document}

The error message reads:

paragraph ended before \empheq was complete.

I cannot figure this out. Thanks in advance for your help!

  • What exactly is the benefit from writing it as macros and not as environments as Troy suggests. Imo environments provide a much more readable code, better separation. Plus with good editors (eg emacs) environments are easy to change. Hints like this becomes quite the hassle in the long run. – daleif Nov 8 '14 at 11:21
  • I write a lot documents that require me to go into and out of the same environments frequently. For example, lecture notes contain a lot of itemize and enumerate environments. Instead of typing out begin{itemize} and end{itemize}, I instead only have to type \iz and \zi. True, code is less readable, but the time saving is big and I'm not sharing this code with anyone. – Brian J Nov 8 '14 at 16:18
  • I would suggest you find yourself a better editor. For example one that can handle user defined templates. In my editor I have a shortcut for a standard preamble, stdpkg after which I just hit Tab, and it expands to the true meaning. Again it makes this a lot more maintainable not just for you, but also to those you later on passes your files on to. – daleif Nov 8 '14 at 20:28
  • OK thanks a lot for the tip. I appreciate it. – Brian J Nov 8 '14 at 21:42
1

In order to do what the empheq environment does, (I suspect) they are collect all of the material between \begin{empheq} and \end{empheq} into a box. If the delimiter that terminates the box collection is hidden in unexpanded macros, the collection continues until the end of the document.

This new environment works for me:

\newenvironment{empheqbox}{%
    \empheq[box=\widefbox]{align}%
}{%
    \endempheq%
}

You'll notice that I'm using the direct environment commands for empheq because using the environment form also generated the runaway argument error.


Update

After reading the empheq package documentation, which references this document, it is indeed AmS's underlying mechanism for storing the environment's content that is the source of the error.

This has to do with unfortunately nontrivial technical complications: the given environments must read their contents as a delimited macro argument because they do multipass processing of the contents using algorithms inherited from Spivak’s amstex.tex. The obvious solution—substitution of different algorithms that do box shuffling instead of token shuffling for the multipass calculations—would require rewriting these display environments from the ground up; while that is a worthy goal, it was beyond the original scope of the AmS-LaTeX project.

So it appears that \begin{} and \end{} are compulsory for robust usage.

The empheq author notes this and suggests that my environment above actually be written as

\newenvironment{empheqbox}{%
    \setkeys{EmphEqEnv}{align}
    \setkeys{EmphEqOpt}{box=\widefbox}
    \EmphEqMainEnv%
}{%
    \endEmphEqMainEnv%
}
  • Thanks Troy. I have tried to implement your solution by the problem seems to persist. The environment you defined works well, but when I try to define new commands to get around typing \begin and \end, I get the same issue. But I'm still better off now than when I started. Thanks! – Brian J Nov 8 '14 at 4:44
  • Indeed. Please see my more complete answer I have just edited. It seems you need to use \begin and end. – TroyHaskin Nov 8 '14 at 5:23

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.