3

I would like to enclose multiple lines in an align in a group. Because of the group, the \\ delimiting the rows generates a mistake (if I'm guessing right the group is broken at \\ and latex complains about unclosed groups).

My goal is to have a macro \mymath that typesets some complex formula. The formula is a combination of fixed symbols and optional symbols that are defined via local macros (\A and \B) in the MWE. I would like the definitions to be local to the arguments of the \mymath macro, but I would also like the ability to break lines and align content from "outside" the macro, i.e. without baking in align or similar environments inside the macro itself.

MWE

\documentclass[12pt]{article}
\usepackage{amsmath}
\def\A{\alpha_0}\def\B{\beta_0}
\def\mymath#1#2{\def\A{\alpha}\def\B{\beta}\sum #1=[#2]}
\def\mymathb#1#2{\def\A{\gamma}\def\B{\delta}\sum #1=[#2]}
\def\Gmymath#1#2{{\def\A{\alpha}\def\B{\beta}\sum #1=[#2]}}
\def\Gmymathb#1#2{{\def\A{\gamma}\def\B{\delta}\sum #1=[#2]}}

\begin{document}
\begin{equation}
 \mymath{\A}{\B}
 +\A % want alpha_0 here
 \mymathb{\A}{\B}
\end{equation}
\color{red}
\begin{align*}
 \mymath{\A&}{5\B}\\
 \mymathb{\A&}{7\B\\&\qquad\A}
\end{align*}
\color{blue}
\begin{multline*}
 \mymath{\A \B}{long+\\\B superlong \A}
\end{multline*}
\end{document}

If I use \mymath the definitions of \A and \B are lost once I use \\, and are not local to the argument only.

If I use \gmymath the extra group make align and friends very confused about the \\.

Question: Is there a way in which \mymath can make the \\ "leak" the group? The same story applies to alignment characters &.

  • Have you tried \def\bluemath#1{{\color{blue}\begin{gathered}#1\end{gathered}}}? – Mico Nov 17 '18 at 13:24
  • @Mico that would fix the possible uses: for example I would not be able to use &. The intent is to have the user of \bluemath to be able to choose the math environment from outside. For example one may want to do \begin{align} \bluemath{a&b}\\\bluemath{c&d}\end{align} if you use an inner aligned you can't align across rows anymore – Bordaigorl Nov 17 '18 at 15:33
  • each cell of an alignment is a group, you should think of & as a special form equivalent to }{ so basically what you ask is not possible. – David Carlisle Nov 17 '18 at 16:20
  • 1
    if you remove the group, there is no error but the color only extends as far as the first & so "I know that the effect can be achieved without the groups," is rather misleading. – David Carlisle Nov 17 '18 at 16:22
  • I'm not sure I fully understand your typesetting objective. (Actually, I'm pretty sure that I don't fully understand that you're trying to achieve...) In multi-line environments such as align and gather, each row is a group onto itself. Hence, if you can't issue the instruction \color{blue} globally, you'll have to issue it separately for each and every row that's supposed to get a non-default color. – Mico Nov 17 '18 at 18:02

Your Answer

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

Browse other questions tagged or ask your own question.