14

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
21

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

  • To obtain a bracketed matrix one should add \spaligndelims[] right after \[. – Name Aug 7 '18 at 9:18
16

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.