I need to write this matrix, but how can I make it in KaTeX and HTML?
3 Answers
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).
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}
- 40,250
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}
- 46,352
- 4
- 58
- 127
-
Sorry but do you mind converting it to html or katex? I thought I was using latex but I was wrong! Sorry – Santiago Sierra Jan 29 '22 at 22:11
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}
- 22,451




\documentclass{article}to\end{document}is code. It is written. Therefore it is code writing. – Marijn Oct 25 '22 at 09:52