8

I want to show a distance matrix as

Enter image description here

in LaTeX, and I try:

\documentclass{article}

\usepackage{amsmath,amssymb}

\begin{document} We obtain a distance matrix as follows: $$ \boldsymbol{D}={d_{ij}}= \begin{array}{c} \begin{matrix} 1 & 2 & 3 & 4 & 5 \end{matrix} \ \begin{matrix} 1 \ 2 \ 3 \ 4 \ 5 \end{matrix} \begin{bmatrix} 0 & & & & \ 9 & 0 & & & \ 3 & 7 & 0 & & \ 6 & 5 & 9 & 0 & \ 11 & 10 & 2 & 8 & 0 \end{bmatrix} \end{array} $$ \end{document}

It shows

Enter image description here

which displays a mediocre result, and the alignment of the result (which I am not very satisfied with) can still be improved.

If one only use basic \usepackage{amsmath,amssymb} without using other (complex) packages, like \usepackage{kbordermatrix}, how can one improve the alignment in a concise and elegant way? P.S.: Simple native LaTeX commands can be easily applied to Markdown mathematical equations.

Also, try to avoid using manual adjustments like \qquad, \quad, \:, \;, etc., for spacing in LaTeX. This is because if the numbers inside the matrix change (e.g., from 2 to 200), it will further increase the alignment workload.

3 Answers3

13

You can use nicematrix.

\documentclass{article}

\usepackage{amsmath,amssymb,bm,nicematrix}

\begin{document}

We obtain a distance matrix as follows: [ \bm{D}={d_{ij}}= \begin{bNiceMatrix}[ first-row,first-col, code-for-first-row=\scriptstyle, code-for-first-col=\scriptstyle, ] & 1 & 2 & 3 & 4 & 5 \ 1 & 0 & & & & \ 2 & 9 & 0 & & & \ 3 & 3 & 7 & 0 & & \ 4 & 6 & 5 & 9 & 0 & \ 5 & 11 & 10 & 2 & 8 & 0 \end{bNiceMatrix} ]

\end{document}

enter image description here

Avoid $$ in LaTeX, see Why is \[ ... \] preferable to $$ ... $$?

egreg
  • 1,121,712
  • 1
    Sir @egreg; I believe the OP wants to implement the code into Markdown, is this suggestion supports Markdown? please advise. Excuse if my understanding is wrong – MadyYuvi Feb 29 '24 at 09:59
  • 1
    @MadyYuvi Of course not, but if you want to do complex tasks, you need the right tools. You don't dig a tunnel with a spade. – egreg Feb 29 '24 at 10:19
  • 1
    Sir, Just I'm asking, I'm always ready to accept the new technologies. Please excuse, if my post hurts you, shall I remove? – MadyYuvi Feb 29 '24 at 10:22
  • Both of your comments are very insightful and helpful. Yes, I would like to use LaTeX code in both TeXstudio and Markdown (editors). Thank you! @MadyYuvi@egreg – John Stone Mar 01 '24 at 01:21
  • Yes, I know \[...\] is preferable to $$ ... $$ in pure tex editors (eg. TeXStudio), but $$ ... $$ can provide real-time rendering of mathematical equations in markdown (editors), making it intuitive and efficient. ps: Personally, I don't like LyX either. It can be bloated and inefficient. – John Stone Mar 01 '24 at 01:34
8

You could use inner arrays with fixed column width to ensure on the same alignment. The bottom array can be enclosed around a scaled squared brackets. This is still prone to misalignment as well as column width would need to be adjusted every time you update the content. Check if this approach can pass the conversion.

The code

\documentclass{article}
\usepackage{array}
\usepackage{amsmath}
\usepackage{amssymb}

\setlength\arraycolsep{3pt} \newlength\collen \settowidth\collen{$00$} \newcolumntype{D}{>{\centering\arraybackslash}p{\collen}}

\begin{document} We obtain a distance matrix as follows: [ \boldsymbol{D} = {d_{ij}} = \begin{array}{c@{}c} & \begin{array}{5{D}} 1 & 2 & 3 & 4 & 5 \end{array} \[3pt] \begin{array}{r} 1 \ 2 \ 3 \ 4 \ 5 \end{array} & \left[ \begin{array}{5{D}} 0 & & & & \ 9 & 0 & & & \ 3 & 7 & 0 & & \ 6 & 5 & 9 & 0 & \ 11 & 10 & 2 & 8 & 0 \end{array} \right] \end{array} ] \end{document}

enter image description here

Celdor
  • 9,058
  • Your code cannot automatically adjust. For example, when you change 11 & 10 & 2 & 8 & 0 to 11 & 10 & 2000 & 8 & 0, we would need to reset \setlength.....blabla to adjust the spacing accordingly. Tks anyway@Celdor – John Stone Mar 01 '24 at 01:55
  • Good news is that your code works in both LaTeX editors (e.g., Texstudio) and Markdown editors (e.g., Typora, ...), i.e. they both support \begin{array}...\end{array}, which is a direction worth considering further. – John Stone Mar 01 '24 at 02:07
  • 1
    This approach is based on multiple arrays, which are kind of independent and have own alignments. In this case fixing column is to ensure top and bottom arrays line up. Downside is a manual adjustment. I considered this approach because markdown was mentioned in the question. Otherwise, nicematrix should be used for flexibility and simplicity without unnecessary tricks. – Celdor Mar 01 '24 at 03:12
3

Using a simple blkarray package more easy.

\documentclass[12pt]{article}
\usepackage{amsmath,amssymb,blkarray}
\begin{document}
\[
  \mathbf{D}=\{d_{ij}\}=
    \begin{blockarray}{ccccccc}
        & {\scriptstyle 1} & {\scriptstyle 2} & {\scriptstyle 3} & {\scriptstyle 4} & {\scriptstyle 5}\\
      \begin{block}{c[ccccc]c}
       {\scriptstyle1} & 0 & & & & & \\
      {\scriptstyle 2}& 9 & 0 & & & & \\
       {\scriptstyle 3}& 3 & 7 & 0 & & & \\
      {\scriptstyle 4}& 6 & 5 & 9 & 0 & & \\
      {\scriptstyle 5}&11 & 10 & 2 & 8 & 0 &\\
      \end{block}
      \end{blockarray}
\]
\end{document}

enter image description here

Sebastiano
  • 54,118