7

I would like to following picture in tikz, for which I have written the code. However scaling this image, distorts the location of the rectangle nodes. In my picture shown, there are common edges between the nodes. Irrespective of scaling, I don't want them to be distorted. Could someone give me tips to resolve this problem?

enter image description here

\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}

Here is how the distorted image looks like -

enter image description here

I also tried something like following, but it doesn't work.

\node (c0st0) [anchor=west,right of=c0pos0,node distance=0cm,draw,fill=gray,minimum height=0.65cm,minimum width=0.5cm] {$st_0$};

Thank You

1
  • Use positioning tikzlibrary
    – JLDiaz
    Jun 24, 2014 at 13:00

2 Answers 2

12

Not sure of understanding what do you mean with "scale". The following figure and code are inmune to scaling, because positions are specified relative to other nodes, and widths and heights are specified in absolute units:

\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}

Result

Update

Following suggestions by @Ignasi (which I agree with), this is an alternative definition of mybox style:

\tikzset{
  mybox/.style = {
     text width=#1, 
     text centered,
     text height=4mm, 
     text depth=2.5mm,
     inner sep=3pt, 
     draw},
  mybox/.default=0.5cm,
}

Which, using the same tikzpicture code than above, produces:

Result2

2
  • I think what I was doing wrong was not including node distance=-\pgflinewidth. This is something I learned new. Thanks for the answer! Its perfectly what I was looking for. Jun 24, 2014 at 13:23
  • I think some text height and text depth will help to better align nodes text.
    – Ignasi
    Jun 24, 2014 at 13:25
5

A PSTricks solution:

\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}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .