2

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
0

The question is still a bit unclear but I think you want to make global definitions (so they escape the alignment cells) and don't add groups (which would break the alignment layout)

enter image description here

\documentclass[12pt]{article}
\usepackage{color,amsmath}
\def\A{\alpha_0}\def\B{\beta_0}
\def\mymath#1#2{\gdef\A{\alpha}\gdef\B{\beta}\sum #1=[#2]}
\def\mymathb#1#2{\def\gA{\gamma}\gdef\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}

Note that as every alignment cell is a group such definitions have to be global however (as discussed in the blkarray package documentation) it is possible to give the appearance of local scope (eg to the set of cells in a single row) by clearing the global definitions "by hand" at the end of the desired scope..

  • Hi, thanks for trying to answer even if my question is unclear! Let me try to explain better: the example with the colors is just to have something simple, but my goal is to have local definitions and allow flexibility in how the arguments are broken into columns and rows – Bordaigorl Jun 9 at 19:10
  • @Bordaigorl they can not be local as they have to escape the alignment groups, but you could maintain a global list of restores and restore things "by hand" (see blkarray documentation for a discussion of techniques for managing scope within an alignment – David Carlisle Jun 9 at 19:50
  • Wow blkarray is an interesting read! Thanks for this. I think "It cannot be done, unless you globally apply your definitions and then clean them up explicitly" is a valid answer – Bordaigorl Jun 10 at 21:59
  • @Bordaigorl I added some words:-) – David Carlisle Jun 10 at 22:04
  • Ah! I found a workaround that works if one wraps groups around each argument \def\Gmymath#1#2{{\def\A{\alpha}\def\B{\beta}\sum #1}={\def\A{\alpha}\def\B{\beta}[#2]}} and using \def\alignch{&} and then \Gmymath{\A\aftergroup\alignch}{\B} – Bordaigorl Jun 16 at 17:29

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.