3

I need to write this matrix, but how can I make it in KaTeX and HTML?

17

3 Answers 3

5

With a normal array along with \multicolumns this can be quite easy.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\[
J(2)=\left(\begin{array}{lllll}
\cline{1-1}
\multicolumn{1}{|l|}{2} & 0 & 0                      & 0 & 0                      \\ \cline{1-3}
\multicolumn{1}{l|}{0}  & 2 & \multicolumn{1}{l|}{0} & 0 & 0                      \\
\multicolumn{1}{l|}{0}  & 1 & \multicolumn{1}{l|}{2} & 0 & 0                      \\ \cline{2-5} 
0                       & 0 & \multicolumn{1}{l|}{0} & 2 & \multicolumn{1}{l|}{0} \\
0                       & 0 & \multicolumn{1}{l|}{0} & 1 & \multicolumn{1}{l|}{2} \\ \cline{4-5} 
\end{array}\right)
\]

\end{document}

enter image description here

1
  • Sorry but do you mind converting it to html or katex? I thought I was using latex but I was wrong! Sorry Jan 29, 2022 at 22:11
5

With {NiceArray} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions{exterior-arraycolsep}

$J(2) = 
\left(\,\begin{NiceArray}{ccccc}
\Block[draw]{}{}2 & 0 & 0 & 0 & 0 \\
0 & \Block[draw]{2-2}{}2 & 0 & 0 & 0 \\
0 & 1 & 2 & 0 & 0 \\
0 & 0 & 0 & \Block[draw]{2-2}{}2 & 0 \\
0 & 0 & 0 & 1 & 2 
\end{NiceArray}\,\right)$\

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

If you prefer, you can also draw the rules with an Tikz instruction after the content of the matrix. The output is the same.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\NiceMatrixOptions{exterior-arraycolsep}

$J(2) = 
\left(\,\begin{NiceArray}{ccccc}
2 & 0 & 0 & 0 & 0 \\
0 & 2 & 0 & 0 & 0 \\
0 & 1 & 2 & 0 & 0 \\
0 & 0 & 0 & 2 & 0 \\
0 & 0 & 0 & 1 & 2 
\CodeAfter 
  \tikz \draw (1-|1) -| (4-|2) -| (last-|last) -| (2-|4) -| cycle ; 
\end{NiceArray}\,\right)$\

\end{document}
1
  • +1: I am surprised how little code that required! Jan 29, 2022 at 21:17
2

A starting point (without the lines within the matrix).

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation}
J(2) = 
\begin{pmatrix}
2 & 0 & 0 & 0 & 0\\
0 & 2 & 0 & 0 & 0\\
0 & 1 & 2 & 0 & 0\\
0 & 0 & 0 & 2 & 0\\
0 & 0 & 0 & 2 & 1
\end{pmatrix}\,.
\end{equation}
% the "\," adds a small space before the dot.

\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .