我正在尝试绘制一个 TikZ 图,我想从零开始计数,而不是从一开始。我已经调整了这个回答满足我的需要。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[auto matrix/.style={matrix of nodes,
draw,thick,inner sep=0pt,
nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
cells={nodes={minimum width=1.9em,minimum height=1.9em,
draw,very thin,anchor=center,fill=white,
execute at begin node={%
$\vphantom{x_|}\ifnum\the\pgfmatrixcurrentrow<4
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{\the\pgfmatrixcurrentrow}_{\the\pgfmatrixcurrentcolumn}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
{#1}^{\the\pgfmatrixcurrentrow}_{N}
\fi
\fi
\else
\ifnum\the\pgfmatrixcurrentrow=5
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{T}_{\the\pgfmatrixcurrentcolumn}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
{#1}^{T}_{N}
\fi
\fi
\fi
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=14
\cdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=41
\vdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=44
\ddots
\fi$
}
}}}]
\matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=x](matx){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
node[midway,below] {$D$};
\draw[thick,-stealth] ([yshift=-1ex]matx.south west) --
([yshift=-1ex]matx.south east) node[midway,below] {joints};
\draw[thick,-stealth] ([xshift=-1ex]matx.north west)
-- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
\end{tikzpicture}
\end{document}
此代码有以下输出。
我希望索引从零开始。像这样。
我尝试使用 calc 包从中减去类似的\the\pgfmatrixcurrentrow
东西\the\pgfmatrixcurrentcolumn
$\the\pgfmatrixcurrentrow - 1 = \result$
但无济于事。任何帮助都将不胜感激。
答案1
您可以\pgfmathtruncatemacro
在将所有代码放入矩阵定义之前使用来计算所需的数字,并用计算变量替换所有\the\pgfmatrixcurrentrow
和。\the\pgfmatrixcurrentcolumn
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[auto matrix/.style={matrix of nodes,
draw,thick,inner sep=0pt,
nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
cells={nodes={minimum width=1.9em,minimum height=1.9em,
draw,very thin,anchor=center,fill=white,
execute at begin node={%
\pgfmathtruncatemacro{\nrow}{\the\pgfmatrixcurrentrow-1}%
\pgfmathtruncatemacro{\ncol}{\the\pgfmatrixcurrentcolumn-1}%
$\vphantom{x_|}\ifnum\the\pgfmatrixcurrentrow<4
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{\nrow}_{\ncol}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
{#1}^{\nrow}_{N}
\fi
\fi
\else
\ifnum\the\pgfmatrixcurrentrow=5
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{T}_{\ncol}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
{#1}^{T}_{N}
\fi
\fi
\fi
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=14
\cdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=41
\vdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=44
\ddots
\fi$
}
}}}]
\matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=x](matx){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
node[midway,below] {$D$};
\draw[thick,-stealth] ([yshift=-1ex]matx.south west) --
([yshift=-1ex]matx.south east) node[midway,below] {joints};
\draw[thick,-stealth] ([xshift=-1ex]matx.north west)
-- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
\end{tikzpicture}
\end{document}
答案2
由于这些是整数,因此使用\the\numexpr\pgfmatrixcurrentrow-1
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[auto matrix/.style={matrix of nodes,
draw,thick,inner sep=0pt,
nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
cells={nodes={minimum width=1.9em,minimum height=1.9em,
draw,very thin,anchor=center,fill=white,
execute at begin node={%
$\vphantom{x_|}\ifnum\the\pgfmatrixcurrentrow<4
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{\the\numexpr\pgfmatrixcurrentrow-1}_{\the\numexpr\pgfmatrixcurrentcolumn-1}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
\,{#1}^{\the\numexpr\pgfmatrixcurrentrow-1}_{N}
\fi
\fi
\else
\ifnum\the\pgfmatrixcurrentrow=5
\ifnum\the\pgfmatrixcurrentcolumn<4
{#1}^{T}_{\the\numexpr\pgfmatrixcurrentcolumn-1}
\else
\ifnum\the\pgfmatrixcurrentcolumn=5
\,{#1}^{T}_{N}
\fi
\fi
\fi
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=14
\cdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=41
\vdots
\fi
\ifnum\the\pgfmatrixcurrentrow\the\pgfmatrixcurrentcolumn=44
\ddots
\fi$
}
}}}]
\matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\matrix[auto matrix=x](matx){
& & & & \\
& & & & \\
& & & & \\
& & & & \\
& & & & \\
};
\draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
node[midway,below] {$D$};
\draw[thick,-stealth] ([yshift=-1ex]matx.south west) --
([yshift=-1ex]matx.south east) node[midway,below] {joints};
\draw[thick,-stealth] ([xshift=-1ex]matx.north west)
-- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
\end{tikzpicture}
\end{document}
我还在\,
最后一列中添加了内容以避免过多重叠。