# A quick way to convert a block of numbers or characters into a matrix (without using ampersand &)

I have a block of numbers like this (actually it is a Hadamard matrix of order 12). What is the quickest way to convert this block into a matrix? Of course one can do it with a matrix environment like bmatrix, pmatrix etc by adding ampersand & between the numbers, but I am looking for a solution by just coping the block.

1 -1 -1 -1  -1 -1 -1 -1  -1 -1 -1 -1
1  1 -1  1  -1 -1 -1  1   1  1 -1  1
1  1  1 -1   1 -1 -1 -1   1  1  1 -1
1 -1  1  1  -1  1 -1 -1  -1  1  1  1
1  1 -1  1   1 -1  1 -1  -1 -1  1  1
1  1  1 -1   1  1 -1  1  -1 -1 -1  1
1  1  1  1  -1  1  1 -1   1 -1 -1 -1
1 -1  1  1   1 -1  1  1  -1  1 -1 -1
1 -1 -1  1   1  1 -1  1   1 -1  1 -1
1 -1 -1 -1   1  1  1 -1   1  1 -1  1
1  1 -1 -1  -1  1  1  1  -1  1  1 -1
1 -1  1 -1  -1 -1  1  1   1 -1  1  1


The spalign package is very useful for this: It allows to create a matrix by separating the columns by spaces and lines by semicolons, e.g.

\documentclass{article}
\usepackage{spalign}
\begin{document}
\spalignmat{ 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1; 1 1 -1 1 -1 -1 -1 1 1 1 -1 1; 1 1 1 -1 1 -1 -1 -1 1 1 1 -1; 1 -1 1 1 -1 1 -1 -1 -1 1 1 1; 1 1 -1 1 1 -1 1 -1 -1 -1 1 1; 1 1 1 -1 1 1 -1 1 -1 -1 -1 1; 1 1 1 1 -1 1 1 -1 1 -1 -1 -1; 1 -1 1 1 1 -1 1 1 -1 1 -1 -1; 1 -1 -1 1 1 1 -1 1 1 -1 1 -1; 1 -1 -1 -1 1 1 1 -1 1 1 -1 1; 1 1 -1 -1 -1 1 1 1 -1 1 1 -1; 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 }
\end{document}


If you also want to avoid the semicolons, you can ask TeX to insert a semicolon at every end of line:

\documentclass{article}
\usepackage{spalign}
\begin{document}
\begingroup\endlinechar=\;\spalignmat{ 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 -1 1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 }\endgroup%
\end{document}


• To obtain a bracketed matrix one should add \spaligndelims[] right after . – Name Aug 7, 2018 at 9:18 I'll add to @MarcelKrüger's good answer the right alignment option [r] for nice output. \documentclass{article} \usepackage{spalign} \begin{document} \[ \begingroup\endlinechar=\;\spalignmat[r]{ 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 1 1 -1 1 -1 -1 -1 1 1 1 -1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 1 1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 -1 1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 1 1 -1 1 1 }\endgroup%
\end{document}