3

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

  • 1
  • 5
    I’m voting to close this question because it is about KaTeX. – Marijn Jan 30 '22 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 Beleznay Oct 23 '22 at 14:42
  • @FerencBeleznay Stack Overflow has KaTeX questions, see https://stackoverflow.com/questions/tagged/katex?tab=Newest for some examples. – Marijn Oct 23 '22 at 15:13
  • @Marijn Thanks for your reply, I will try there. However, please note that for example a comment for https://stackoverflow.com/questions/74007016/newtonian-notation-for-derivates-on-latex 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 Beleznay Oct 23 '22 at 16:25
  • @FerencBeleznay the question you linked is about mathematical notation, i.e., which symbol to choose for a derivative and which LaTeX/KaTeX command can be used for that symbol. This is indeed off-topic on SO but the first part (which symbol to choose) would also be off-topic here even if LaTeX is used and not KaTeX, because it is in essence a mathematics question which can be asked on Math.SE. If you have a question that involves writing code however (like this question here about drawing a matrix) then I am fairly sure it is on-topic on Stack Overflow. – Marijn Oct 24 '22 at 07:27
  • Note that KaTeX and MathJax are functionally more or less a subset of LaTeX, so if you want to ask "how can I do X in KaTeX" then you can ask "how do I do X in LaTeX" instead, which is on-topic here, and hope that KaTeX will support the LaTeX answers that you get. – Marijn Oct 24 '22 at 07:33
  • @Marijn This question here is not about writing code. It is about how to achieve a goal in a subset of LaTeX. This is what the accepted answer is doing. At the moment, KaTeX does not support multicolumn. However, this is mentioned on their homepage, so probably it is in their plan. At the moment, a similar diagram can be created using nested matrices. – Ferenc Beleznay Oct 25 '22 at 09:17
  • @FerencBeleznay but the way to achieve the goal is to write some code, right? So the question is about how to write that code. – Marijn Oct 25 '22 at 09:19
  • @Marijn Do you consider the accepted answer as code writing? I do not. I consider it a clever way of using arrays. A similar approach (with arrays inside arrays) give a similar output (just the spacing is not right and the boxes do not meet at the corners). – Ferenc Beleznay Oct 25 '22 at 09:23
  • @FerencBeleznay yes, I consider it code writing. Everything from \documentclass{article} to \end{document} is code. It is written. Therefore it is code writing. – Marijn Oct 25 '22 at 09:52
  • @marijn We clearly see thing in different ways. According to this logic all the discussions linked in the first comment above (and quite a few in this group) are off topic. – Ferenc Beleznay Oct 26 '22 at 11:50
  • @FerencBeleznay I think there is a misunderstanding. Code writing is on-topic here on TeX.SE, and on Stack Overflow. The question here about matrices is about code writing, which would make it on-topic here, except that it is about KaTeX which is off-topic here. It is still on-topic on SO though, because they take KaTeX questions about code writing. What is not on-topic either on SO or here are questions about mathematical notation. A question can be about both (which notation do I need and how do I write code to get that notation?) in which case [...] – Marijn Oct 26 '22 at 11:56
  • one part of the question is on-topic and the other part of the question is off-topic. – Marijn Oct 26 '22 at 12:03
  • Anyway, for your situation, if you have a code-writing question about KaTeX that you can rewrite as a question about LaTeX, then you can post it here. If you have a code-writing question that is specific to KaTeX and cannot be changed into a LaTeX question then you can post on SO. If you have a notation question then you can ask on Math.SE. I hope it is a bit more clear now? – Marijn Oct 26 '22 at 12:03
  • @marijn It is kind of pointless arguing. I keep repeating my point of you, you keep repeating your point of view. Nobody asked "what notation shall I use" (neither here, nor in the discussion I linked). People are asking "how to achieve a certain, very specific goal if the full power of TeX and its extensions are not available". I do not see how this does not fit in this topic. Either using KaTeX as an engine (to support a limited subset for online publication) or a related software (surely KaTeX is related to TeX). These are both mentioned as examples in the help material. – Ferenc Beleznay Oct 26 '22 at 17:13
  • I realize though that arguments like this are not really OK on these sites. We cannot convince each other. I am happy if a moderator checks the arguments and removes all these comments we wrote to each other. – Ferenc Beleznay Oct 26 '22 at 17:17

3 Answers3

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}

F. Pantigny
  • 40,250
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

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
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