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 \cr
s in the arguments.
This seems like it should be easy... what am I missing?
\foreach
loop is grouped.sagetex
solution given there will be helpful if you're doing extensive work with matrices. For example, here or here\cr
???\cr
is 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.