I have the following code which doesn't work:
\documentclass[12pt]{article}
\tracingcommands=2
\tracingmacros=2
\tracingall
\makeatletter
\let\sep\relax
\def\put@stack@#1#2{\edef#2{#1\sep#2}}
\def\put@queue@#1#2{\edef#2{#2\sep#1}}
\def\get@#1#2{\expandafter\get@@#2\endget@@#1#2}
\def\get@@#1\sep#2\endget@@#3#4{\edef#3{#1}\edef#4{#2}}
\def\empty@stack{\sep}
\newtoks\piecetoks
\def\leftrightbr{
\let\o@left\left
\let\o@right\right
\let\piece@nd\relax
\let\pieces@stack\empty@stack
\let\delims@stack\empty@stack
\let\on@end@line\empty@stack
\let\on@begin@line\empty@stack
\def\left##1{
\piece@nd
\let\piece@nd\endgroup
\def\on@first@right{
\put@stack@{\the\toks0}{\pieces@stack}
}
\put@stack@{##1}{\delims@stack}
\let\repl\relax
\let\vphantomer####1{}
\put@stack@{\the\piecetoks}{\pieces@stack}
\put@stack@{\right.}{\on@end@line}
\put@stack@{\left.}{\on@begin@line}
\piecetoks=\begingroup
}
\def\right##1{
\endgroup
\on@first@right
\let\on@first@right\relax
\get@\@dummy\on@end@line
\get@\@dummy\on@begin@line
\get@\@lpart\pieces@stack
\get@\@ldelim\delims@stack
\ifx\@lpart\empty
\repl
\else
\edef\repl{\o@left\@ldelim\@lpart\vphantomer{\the\piecetoks}\repl\the\piecetoks\vphantomer{\@lpart}\o@right##1}
\fi
}
\def\mathbr{
\piece@end
\put@stack@{\piece}{\pieces@stack}
\let\vphantomer\vphantom
\edef\repl{\on@end@line \\ \on@begin@line}
\def\piece\begingroup
}
}
\begin{document}
\begin{equation}
\leftrightbr
\left(a\right)
\end{equation}
\end{document}
So what's the catch?
EDIT: The error is
! Undefined control sequence.
<argument> \@dummy
l.85 \left(
a\right)
Looking into log gets us to conclusion that the problem is caused by call to \get@\@dummy\on@end@line
. This seems to be strange since direct call to \get@ on \empty@stack results in no error.
EDIT: The code can be narrowed down:
\documentclass[12pt]{article}
\tracingcommands=2
\tracingmacros=2
\tracingall
\newtoks\piecetoks
\def\leftrightbr{
\def\left##1{
\piecetoks=\begingroup
}
\def\right##1{
\endgroup
}
}
\begin{document}
\begin{equation}
\leftrightbr
\left(a\right)
\end{equation}
\end{document}
This is shorter, isn't it? :-) It gives the other error, and the other question arises: how can we achieve behavior in which TeX captures tokens between two macros' calls?
Undefined control sequence \@some
with the second code. It would be beneficial if you explained what the macros are supposed to do.\@some
to\some
both files run without errors.\left
expects a\right
, so you can gather all tokens between them. But, if you plan to use this withamsmath
alignments, I can see many other problems.