答案1
这是使用 tikz 的可能性。我创建了您需要的命令,并为城市创建了另一个命令(我认为为城市分配数字更容易)。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{siunitx}
\sisetup {output-decimal-marker={,}}
\def\l{1} % cell size
\def\d{0.1} % cell distance
\newcommand{\scalentry}[3] % city1 (number, row), city2 (number, column), distance
{%
\pgfmathparse{int(mod(#1,2))}
\ifnum\pgfmathresult=0
\draw ({#2*(\l+\d)},{#1*(\l+\d)}) rectangle ++ (\l,\l);
\else
\draw[fill=gray!20] ({#2*(\l+\d)},{#1*(\l+\d)}) rectangle ++ (\l,\l);
\fi
\node at ({#2*(\l+\d)+0.5*\l},{#1*(\l+\d)+0.5*\l}) {\num{#3}};
}
\newcommand{\city}[2] % name, position
{%
\node[above right] at ({#2*(\l+\d)},{(#2+1)*(\l+\d)-0.25*\l}) {#1};
}
\begin{document}
\begin{tikzpicture}[y={(0,-1)}]
\city{Argenta} {1}
\city{Boccaleone}{2}
\city{San Biagio}{3}
\city{Bando} {4}
% ...
\scalentry{2}{1} {4.2}
\scalentry{3}{1} {4.8}
\scalentry{3}{2} {9}
\scalentry{4}{1} {6}
\scalentry{4}{2} {7.8}
\scalentry{4}{3}{10.8}
% ...
\end{tikzpicture}
\end{document}
答案2
有一个仅基于 TeX 基元的解决方案:
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\def\scalentry#1#2#3{\sdef{c:#1:#2}{#3}\sdef{c:#2:#1}{#3}}
\def\cs#1{\csname#1\endcsname}
\def\headerlist#1{\par\def\headerlistL{}\headerlistA#1,,}
\def\headerlistA#1,{\ifx,#1,\else \headerlistB{#1}\expandafter\headerlistA\fi}
\def\headerlistB#1{\def\headerlistT{#1}%
\expandafter\headerlistC\headerlistL,%
#1\vskip1pt
\edef\headerlistL{\headerlistL#1,}}
\def\headerlistC#1,{\ifx,#1,\else
\printitem{\cs{c:\headerlistT:#1}}\expandafter\headerlistC \fi}
\def\printitem#1{\leavevmode \lower3pt
\vbox{\hrule\hbox to3em{\vrule\hss#1\strut\hss\vrule}\hrule}\kern2pt}
%% Test
\scalentry{Argenta}{Boccaleone}{4,2}
\scalentry{Argenta}{San Biagio}{4,8}
\scalentry{Boccaleone}{San Biagio}{9}
\scalentry{Argenta}{Bando}{6}
\scalentry{Boccaleone}{Bando}{7,8}
\scalentry{San Biagio}{Bando}{10,8}
Prints the table:
\headerlist{Argenta,Boccaleone,San Biagio,Bando}
Prints another table:
\headerlist{Boccaleone,San Biagio,Bando,Argenta}