# Convert a string with $N^2$ entries to a rank-$N$ matrix

Suppose we are given a set of numeral data like a string that consists of $$N^2$$ entries, say $$N$$ is very large (here $$N=8$$ for simplicity, but $$N$$ can be $$10^8$$...)

2 −1 0 0 0 0 0 0

−1 2 −1 0 0 0 0 0

0 −1 2 −1 0 0 0 0

0 0 −1 2 −1 0 0 0

0 0 0 −1 2 −1 0 −1

0 0 0 0 −1 2 −1 0

0 0 0 0 0 − 1 2 0

0 0 0 0 −1 0 0 2


How do we use Mathematica to input this string and output its matrix form as a rank-$$N$$, namely $$N$$ by $$N$$, matrix in an efficient way? Say in both the MathML input font and the LaTex font?

I expect the output will be in,

LaTex font:

$$\begin{pmatrix} 2 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\ -1 & 2 & -1& 0 & 0 & 0 & 0 & 0 \\ 0 & -1 & 2 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & -1 & 2 & -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 & 2 & -1 & 0 & -1 \\ 0 & 0 & 0 & 0 & -1 & 2 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 2 & 0 \\ 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2 \end{pmatrix}$$


MathML input font:

$$\begin{pmatrix} 2 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\ -1 & 2 & -1& 0 & 0 & 0 & 0 & 0 \\ 0 & -1 & 2 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & -1 & 2 & -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 & 2 & -1 & 0 & -1 \\ 0 & 0 & 0 & 0 & -1 & 2 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 2 & 0 \\ 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2 \end{pmatrix},$$

or the one written in Table with two layers of brackets, the first layer brackets has 8 brackets inside, the second layer brackets has 8 number inside.

{{...},{...},{...},{...},{...},{...},{...},{...}}

Question -- Can we obtain this final Table form with two layers of brackets?

You keep referring to "font", so maybe your question is about displaying the data, but if it's just about getting the string data into a numeric list format, then you can just use ImportString.

I copy-pasted your string just as you provided it, so I also included a cleaning step:

data = "2 -1 0 0 0 0 0 0

\[Minus]1 2 \[Minus]1 0 0 0 0 0

0 \[Minus]1 2 \[Minus]1 0 0 0 0

0 0 \[Minus]1 2 \[Minus]1 0 0 0

0 0 0 \[Minus]1 2 \[Minus]1 0 \[Minus]1

0 0 0 0 \[Minus]1 2 \[Minus]1 0

0 0 0 0 0 \[Minus] 1 2 0

0 0 0 0 \[Minus]1 0 0 2";
ImportString[StringReplace[data, {"\n\n" -> "\n", "\[Minus] " -> "-", "\[Minus]" -> "-"}], "Table"]

(*
{{2, -1, 0, 0, 0, 0, 0, 0},
{-1, 2, -1, 0, 0, 0, 0, 0},
{0, -1, 2, -1, 0, 0, 0, 0},
{0, 0, -1, 2, -1, 0, 0, 0},
{0, 0, 0, -1, 2, -1, 0, -1},
{0, 0, 0, 0, -1, 2, -1, 0},
{0, 0, 0, 0, 0, -1, 2, 0},
{0, 0, 0, 0, -1, 0, 0, 2}}
*)

• Could you explain this: ImportString[StringReplace[data, {"\n\n" -> "\n", "[Minus] " -> "-", "[Minus]" -> "-"}], "Table"] for me in detail? How does each function work?
– zeta
Nov 21, 2023 at 0:28
• ImportString[..., "Table"] is saying to import it as a table, a list of data, and it'll automatically try to interpret the data as mathematica expressions (so numbers in this case). The StringReplace[...] is there to clean up the data: "\n\n" -> "\n" gets rid of the extra blank lines, "\[Minus] " -> "-" handles that one weird case with the extra space on the second-to-last line of data, and "\[Minus]" -> "-" handles all the rest of the "[Minus]" cases ("[Minus]" would be interpreted as a string character, not the numeric negative sign). Nov 21, 2023 at 0:40
• Thanks this works pretty well.
– zeta
Dec 11, 2023 at 17:22

Why not use the new latex interface to interpret your text? Basically, you enter Ctrl-4, then enter the tex into the cell, and then hit enter. Here is a short video where I do this:

• I may have misinterpreted the question, but I thought @zeta was asking how to get latex commands as output, given string input. So I don't think they have the latex commands to input into the latex interface. I could have just misinterpreted the question though.
– ydd
Nov 20, 2023 at 16:34
• Thanks, but the starting point is this string: 2 −1 0 0 0 0 0 0 −1 2 −1 0 0 0 0 0 0 −1 2 −1 0 0 0 0 0 0 −1 2 −1 0 0 0 0 0 0 −1 2 −1 0 −1 0 0 0 0 −1 2 −1 0 0 0 0 0 0 −1 2 0 0 0 0 0 −1 0 0 2
– zeta
Nov 20, 2023 at 17:35
• @CarlWoll I haven’t been able to find documentation of this useful feature. Does the rendered LaTeX become a usable object or is it formatted text…obviously I’ll test when I get a chance. Thanks for highlighting this feature. +1 of course Nov 21, 2023 at 7:38
• This is still very nice, please keep it - I voted up.
– zeta
Dec 12, 2023 at 22:47

The way I did this is pretty messy, so maybe someone can improve on this:

First I replace "\[Minus] " with "\[Minus]" in the 7th row because theres an extra space there. I then replace "\[Minus]" with -. There is a distinction between \[Minus and - : it appears Interpreter only works with - here

str = "2 −1 0 0 0 0 0 0

−1 2 −1 0 0 0 0 0

0 −1 2 −1 0 0 0 0

0 0 −1 2 −1 0 0 0

0 0 0 −1 2 −1 0 −1

0 0 0 0 −1 2 −1 0

0 0 0 0 0 − 1 2 0

0 0 0 0 −1 0 0 2";

str = StringReplace[str, "\[Minus] " -> "\[Minus]"];
str = StringReplace[str, "\[Minus]" -> "-"];


I then use DelimitedSequence to interpret str as a sequence of integers:

interp =
Interpreter[DelimitedSequence["Integer"]][str];
n = Sqrt@Length@interp;
mat = ArrayReshape[interp, {n, n}];
mat//MatrixForm


For LaTeX input, I Riffle an ampersand and a LaTeX new line \ at the end of each row. Then convert back ToString and StringJoin all together:

addDings = Riffle[#, " & "] & /@ mat;
out = Append[#, " \\\\ \n"] & /@ addDings;
latex = StringJoin[ToString /@ Flatten@out];
(*drop last \\ and add \begin and \end*)
latex = StringDrop[latex, -4];
StringJoin["\\begin{pmatrix}\n", latex, " \n\\end{pmatrix}"]



As a sanity check I test the output by pasting it here (I copied the output from the above code block as Plain Text and put it here enclosed with a pair of ):

$$\begin{pmatrix} 2 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\ -1 & 2 & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & -1 & 2 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & -1 & 2 & -1 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 & 2 & -1 & 0 & -1 \\ 0 & 0 & 0 & 0 & -1 & 2 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 2 & 0 \\ 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2 \end{pmatrix}$$