答案1
您可以尝试使用\pic
,但由于您没有说明如何使用它,所以我不知道这种方法是否适合您的需要:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\newcounter{tiltedmatrixcolumncount}
\tikzset{
tilted matrix/reset columns/.code={
\setcounter{tiltedmatrixcolumncount}{0}
},
tilted matrix/column head/.code={
\stepcounter{tiltedmatrixcolumncount}
\tikzset{tilted matrix/column head \arabic{tiltedmatrixcolumncount}/.initial={#1}}
},
tilted matrix/header nodes/.style={
anchor=base,
},
pics/tilted matrix/.style={
/tikz/tilted matrix/reset columns,
/tikz/tilted matrix/column head/.list={#1},
code={
\draw (0.5,0) -- ++(0,-0.5) coordinate (-coord 0-0);
\foreach \x [remember=\x as \lastx (initially 0)]
in {1,...,\arabic{tiltedmatrixcolumncount}} {
\node[tilted matrix/header nodes] at (\x,0.25)
{\pgfkeysvalueof{/tikz/tilted matrix/column head \x}};
\draw ({\x + 0.5},0) -- ++(0,-0.5) coordinate (-coord \x-0)
-- ++({\x * -0.5},{\x * -0.5});
\ifnum\value{tiltedmatrixcolumncount}<2\else
\foreach \i [count=\y] in {\x,...,\arabic{tiltedmatrixcolumncount}} {
\coordinate (-node \y-\x)
at ({\x + 0.5 * \y - 0.5},{-0.5 * \y});
}
\foreach \y [remember=\y as \lasty (initially 0)]
in {1,...,\x} {
\coordinate (-coord \x-\y)
at ({\x - 0.5 * \y + 0.5},{-0.5 * \y - 0.5});
\draw (-coord \x-\y) -- (-coord \lastx-\lasty);
}
\fi
}
}
}
}
\begin{document}
\begin{tikzpicture}
\pic (my matrix) at (0,0) {tilted matrix={$a$, $b$, $c$, $d$}};
\node at (my matrix-node 1-1) {1};
\node at (my matrix-node 3-2) {2};
\pic (my other matrix) at (0,-4) {tilted matrix={$b$, $b$, $a$, $c$, $b$, $c$}};
\end{tikzpicture}
\end{document}
单元格的值使用锚定到 提供的相关坐标的节点来放置\pic
。坐标编号的逻辑如下所示: