我想在 tikz 中显示以下图片,为此我编写了代码。但是缩放此图像会扭曲矩形节点的位置。在我显示的图片中,节点之间有共同的边缘。无论缩放与否,我都不希望它们扭曲。有人能给我一些解决这个问题的提示吗?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (rxst0) at (0.75,-0.5) [draw,fill=gray!20,minimum height=0.65cm,minimum width=0.5cm] {$st_0$};
\node (rxst1) at (2.25,-0.5) [draw,fill=gray!20,minimum height=0.65cm,minimum width=0.5cm] {$st_1$};
\node (c0pos0) at (0,0) [draw,minimum height=0.65cm,minimum width=1.0cm] {$pos_0$};
\node (c0st0) at (0.75,0) [draw,fill=gray,minimum height=0.65cm,minimum width=0.5cm] {$st_0$};
\node (c0pos1) at (1.55,0) [draw,minimum height=0.65cm,minimum width=1.0cm] {$pos_1$};
\node (c0st1) at (2.25,0) [draw,fill=gray,minimum height=0.65cm,minimum width=0.5cm] {$st_1$};
\end{tikzpicture}
\end{document}
扭曲的图像如下所示 -
我也尝试了类似下面的操作,但是没有效果。
\node (c0st0) [anchor=west,right of=c0pos0,node distance=0cm,draw,fill=gray,minimum height=0.65cm,minimum width=0.5cm] {$st_0$};
谢谢
答案1
不确定您理解“缩放”是什么意思。下图和代码不受缩放影响,因为位置是相对于其他节点指定的,宽度和高度以绝对单位指定:
\usetikzlibrary{positioning}
\tikzset{
mybox/.style = {
minimum width=#1,
minimum height=0.65cm,
inner sep=3pt,
draw},
mybox/.default=0.5cm,
}
\begin{tikzpicture}[node distance=-\pgflinewidth]
\node [mybox=1cm, fill=white] (c0pos0) {$pos_0$};
\node [mybox,fill=gray,right=of c0pos0] (rxst0) {$st_0$};
\node [mybox,fill=gray!20,below=of rxst0] {$st_0$};
\node [mybox=1cm,fill=white,right=of rxst0] (c0pos1) {$pos_1$};
\node [mybox,fill=gray,right=of c0pos1] (rxst1) {$st_1$};
\node [mybox,fill=gray!20,below=of rxst1] {$st_1$};
\end{tikzpicture}
\begin{tikzpicture}[node distance=-\pgflinewidth, scale=2]
\node [mybox=1cm, fill=white] (c0pos0) {$pos_0$};
\node [mybox,fill=gray,right=of c0pos0] (rxst0) {$st_0$};
\node [mybox,fill=gray!20,below=of rxst0] {$st_0$};
\node [mybox=1cm,fill=white,right=of rxst0] (c0pos1) {$pos_1$};
\node [mybox,fill=gray,right=of c0pos1] (rxst1) {$st_1$};
\node [mybox,fill=gray!20,below=of rxst1] {$st_1$};
\end{tikzpicture}
更新
根据@Ignasi 的建议(我同意),这是mybox
风格的另一种定义:
\tikzset{
mybox/.style = {
text width=#1,
text centered,
text height=4mm,
text depth=2.5mm,
inner sep=3pt,
draw},
mybox/.default=0.5cm,
}
使用tikzpicture
与上面相同的代码,可得到以下结果:
答案2
PSTricks 解决方案:
\documentclass{article}
\usepackage{amsmath} % for \text
\usepackage{multido} % for \multido
\usepackage{pstricks} % for the rest of the drawing
\begin{document}
\def\blocks{2} % the number of different indices
\begin{pspicture}(\numexpr 5*\blocks,4)
\psset{dimen = middle, fillstyle = solid} % `dimen = middle` gives you perfectly overlapping rectangles as wanted
\multido{\iA = 0+5, \iB = 0+1}{\blocks}{%
\psframe(\iA,2)(!\iA\space 3 add 4)
\rput(!\iA\space 1.5 add 3){\Huge $\text{pos}_{\iB}$}
\psframe[fillcolor = black!60](!\iA\space 3 add 2)(!\iA\space 5 add 4)
\rput(!\iA\space 4 add 3){\Huge $\text{st}_{\iB}$}
\psframe[fillcolor = black!20](!\iA\space 3 add 0)(!\iA\space 5 add 2)
\rput(!\iA\space 4 add 1){\Huge $\text{st}_{\iB}$}}
\end{pspicture}
\end{document}