Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 enviroment? 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}
share|improve this question
    
@Jake: But egreg's answer to what is the proper method of accesing a counter seems to indicate otherwise. Or, did I misunderstand his answer? –  Peter Grill Nov 18 '11 at 1:25
    
Oh yeah, you're right. Ah, so much left to learn... –  Jake Nov 18 '11 at 1:46
add comment

1 Answer

up vote 5 down vote accepted

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}%
}
share|improve this answer
    
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 2:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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