I am writing a macro that will read a system of linear equations in a custom format and call \begin{array}
with the appropriate format. For that I need to loop through each row and compute what is going to be displayed.
This is what I have so far.
\documentclass[10pt,a4paper]{article}
\usepackage{xstring}
\newcommand{\lineareqsys}[3]{
\noexpandarg
\StrCount{#3}{\\}[\rows] %check \rows = arg > 0
\newcounter{i} \setcounter{i}{0}
\def\str{#3}
\def\strout{}
\expandafter\StrSubstitute\expandafter{\str}{+}{+&}[\str]
\expandafter\StrSubstitute\expandafter{\str}{-}{-&}[\str]
\loop
\stepcounter{i}
\expandafter\def\csname strout\roman{i}\endcsname{}
\expandafter\StrCut\expandafter{\str}{\\}{\str}{\rest}
\expandafter\StrPosition\expandafter{\str}{x}[\xpos]
\expandafter\StrLeft\expandafter{\str}{\xpos}[\strappx]
\expandafter\def\expandafter\strout\expandafter{\strout \strappx &}
\expandafter\StrGobbleLeft\expandafter{\str}{\xpos}[\str]
\expandafter\StrPosition\expandafter{\str}{y}[\ypos]
\expandafter\StrLeft\expandafter{\str}{\ypos}[\strappy]
\expandafter\def\expandafter\strout\expandafter{\strout \strappy &}
\expandafter\StrGobbleLeft\expandafter{\str}{\ypos}[\str]
\expandafter\StrPosition\expandafter{\str}{=}[\eqpos]
\expandafter\StrLeft\expandafter{\str}{\eqpos}[\strappeq]
\expandafter\def\expandafter\strout\expandafter{\strout \strappeq &}
\expandafter\StrGobbleLeft\expandafter{\str}{\eqpos}[\str]
\expandafter\StrRight\expandafter{\str}{\eqpos}[\strapplb]
\expandafter\def\expandafter\strout\expandafter{\strout \strapplb \\}
\expandafter\def\expandafter\str\expandafter{\rest}
\expandafter\def\csname strout\roman{i}\endcsname{\strout}
\ifnum \thei < #1 \relax
\repeat
\newcounter{j} \setcounter{j}{0}
\def\alig{r}
\loop
\stepcounter{j}
\expandafter\def\expandafter\alig\expandafter{\alig cr}
\ifnum \thej < #2 \relax
\repeat
\left\lbrace
\begin{array}{\alig}
\strout
\end{array}
\right.
}
\begin{document}
$\lineareqsys{2}{2}{
x + y = 5 \\
x - y = 3 \\
}$
\end{document}
There is a problem with line
\expandafter\def\csname strout\roman{i}\endcsname{\strout}
because \strout
is not fully expanded, so after the second time of the loop data is replaced and I get the last row repeated twice.
I've been checking other threads but I haven't managed to get it right. How can I solve this problem?
Note1: The code isn't finished so it doesn't handle the cases where there is no x
or y
or a line starts with a -
sign.
Note2: The format I was referring to is straightforward.
ax + by + cz = d \\
for as many rows as necessary.
Edit: Simpler version of previous code.
\documentclass{article}
\newcommand{\lineareqsys}{
\newcounter{i} \setcounter{i}{0}
\def\strout{}
\loop
\stepcounter{i}
\expandafter\def\expandafter\strout\expandafter{\strout \arabic{i} row \\}
\ifnum \thei < 2 \relax
\repeat
\begin{array}{l}
\strout
\end{array}
}
\begin{document}
$\lineareqsys$
\end{document}
This ilustrates the same problem. \arabic{i}
is expanded at the end when it's value is 2. Not dinamic.
\strout
is safe for expansion then just replace\def
by\edef
or if you just want to expand it once thenexpandafter\def\csname strout\roman{i}\expandafter\endcsname\expandafter{\strout}
but the code seems massively over complicated, can you give a small text description of the intended effect?\edef
gives a compile time error. I think because it relays on#3
but I'm not sure if this is the reason.systeme
package?\newcounter
inside a command! (you do it twice)