5

Given some columns of an array as comma-separated lists, how can I construct the array?

I figure if I can extract a specific row from a comma-separated list of such columns, then I'll probably be able to do the rest, but I can't even get the following code to compile:

\documentclass{article}
\usepackage{tikz}

\def\colzero{0,0,1}
\def\colone{0,1,0}
\def\coltwo{0,1,1}

\newcommand{\getrow}[2]{%
    \xdef\rowans{}
    \foreach \x in #2 {
        \xdef\rowans{\rowans \pgfmathparse{{\x}[#1]}\pgfmathresult}
        }
    \rowans
}

\begin{document}
$\getrow{0}{{\colzero},{\colone},{\coltwo}}$
\end{document}

I somehow managed to figure out how to do this once before (using various things I found here on StackExchange), but now I just can't find any answers related to what I'm looking for.

Ideally I'm looking to do this with a variable number of columns, though not necessarily with a variable number of rows.

  • Welcome to TeX.SX! – user31729 Jun 5 '15 at 16:26
  • Do not put \pgfmathparse inside an \edef/\xdef. Do \pgfmathparse{{\x}[#1]}\xdef\rowans{\rowans\pgfmathresult} instead, for example. If you just do want to typeset the result, you don't need any \xdef or \rowans you can just do that in the loop: \pgfmathprint{{\x}[#1]}. – Qrrbrbirlbel Jun 5 '15 at 16:43
  • I still get the same error when I replace the relevant section with your suggestion: "Paragraph ended before \pgffor@next was complete". – Yiab Jun 6 '15 at 12:57
3

I'd suggest using expl3 and xparse; you define columns with a \setcolumn command and use \getrow that gathers the items in the chosen row and produces them with a separator given in the final argument.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\setcolumn}{mm}
 {
  \clist_if_exist:cF { g_ylab_column_#1_clist }
   {
    \clist_new:c { g_ylab_column_#1_clist }
   }
  \clist_gset:cn { g_ylab_column_#1_clist } { #2 }
 }

\NewDocumentCommand{\getrow}{mmm}
 {
  \seq_clear:N \l_ylab_row_seq
  \clist_map_inline:nn { #2 }
   {
    \seq_put_right:Nx \l_ylab_row_seq
     {
      \clist_item:cn { g_ylab_column_##1_clist } { #1 }
     }
   }
  \seq_use:Nn \l_ylab_row_seq { #3 }
 }
\ExplSyntaxOff

\setcolumn{colone}{0,0,1}
\setcolumn{coltwo}{0,1,0}
\setcolumn{colthree}{0,1,1}

\begin{document}

\getrow{1}{colone,coltwo,colthree}{}

\getrow{2}{colone,coltwo,colthree}{, }

\begin{tabular}[t]{ccc}
\getrow{1}{colone,coltwo,colthree}{&} \\
\getrow{2}{colone,coltwo,colthree}{&} \\
\getrow{3}{colone,coltwo,colthree}{&}
\end{tabular}

\end{document}

A \setcolumn command defines a clist variable if not already existent and (globally) sets it. You'll call it by name in a \getrow command that sets a seq variable with the items in the specified row. Then it produces the sequence with the chosen separator between items.

enter image description here

Note that, in expl3 counting of items generally starts from 1 (which is more logical than starting from 0).

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.