Es wäre viel einfacher das Rad nicht neu zu erfinden und das [`systeme`-Paket](https://ctan.org/pkg/systeme) zu verwenden.
\documentclass{article}
\usepackage{systeme}
\begin{document}
\systeme{
x_1 + x_2 + 2 x_3 - x_4 = 0,
x_2 - x_3 + 3 x_4 = 0
}
\end{document}
[![alt text][1]][1]
Ansonsten würde ich Lua für sowas benutzen.
\documentclass{article}
\usepackage{amsmath}
\usepackage{luacode}
\begin{luacode*}
lgs = lgs or {}
lgs.saved = lgs.saved or {}
local sprint = tex.sprint
local function define(name, array)
if lgs.saved[name] then
print("Redefining LGS `" .. name .. "'")
end
lgs.saved[name] = array
local len = #array[1]
for i, row in ipairs(array) do
assert(#row == len, "LGS `" .. name .. "' is not square!")
end
end
lgs.define = define
local function explicit(name)
local array = assert(lgs.saved[name], "LGS `" .. name .. "' undefined!")
local len = #array[1]
sprint("\\begin{array}{@{}*{" .. len .. "}{c@{}r@{}}}")
for i, row in ipairs(array) do
local isfirst = true
for j, col in ipairs(row) do
local sign = ""
local entry = "x_{" .. j .. "}"
if (col == -1) then
sign = "-"
elseif (col == 0) then
entry = ""
elseif (col == 1) then
sign = "+"
else
sign = col < 0 and "+" or "-"
entry = math.abs(col) .. entry
end
if (isfirst and sign == "+") then
sprint("& " .. entry .. " &")
elseif (j < len) then
sprint("{}" .. sign .. "{} & " .. entry .. " &")
else
sprint("{}=" .. col .. " \\\\")
end
if (col ~= 0) then
isfirst = false
end
end
end
sprint("\\end{array}")
end
lgs.explicit = explicit
local function matrix(name)
local array = assert(lgs.saved[name], "LGS `" .. name .. "' undefined!")
local len = #array[1]
sprint("\\begin{pmatrix}")
for i, row in ipairs(array) do
for j, col in ipairs(row) do
if (j < len) then
sprint(col .. " &")
else
sprint(col .. " \\\\")
end
end
end
sprint("\\end{pmatrix}")
end
lgs.matrix = matrix
\end{luacode*}
\newcommand\definelgs[2]{\directlua{lgs.define([[#1]],{#2})}}
\newcommand\lgsexplicit[1]{\directlua{lgs.explicit([[#1]])}}
\newcommand\lgsmatrix[1]{\directlua{lgs.matrix([[#1]])}}
\begin{document}
\definelgs{first}{
{ 1, 1, 2, -1, 0 },
{ 0, 1, 1, 3, 0 }
}
\[\lgsexplicit{first}\]
\[\lgsmatrix{first}\]
\definelgs{second}{
{ 1, -2, 1 },
{ 1, 3.14, 0 },
}
\[\lgsexplicit{second}\]
\[\lgsmatrix{second}\]
\end{document}
[1]: http://texwelt.de/wissen/upfiles/test_368.pnghttp://texwelt.de/wissen/upfiles/test_368.png