0

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?

TheBaum
  • 122
  • 1
  • 6
  • Why do you need to regex replace something at all? You could simply generate your string correctly in the first place? Or is there some reason that this is not an option? If so, please add the information / code to your question that explains where your `text` string comes from. Otherwise, see my answer below. – buddemat Dec 07 '20 at 19:27

1 Answers1

1

Well, your replacement does work (it correctly replaces \ with \\) , but there is a problem in your initial string definition: When you define text, it does not actually start with a backslash and the word begin but instead with the character \b which is a ASCII backspace (cmp: what does backward-slash b do in Python?), followed by egin.

You can see this if you print a non-interpreted representation of your string using print(repr(text)):

'\n\x08egin{alignat*}{2}\n\x08egin{sysmatrix}{rrr|r}\n 1 &  2 & 0 & 0 \\\n-1 &  1 & 2 & 0 \\\n 1 &  0 & 1 & 5 \\\n 0 & -2 & 1 & 4\n\\end{sysmatrix}'

If you properly define your string in the first place

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}'''

everything should be fine. A simple print(text) yields

\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}

Though that also makes the whole need for a regex replacement obsolete, so there may be missing constraints in your question.

buddemat
  • 4,552
  • 14
  • 29
  • 49
  • Thank you very much, your solution works quite well for my purpose... I was still wondering I could replace backslahes somehow. Found an anwerin a suggested Thread: https://stackoverflow.com/questions/17327202/python-replace-single-backslash-with-double-backslash – TheBaum Dec 08 '20 at 20:10