While the previous answers provide a perfectly okay way to do what you wanted, they do have the drawback that you need to write \& or whatever other replacement instead of the usual & (and then you can not use that specific macro for other purposes inside the argument). That is fine in most cases, but suppose one would want to preserve the regular syntax, there is nothing preventing them from making the & character active themselves, thereby replicating the usual behaviour of TikZ. It is not even too difficult:
\newcommand\mymatrix{%
\begingroup%
\long\def\tmp##1{%
\endgroup%
\begin{tikzpicture}
\matrix[matrix of math nodes] {##1};
\end{tikzpicture}
}%
\catcode`&=13
\tmp%
}
This we can use as desired:
\mymatrix{
a & b \\
e & f \\
}
So what is happening here? When TeX reads in the arguments of a macro, it uses the category codes in effect at the moment, and that is preserved in the resulting sequence of tokens. Technical details aside, in the naive approach we end up with tokens saying {name: &, catcode: 4} while TikZ would expect to get {name: &, catcode: 13}, where 13 means an active character. So to create the active & tokens TikZ expects, we need to change the category code before the respective characters are read. (It would also be possible to replace the characters with their active versions after the fact, but it would be needlessly complicated when we can just make the TeX engine parse them correctly to begin with.)
Hence, notice that \mymatrix as defined seemingly does not take any arguments. (We will nonetheless be able to use it as we would expect to though.) When TeX encounters the macro, it does not yet go ahead and parse the “argument” as it would otherwise. Instead, since no argument is expected, the definition is expanded immediately, and the result is placed at the front of the processing queue.
As TeX continues, we first start a new group with \begingroup to ensure any changes remain localized, in particular category code alterations (see the next paragraph). Then we define the temporary macro \tmp which will serve as our proxy to process the following block. But before it is called, the category code of & is changed to 13, i.e. it is made active. Then we call \tmp, which as defined expects a single argument, so TeX reads in the next part as it would have if \mymatrix took an argument, but now & is active as it should be.
Once TeX has read in the argument and converted it to tokens (preserving & as active), it expands \tmp to its definition and starts processing the resulting sequence of tokens. The very first thing we do is close the group started before, with \endgroup. Thus the category code of & changes back to its original value and the change we did doesn't affect anything else other than how our argument was parsed. (Incidentally, this also deletes the macro \tmp as that too was defined inside the group; we don't need it any more anyway, it has already been expanded.)
Finally, all that is left is to start the tikzpicture environment and call \matrix with the argument provided. At this point, the sequence of characters has already been parsed to a sequence of tokens, with & being interpreted as active, and that token sequence has been substituted in place of the ##1, so we are good to go, TikZ will not complain any more.
\long\def\tmp##1{with something like\NewDocumentCommand{\tmp}{O{}+m}{, and then use double dash to refer to these arguments, like##1. – tobiasBora Oct 07 '21 at 07:25\ensuremath\vcenter{\hbox{...}}}. This https://tex.stackexchange.com/a/611535/116348 suggests that I need to use environ or define my argument with\NewDocumentEnvironment{ABC}{O{}+b}, but then your method does not apply anymore. – tobiasBora Oct 07 '21 at 07:43\newcommandor\NewDocumentCommand. The issue is that if\tmpis defined, this will fail, but then you can work around this with an additional\ifcsnamecheck. – Taederias Oct 07 '21 at 11:50xparsedoes exactly when parsing the environment body, but I imagine that you would need to set (and reset) the catcode before (and after) for it to work, and hope nothing inside\NewDocumentEnvironmenttouches or relies on that. Alternatively, if one doesn't have thebargument specifier in the definition, they can simply change the catcode inside the begin block and be done with it, but your use case requires that. I will try and get back to you on that one (and potentially supplement my answer). – Taederias Oct 07 '21 at 11:59alignenvironment... If you know how to fix that, I created a new answer here https://tex.stackexchange.com/questions/619960/use-inside-commands – tobiasBora Oct 23 '21 at 12:17