I'm trying to write a macro to abstract different possible representations of matrices. In particular, we're changing whether we want to represent a particular matrix as a column or row vector. My hope was to do something like the following:
\documentclass{article}
\usepackage{amsmath,pgffor}
\newcommand\Hor[1]{\begin{pmatrix}\foreach \n [count=\ni] in {#1} {\ifnum \ni=1 \n \else &\n \fi}\end{pmatrix}}
\newcommand\Ver[1]{\begin{pmatrix}\foreach \n [count=\ni] in {#1} {\ifnum \ni=1 \n \else \cr\n \fi}\end{pmatrix}}
\begin{document}
\section*{Some things that work}
\[
\begin{pmatrix}
1 & 2 & 3
\end{pmatrix}
\]
\[
\begin{pmatrix}
1 \cr 2 \cr 3
\end{pmatrix}
\]
\section*{Some things that don't}
\[
\Hor{1,2,3}
\]
\[
\Ver{1,2,3}
\]
\end{document}
In particular, I'd like to be able to have a single macro which switches between row and column matrices, ergo using \foreach instead of just having &s or \crs in the arguments.
This seems like it should be easy... what am I missing?


\foreachloop is grouped. – Henri Menke Dec 12 '19 at 23:05sagetexsolution given there will be helpful if you're doing extensive work with matrices. For example, here or here – DJP Dec 12 '19 at 23:25\cr???\cris not supported syntax for any ams or standard latex matrix construct. It is a tex primitive that underlies\\so it sometimes works but it shouldn't be used in pmatrix ever. – David Carlisle Dec 13 '19 at 03:10