3

I would like to replicate a matrix like this:

angular matrix

A matrix with straight horizontal and vertical lines forming a rectangle around its content.

I tried using array environment with hlines

$$
\begin{array}{c|cccccccc|}
      & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
    \hline
    a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    b & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    c & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    d & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    e & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    \hline
\end{array}
$$

but got this intead:

wrong matrix

Is it possible to do it in latex or do i need an external program to generate such picture and insert it?

Youda008
  • 133

5 Answers5

4

You can of course do this with LaTeX. To generate a horizontal line that spans a subset of the columns, use \cline{x-y}, where makes a rule from column x to column y.

To remove the vertical bars from the top row you need to override the column specification for two of the cells with \multicolumn. The syntax is \multicolumn{<number of columns>}{<column specifier>}{<cell content>}. The first and last column have c| as column specifier, using \multicolumn{1}{c}{} in the first cell changes that to c without the vertical bar.

Finally, don't use $$ .. $$ for display math in LaTeX, it is recommended to use \[ \] instead.

\documentclass[a4paper]{article}
\usepackage[czech,english]{babel}
\begin{document}
\[
\shorthandoff{-}
\begin{array}{c|cccccccc|}
  \multicolumn{1}{c}{}    & 1 & 2 & 3 & 4 & 5 & 6 & 7 &   \multicolumn{1}{c}{8} \\
    \cline{2-9}
    a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    b & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    c & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    d & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    e & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
    \cline{2-9}
\end{array}
\]
\end{document}

enter image description here

Torbjørn T.
  • 206,688
3

Here's a code. Don't use $$ … $$ for displaymah, but \[ … \]. I loaded the array package to have some more functionalities (less tight vertical spacing, and a simpler code for repetition of cells with the same specifier):

\documentclass{article}
\usepackage{mathtools, array}

\begin{document}

\[ \setlength\extrarowheight{2pt}\begin{array}{c|*{8}{c}|}
  \multicolumn{1}{c}{} & \mathbf{1} & \mathbf{2} & \mathbf{3} & \mathbf{4} & \mathbf{5} & \mathbf{6} & \mathbf{7 }& \multicolumn{1}{c}{\mathbf{8}} \\
  \cline{2-9}
  \mathbf{a} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
  \mathbf{b} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
  \mathbf{c} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
  \mathbf{d} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
  \mathbf{e} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
  \cline{2-9}
  \end{array} \]%

\end{document} 

enter image description here

Bernard
  • 271,350
3

With {NiceMatrix} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

[\begin{NiceMatrix} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \ a & \Block[draw]{5-8}{} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ b & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ c & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ d & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ e & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \ \end{NiceMatrix}]

\end{document}

Output of the above code

F. Pantigny
  • 40,250
2

What we can do in OpTeX:

\table{($)c($)|8c|}{
  \mspan1[c]{}  & 1 & 2 & 3 & 4 & 5 & 6 & 7 & \mspan1[c]{8} \crlp{2-9}
              a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \cr
              b & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \cr
              c & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \cr
              d & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \cr
              e & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \crlp{2-9}
}

\bye

wipet
  • 74,238
1

A solution with \tikzmark:

\documentclass[a4paper]{article}
\usepackage[czech]{babel}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}
\[
\begin{tikzpicture}[remember picture,overlay]
\draw ([yshift=1.2\ht\strutbox,xshift=-3pt]{pic cs:start}) 
  rectangle ([xshift=3pt,yshift=-\dp\strutbox]{pic cs:end});
\end{tikzpicture}
\begin{matrix}
& \mathbf{1} & \mathbf{2} & \mathbf{3} & \mathbf{4}
& \mathbf{5} & \mathbf{6} & \mathbf{7} & \mathbf{8}\\
\mathbf{a} & 
  \tikzmark{start}
  0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\mathbf{b} &
  0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\mathbf{c} &
  0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\mathbf{d} &
  0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\mathbf{e} &
  0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\tikzmark{end}
\end{matrix}
\]
\end{document}

Note that this requires two passes when the matrix changes, in order to be properly typeset.

enter image description here

egreg
  • 1,121,712