答案1
使用kbordermatrix 包. (我认为由于许可原因它不能包含在 TeX 发行版中。)
\documentclass{article}
\usepackage{rotating}
\usepackage{kbordermatrix}
\begin{document}
\[ C = \raisebox{-2\baselineskip}{%
\begin{tabular}[b]{c@{\hspace*{0.5em}}c}
& To node
\\
\begin{sideways}
From node
\end{sideways}
& \raisebox{0.5\height}%
{\(\kbordermatrix
{ & 1 & 2 & 3 & 4 & 5 \\
1 & 1 & x_1 & x_5 & 0 & 0 \\
2 & x_1 & 1 & x_2 & x_3 & x_4 \\
3 & x_5 & x_2 & 1 & x_6 & 0 \\
4 & 0 & x_3 & x_6 & 1 & x_7 \\
5 & 0 & x_4 & 0 & x_7 & 1
}
\)%
}
\end{tabular}}
\]
\end{document}
答案2
TikZ 也可以做类似的事情。矩阵的基线校正是通过以下方式实现的埃格雷格公式。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\[
C =
\begin{tikzpicture}[baseline={([yshift=-\dimexpr\fontdimen22\textfont2\relax]M.center)}]
\matrix [
matrix of math nodes,
left delimiter={[}, right delimiter={]},
inner sep=1pt, column sep=1ex, row sep=1ex,
execute at begin cell=\mathstrut,
] (M) {
1 & x_1 & x_5 & 0 & 0 \\
x_1 & 1 & x_2 & x_3 & x_4 \\
x_5 & x_2 & 1 & x_6 & 0 \\
0 & x_3 & x_6 & 1 & x_7 \\
0 & x_4 & 0 & x_7 & 1 \\
};
\foreach \c in {1,...,5}
\node[above=1em] at (M-1-\c) {$\scriptstyle\c$};
\foreach \r in {1,...,5}
\node[left=1.5em] at (M-\r-1) {$\scriptstyle\r$};
\node[above=2em] at (M-1-3) {To node};
\node[rotate=90,above=2.5em] at (M-3-1) {From node};
\end{tikzpicture}
\]
\end{document}