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

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}

enter image description here


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}

enter image description here

Tags:

Matrices