3

I have the following:

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}

\pgfkeys{
        /tikz/gaussenv/.cd,
        % column number
        col/.initial=1,
        colC/.initial=4,
}

\newenvironment{gaussMatrix}[1][]%
{
        \pgfkeys{/tikz/gaussenv/.cd, #1}
        \pgfkeys{/tikz/gaussenv/colC=\pgfmathparse{int(\pgfkeysvalueof{/tikz/gaussenv/col}-1)}\pgfmathresult}
        %\pgfkeysvalueof{/tikz/gaussenv/colC}
        \begin{pNiceArray}[name=mymatrix]{*{\pgfkeysvalueof{/tikz/gaussenv/colC}}{C}|C}
        }%
        {
        \end{pNiceArray}
}


\begin{document}
\begin{align*}
        \begin{gaussMatrix}[col=5,colC=4] %number of colums in total, number of columns left of the line
                1 & 1 & 1 & 1 & 1 \\
                2 & 2 & 2 & 2 & 2 \\
                3 & 3 & 3 & 3 & 3 \\
                4 & 4 & 4 & 4 & 4 \\
                5 & 5 & 5 & 5 & 5 \\
        \end{gaussMatrix}
\\
\end{align*}

\end{document}

But somehow nicematrix doesn't like the output of pgfkeys. Without the calculation somehow it works, but I'd like to remove it that the total number of columns -1 has to be specified (even though this is easy to calculate).

Does someone know why this does not work and maybe a way to fix it?

1 Answer 1

3

You need to distinguish between some combination of commands that can be used to typeset a number, and some macro that can be used to store that number. You can use \pgfmathsetmacro to create a macro:

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}

\pgfkeys{
        /tikz/gaussenv/.cd,
        % column number
        col/.initial=1,
        colC/.initial=4,
}

\newenvironment{gaussMatrix}[1][]%
{
        \pgfkeys{/tikz/gaussenv/.cd, #1}
        \pgfmathsetmacro{\mycolC}{int(\pgfkeysvalueof{/tikz/gaussenv/col}-1)}
        \pgfkeys{/tikz/gaussenv/colC=\mycolC}
        \begin{pNiceArray}[name=mymatrix]{*{\pgfkeysvalueof{/tikz/gaussenv/colC}}{C}|C}
        }%
        {
        \end{pNiceArray}
}


\begin{document}
\begin{align*}
        \begin{gaussMatrix}[col=5,colC=4] %number of colums in total, number of columns left of the line
                1 & 1 & 1 & 1 & 1 \\
                2 & 2 & 2 & 2 & 2 \\
                3 & 3 & 3 & 3 & 3 \\
                4 & 4 & 4 & 4 & 4 \\
                5 & 5 & 5 & 5 & 5 \\
        \end{gaussMatrix}
\\
\end{align*}

\end{document}

enter image description here

This assumes that you have a reason to store the result in a pgf key. If this is not the case, you can use a shorter code.

\documentclass[10pt, a4paper]{article}

\usepackage{nicematrix}

\pgfkeys{
        /tikz/gaussenv/.cd,
        % column number
        col/.initial=1,
        colC/.initial=4,
}

\newenvironment{gaussMatrix}[1][]%
{
        \pgfkeys{/tikz/gaussenv/.cd, #1}
        \begin{pNiceArray}[name=mymatrix]{*{\the\numexpr\pgfkeysvalueof{/tikz/gaussenv/col}-1}{C}|C}
        }%
        {
        \end{pNiceArray}
}


\begin{document}
\begin{align*}
        \begin{gaussMatrix}[col=5,colC=4] %number of colums in total, number of columns left of the line
                1 & 1 & 1 & 1 & 1 \\
                2 & 2 & 2 & 2 & 2 \\
                3 & 3 & 3 & 3 & 3 \\
                4 & 4 & 4 & 4 & 4 \\
                5 & 5 & 5 & 5 & 5 \\
        \end{gaussMatrix}
\\
\end{align*}

\end{document}
3
  • Thanks, the only reason why I stored this in a new key was that I didn't got it working without (but on the other side with a new key too). 1) Would you be so kind and explain why \pgfmathparse didn't work? 2) Thanks for helping me with the creation of that environment, if you're interested in looking with what I came up now, see gitlab.com/AtticusSullivan/gaussenv ;)
    – atticus
    Jun 6, 2020 at 17:36
  • 2
    @atticus The combination \pgfmathparse{int(\pgfkeysvalueof{/tikz/gaussenv/col}-1)}\pgfmathresult can be used to typeset the number, but it is not a macro that expands to that number. (pgf keys are somewhat forgiving in this regard but not to this extent.) You may be interested in this related discussion. And thanks for the link!
    – user194703
    Jun 6, 2020 at 17:41
  • Thanks for the explanation.
    – atticus
    Jun 6, 2020 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .