3

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

17
  • 1
  • 5
    I’m voting to close this question because it is about KaTeX.
    – Marijn
    Commented Jan 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? Commented Oct 23, 2022 at 14:42
  • @FerencBeleznay Stack Overflow has KaTeX questions, see stackoverflow.com/questions/tagged/katex?tab=Newest for some examples.
    – Marijn
    Commented Oct 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. Commented Oct 23, 2022 at 16:25

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 Commented 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! Commented 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 .