6

Background:

I just recently saw that one could define multiple columns in a tabular or array using the syntax:

*{num}{<align>} 

So, for example \begin{tabular}{r*{3}{l}r} is the identical to \begin{tabular}{rlllr}, and similarly for array.

Problem:

I used this to produce a solution to Best way to create an system of equations environment? where the number used was provided as a parameter. This works fine if the specified parameter is one less than the number of variables.

So, I attempted to modify this to accept the actual number of variables, and set that to a counter value, decrement it by one, and use the new value of the counter to specify how many of these were to be used. I suspect that this is an expansion issue (which I haven't quite gotten the hang of).

Alternatively, if I could somehow perform a decrement operation and apply that value to the column specification would be fine as well.

Incorrect Result:

Currently, the code below produces: enter image description here

Desired Result:

I would like to use something equivalent to the intent of the commented \begin{array} that uses a value of one less than the provided parameter. So when this works as desired, the output should be (note the shift in the right vertical bar): enter image description here

Code:

\documentclass[border=5pt]{standalone}
\usepackage{xcolor}
\usepackage{array}
\usepackage[paperwidth=4.7in]{geometry}

\newcounter{NumberOfRepeatedColumns} \newenvironment{MySystem}[2]{% <num+1> <r|l|c> \setcounter{NumberOfRepeatedColumns}{#1} \addtocounter{NumberOfRepeatedColumns}{-1} \begin{array}{c| #1{#2} | c}% want to replace this with something equivalent to line below %\begin{array}{c| {\the\value{NumberOfRepeatedColumns}}{#2}}% \the\value{NumberOfRepeatedColumns}\ }{% \end{array}% }

\newcommand{\data}{123 & abc & de & f \1 & a & def & fg}%

\begin{document} \color{red} $\begin{MySystem}{4}{r} \data \end{MySystem}$ \hspace{0.5in} $\begin{MySystem}{4}{l} \data \end{MySystem}$

\color{blue} $\begin{MySystem}{3}{r} \data \end{MySystem}$ \hspace{0.5in} $\begin{MySystem}{3}{l} \data \end{MySystem}$ \end{document}

Peter Grill
  • 223,288

1 Answers1

8

You can use

\newcounter{NumberOfRepeatedColumns}
\newenvironment{MySystem}[2]{% <num+1> <r|l|c>
    \setcounter{NumberOfRepeatedColumns}{#1}
    \addtocounter{NumberOfRepeatedColumns}{-1}
    \begin{array}{c| *{\theNumberOfRepeatedColumns}{#2} | c}
}{%
    \end{array}%
}

or even better, without the extra counter, and \numexpr:

\newenvironment{MySystem}[2]{% <num+1> <r|l|c>
    \begin{array}{c| *{\numexpr#1-1\relax}{#2} | c}
}{%
    \end{array}%
}
Gonzalo Medina
  • 505,128
  • Thanks. I had tried that, but I guess my real problem was that I was missing the c for the last column. So \begin{array}{c| *{\the\value{NumberOfRepeatedColumns}}{#2} | c} works as well. – Peter Grill Nov 18 '11 at 02:11