I am trying to write a macro that has a different behavior when called from text mode, math mode, display math, and math tables. The first three are easy enough with \ifmmode and \mathchoice. But the last one is a bit more complicated as I have to test for various LaTeX- and AMS-environments: array, array*, gather, align etc. However, as all those environments are implemented as variants of \halign, the cleanest way would be to check if the macro is called from within a \halign.

Hence my question: is there a way to test if a macro is called from \halign ?

(this is a user-level macro that will very probably never be called from inside \noalign, so no need to put an extra test for this case).

Edit: I found a quasi-answer to this, by which I mean that it works in all the above AMS environments but not in a plain \halign. Namely, in plain math, \math@cr@@@ is a macro expanding to \cr, but it is changed in the aligned environments. So I only have to do something like

\ifx\math@cr@@@\plain@cr ... \else ... \fi

closed as unclear what you're asking by egreg, Martin Schröder, jubobs, Peter Jansson, Svend Tveskæg Jan 20 '14 at 17:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Welcome to TeX.SX! Can you give an example of this macro and of what it should do in the various cases? – egreg Nov 21 '13 at 10:36
  • I'm trying to write for example a macro \macro such that $$x \macro$$ expands to x display and \begin{align} x \macro \end{align} etc. expands to x align. – Jérôme Circonflexe Nov 21 '13 at 12:15
  • I tend to agree with @egreg that we need to know what you are after. Very likely there is another solution to your real problem than testing for \halign. Usually you don't need to step so deep in TeX core. – boycott.se - yo' Nov 21 '13 at 12:16
  • Ok, would \makeatletter\def\currentenvironment{\@currenvir}\makeatother and then using \currentenvironment do the trick? Of course, you can't use $$ ... $$ syntax, but that is obsolate in LaTeX anyways, in favour of \[ ... \]. – boycott.se - yo' Nov 21 '13 at 12:17
  • I'm not sure, but maybe something like \ifmmode\else\ifinner <now in halign>\fi\fi? (though it could be also in a \hbox then) – morbusg Nov 21 '13 at 12:19

Browse other questions tagged or ask your own question.