I want to replace "\" with "\\" for further editing and export to LaTeX. (It's for a matrix-calculator which should export all steps of the calculation to LaTeX)
import re
text='''
\begin{alignat*}{2}
\begin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\
-1 & 1 & 2 & 0 \\
1 & 0 & 1 & 5 \\
0 & -2 & 1 & 4
\end{sysmatrix}'''
I've tryed replace()
and re.sub()
. The Answers I found suggested those two, but I gues they don't work in this case.
print(re.sub('\\\\',' \\\\\\\\', text))
print(text.replace('\\',' \\\\'))
print(text.replace('\\',r' \\\\'))
Output
egin{alignat*}{2}
egin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\
-1 & 1 & 2 & 0 \\
1 & 0 & 1 & 5 \\
0 & -2 & 1 & 4
\\end{sysmatrix}
egin{alignat*}{2}
egin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\
-1 & 1 & 2 & 0 \\
1 & 0 & 1 & 5 \\
0 & -2 & 1 & 4
\\end{sysmatrix}
egin{alignat*}{2}
egin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\\\
-1 & 1 & 2 & 0 \\\\
1 & 0 & 1 & 5 \\\\
0 & -2 & 1 & 4
\\\\end{sysmatrix}
It should like like this:
\begin{alignat*}{2}
\begin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\
-1 & 1 & 2 & 0 \\
1 & 0 & 1 & 5 \\
0 & -2 & 1 & 4
\end{sysmatrix}'
or this:
\\begin{alignat*}{2}
\\begin{sysmatrix}{rrr|r}
1 & 2 & 0 & 0 \\\\
-1 & 1 & 2 & 0 \\\\
1 & 0 & 1 & 5 \\\\
0 & -2 & 1 & 4
\\end{sysmatrix}'
Any other ideas how I could write a LaTeX document with python?