1

I would like to create a System of linear equations indeed with pgfplotstable.

How can I add names of variables (x_1, x_2,... or something else: x,y,z), if some strings have been replaced?

string replace={0}{}, string replace={-1}{-x}, string replace={1}{x}

enter image description here

\documentclass[border=5pt, varwidth]{standalone}
\usepackage{pgfplotstable, systeme}
 \pgfplotsset{compat=1.13}

\pgfplotstableset{
string type,%
%header=false, 
every head row/.style={output empty row},%
column type=r,%
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={$}{$},
},%
}

\begin{document}
\pgfplotstableread{
x                    y
-1                   0
0                    1
1                    2
3                    3 
0                    4
13                  5
-6                   6
-\frac{3}{2}    7
-12                 8
}\test


Actual:  \pgfplotstabletypeset[]{\test} 
%
Partially replaced:  \pgfplotstabletypeset[
string replace={0}{},
string replace={-1}{-x},
string replace={1}{x},
]{\test} \\

\bigskip
Target:  $\newcolumntype{R}{>{{}}r<{{}}}
\setlength\arraycolsep{1pt}
\begin{array}{ r R r R}
-                      &x_1 &    &            \\
{}                    & {} &    &x_2           \\
{}                    &x_1 &+2 &x_2           \\
3                     &x_1 &+3 &x_2           \\
{}                   & {} &+4 &x_2           \\
{}  13             &x_1  &+5 &x_2           \\
- 6                 &x_1 &+6 &x_2           \\
-\frac{3}{2}    &x_1 &+7 &x_2           \\
-12                &x_1 &+8 &x_2           \\
\end{array}$

\end{document}
2
  • It would probably be easier to create a new column. Commented Oct 18, 2018 at 16:30
  • @JohnKormylo Nice idea. The problem is maybe, there are sometimes x, sometimes not. It would good, if I could get the coulmn-head (x, y, ... oder x_1, x_2,... for example) and use it in 'string replace'... Mmmhh....
    – cis
    Commented Oct 18, 2018 at 20:41

1 Answer 1

2

This is only a partial solution because I don't know how to treat the sign of the second column if the number is not a number but a math expression (for example if you have -\fraq{...}{...} in the second column).

However, I think it could be useful to you.

With \pgfplotstablemodifyeachcolumnelement and etoolbox I have modified the column values:

\documentclass[border=5pt, varwidth]{standalone}
\usepackage{etoolbox}
\usepackage{pgfplotstable, systeme}
\pgfplotsset{compat=1.13}

\pgfplotstableset{
string type, 
every head row/.style={output empty row},
column type=r,%
%postproc cell content/.append style={
%/pgfplots/table/@cell content/.add={$}{$},
%},%
}

\begin{document}
\pgfplotstableread{
x                    y
-1                   0
0                    1
1                    2
3                    3 
0                    4
13                  5
-6                   6
-\frac{3}{2}    7
-12                 8
}\test

\newcommand{\firstvar}{x_1}
\newcommand{\secondvar}{x_2}
\pgfplotstablemodifyeachcolumnelement{x}\of\test\as\cell{%
\edef\cell{% code from https://tex.stackexchange.com/questions/24922/comparing-an-argument-to-a-string-when-argument-is-a-result-of-a-command-with-et
 \expandafter\ifstrequal\expandafter{\cell}{0}{}{\expandafter\ifstrequal\expandafter{%
        \cell}{-1}{$-\firstvar$}{%
        \expandafter\ifstrequal\expandafter{\cell}{1}{$\firstvar$}{$\cell\firstvar$}%
}}}
}
\pgfplotstablemodifyeachcolumnelement{y}\of\test\as\cell{%
\edef\cell{% this part is to improve to correctly manage the negative value
 \expandafter\ifstrequal\expandafter{\cell}{0}{}{\expandafter\ifstrequal\expandafter{%
        \cell}{-1}{$-\secondvar$}{%
        \expandafter\ifstrequal\expandafter{\cell}{1}{$+\secondvar$}{$+\cell\secondvar$}%
}}}
}
\pgfplotstabletypeset{\test}
\end{document}

enter image description here

You must log in to answer this question.

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