3

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?

  • The \foreach loop is grouped. – Henri Menke Dec 12 '19 at 23:05
  • 1
    You might be interested in Is there a way to automatically transpose a matrix written in Latex? which shows many ways to create the transpose of a matrix. The sagetex solution 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
  • 2
    why \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. – David Carlisle Dec 13 '19 at 3:10
5

As Henri said in the comment, the \foreach loop is grouped, so it isn't really fit for this type of application. You can use expl3's \seq_use:Nn <seq var> {<thing>} to add <thing> between each item of <seq var>:

\documentclass{article}

\usepackage{amsmath,xparse}

\ExplSyntaxOn
\seq_new:N \l_trevion_tmp_seq
\NewDocumentCommand \Hor { m }
  { \trevion_pmatrix:nn {#1} { & } } % Use main command with arg and &
\NewDocumentCommand \Ver { m }
  { \trevion_pmatrix:nn {#1} { \\ } } % Use main command with arg and \\
\cs_new_protected:Npn \trevion_pmatrix:nn #1 #2
  {
    % Split the argument at each comma, and store in the seq var
    \seq_set_from_clist:Nn \l_trevion_tmp_seq {#1}
    \begin{pmatrix}
      % Use the seq var with #2 (& or \\) between each pair of items
      \seq_use:Nn \l_trevion_tmp_seq {#2}
    \end{pmatrix}
  }
\ExplSyntaxOff

\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}

enter image description here

|improve this answer|||||
5

It's easier if you use a command as separator, as then no loop is required.

enter image description here

\documentclass{article}

\usepackage{amsmath,pgffor}


\newcommand\Hor[1]{{\def\,{&}\begin{pmatrix}#1\end{pmatrix}}}
\newcommand\Ver[1]{{\def\,{\\}\begin{pmatrix}#1\end{pmatrix}}}

\begin{document}

\[
\Hor{1\,2\,3}
\]

\[
\Ver{1\,2\,3}
\]

\end{document}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.