15

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
Name
  • 2,816

2 Answers2

23

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

17

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

AboAmmar
  • 46,352
  • 4
  • 58
  • 127