I need to write this matrix, but how can I make it in KaTeX and HTML?
-
1See also Boxed entries in matrices, Draw a box around matrix elements, Drawing boxes in matrices, Boxes in matrices using tikz– gernotJan 29, 2022 at 21:06
-
5I’m voting to close this question because it is about KaTeX.– MarijnJan 30, 2022 at 13:21
-
@Marijn I would also have a question about KaTeX and I would like to ask which stackexchange group would be an appropriate to use instead of this?– Ferenc BeleznayOct 23, 2022 at 14:42
-
@FerencBeleznay Stack Overflow has KaTeX questions, see stackoverflow.com/questions/tagged/katex?tab=Newest for some examples.– MarijnOct 23, 2022 at 15:13
-
@Marijn Thanks for your reply, I will try there. However, please note that for example a comment for stackoverflow.com/questions/74007016/… mentions that it is off topic, because it is not about programming. This question you voted to close is asking whether a specific subset of LaTeX can handle a certain task. It is not about programming. It is well within the scope of this topic (formats, related software, ...). Just because TeX was originally developed for print, these days it is perfectly OK to ask questions about online implementations.– Ferenc BeleznayOct 23, 2022 at 16:25
3 Answers
With a normal array
along with \multicolumn
s 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}
-
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
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}
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}