6

In TikZ I have a styled node, in this case a matrix with an addition separation space and a dashed border. Left and right of this matrix I want to align additional nodes with the north dashed border.

Currently I place the node left of a certain node inside the matrix and use a yshift to align the box manually. This works, however I am curious if there is a way to align with the style information.

enter image description here

My MWE:

\begin{tikzpicture}    
% Tikz Styles
\tikzset{
  item/.style={
    draw, 
    rectangle, 
    font=\sffamily\scriptsize, align=center,
    inner sep=3pt
  },
  board/.style={
    draw, 
    dashed, 
    inner sep=3mm, 
    matrix of nodes
  },
  inBoard/.style={
    item, 
    solid, 
    sharp corners, 
    minimum height=0.8cm, 
    font=\sffamily\scriptsize\bfseries,
    fill=hgrau, 
    thick
  },
  outerBox/.style={
    item,
    solid,
    font=\sffamily\scriptsize\bfseries
  }
}

% Elements
\matrix(Board)[board] {
\node[label](dashedBoxLabel)[]{Dashed Styled Box};
\node[inBoard] (InBox)
  [draw=none,
  below of=dashedBoxLabel, 
  node distance=1.8cm, 
  minimum height=12mm, 
  minimum width=4cm]{In box};\\
};
\node[outerBox] (OtherBox) 
  [left of=dashedBoxLabel,
  yshift=-1.6cm,
  node distance=4cm,
  draw=none,
  fill=hgrau,
  minimum width = 3cm, 
  minimum height=6cm]{Other Box}; 
\end{tikzpicture}
Moriambar
  • 11,466

1 Answers1

5

For the matrix of nodes you don't need to supply node syntax again. The options can be passed via |[...]| way.

You can also use \\[<length>] to separate rows instead of hard coding the locations of the nodes that are already inside-the-cell.

You can use anchoring and positioning library syntax. Please have a look at Difference between "right of=" and "right=of" in PGF/TikZ

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[
% Tikz Styles
  item/.style={
    draw, 
    rectangle, 
    font=\sffamily\scriptsize, align=center,
    inner sep=3pt
  },
  board/.style={
    draw, 
    dashed, 
    inner sep=3mm, 
    matrix of nodes
  },
  inBoard/.style={
    item, 
    solid, 
    sharp corners, 
    minimum height=0.8cm, 
    font=\sffamily\scriptsize\bfseries,
    fill=gray, 
    thick
  },
  outerBox/.style={
    item,
    solid,
    font=\sffamily\scriptsize\bfseries
  }
]

% Elements
\matrix[board] (Board) {
    Dashed Styled Box\\[1cm]
    |[font=\sffamily\scriptsize\bfseries,
      fill=gray, 
      minimum height=12mm, 
      minimum width=4cm
    ]| In box\\
};
\node[outerBox] (OtherBox) 
  [left= 3mm of Board.north west,
  anchor=north east,
  draw=none,
  fill=gray,
  minimum width = 3cm, 
  minimum height=6cm]{Other Box}; 
\end{tikzpicture}
\end{document}
percusse
  • 157,807