# Pass part of halign body as an argument to macro (previously AMS align and its groups)

I originally thought this had to do with the align environment but I discovered it's a feature of \halign, hence the major edit.

When inside a tabular environment or simple halign, I need to be able to pass parts of it (spanning multiple columns and rows) to a macro which would process it and put it right back (for a simple example, place some delimiter on the right and left of the content). It works except when there is something typeset before the macro in the same cell. Here's a MWE:

\documentclass{article}

\def\foo#1\bar{$\rightarrow$ #1 $\leftarrow$}

\begin{document}

\halign{ # & # \cr
\foo foo & bar \bar \cr
%
\foo foo & bar \cr
foo & bar \bar \cr
%
foo \foo foo \bar & \foo bar \bar \cr
%  foo \foo foo  & bar \bar \cr % error 'Forbidden control sequence found while scanning use of \foo'
}

\end{document}


Using a non-delimited macro like \def\foo#1{ whathever #1} I cannot span multiple rows (I can't pass it \cr even if I define it as \long). Can I achieve what I want using an environment, if so---how?

P.S. What I'm trying to do is find a flexible solution to put a fancy frame certain parts of equations inside an ams align environment and I decided to write my own in TikZ following @Marco Daniel's example in How to box two lines inside align*. In the end, I need to grab the whole content of the framed box (which may start anywhere within a cell, and span multiple cells and rows), so I can calculate it's height and depth properly.

• Could you not use something like tikzmark here? – cfr Apr 20 '16 at 2:11

I don't know what you're doing, so this may not be of any use. But you can use tikzmark to mark up points in maths or text and then overlay annotations in TikZ. For example, some simple frames:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit,calc}
\def\bmarkeq#1\emarkeq{#1}

\begin{document}
\begin{align}
\bmarkeq\tikzmark{a1} \underbrace{foobar}_{barbaz} & & foobar \tikzmark{a2}\emarkeq \\
\bmarkeq\tikzmark{b1} \underbrace{foobar}_{barbaz\tikzmark{b3}} & & foobar \tikzmark{b2}\emarkeq foo \\
foo & & \bmarkeq\tikzmark{c1} \underbrace{foobar}_{barbaz\tikzmark{c3}} & & foobar \tikzmark{c2}\emarkeq \\
% typing any text BEFORE \bmarkeq in the SAME group/column fails
% foo \bmarkeq \underbrace{foobar}_{barbaz} & & foobar \emarkeq \\
\end{align}
\begin{tikzpicture}[overlay,remember picture]
\foreach \i in {a1,a2,b1,b2,b3,c1,c2,c3} \coordinate (\i) at ({pic cs:\i});
\node (a) [fit=(a1) (a2) (b1) (b2) (b3), draw=blue, inner xsep=5pt, inner ysep=7.5pt, yshift=5pt] {};
\draw [magenta, inner sep=5pt] ($(c1)!1/2!(c1 |- b3)$) rectangle (c2 |- c3);
\node (c) [fit=(a) (c1) (c2) (c3), draw=green, thick, rounded corners] {};
\end{tikzpicture}
\end{document}


• This looks good, I tried defining separate \bmarkeq and \emarkeq to simply place numbered marks, but to get a box of a proper height, it requires placement of intermediate marks in order to get the position of the top and bottom of the equation. I want to grab the whole equation as an argument, so I can calculate its height and depth and do all the work from there, so in the end I would only need to do something like \bmarkeq <some tall equation, maybe more than 1 line> \emarkeq, which leads me to the original problem... – Aayla Secura Apr 21 '16 at 4:44