# How to display a distance matrix in LaTeX

I want to show a distance matrix as

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

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.

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}


• 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 Commented Mar 1 at 1: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. Commented Mar 1 at 2:07
• 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. Commented Mar 1 at 3:12

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}


Avoid $$ in LaTeX, see Why is $...$ preferable to$$ ... $$? • 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 Commented Feb 29 at 9:59 • @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. Commented Feb 29 at 10:19 • Sir, Just I'm asking, I'm always ready to accept the new technologies. Please excuse, if my post hurts you, shall I remove? Commented Feb 29 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 Commented Mar 1 at 1: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. Commented Mar 1 at 1:34

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}


• Elegant and efficient (for pure latex editors for compiling, eg. Texstudio), but not applicable for Markdown editors (eg. MarkText, Typora, ...). Tks anyway! Commented Mar 1 at 1:59
• @JohnStone Thank you very much. You are welcome. Commented Mar 1 at 8:42