0

Using Tikz, placing three rectangles right above each other with dashed outline, how can I let tikz not draw the lines inbetween multiple times?

Also, the baseline of the text inside those rectangle is not always at the same height inside the box - it depends whether the text uses the cap line (like a 'b'), the descender line (like a 'q') or non (like an 'a').

MWE:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\node (A) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, dashed] {a}; \node (B) at (A.south) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, yshift=+0.4pt, dashed] {b}; \node (Q) at (B.south) [draw,rectangle,minimum width=37mm, minimum height=9mm, anchor=north, yshift=+0.4pt, dashed] {q};

\end{tikzpicture} \end{document}

Manuel
  • 35

1 Answers1

1

Instead of drawing every node, you can draw the border and separation lines after placing them:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{tikzpicture}[box/.style={minimum width=37mm, minimum height=9mm},
    line/.style={dashed}]

\node (A) [box] {a}; \node (B) [box, below=0pt of A] {b}; \node (Q) [box, below=0pt of B] {q}; \node[fit=(A) (Q), draw, line, inner sep=0pt] (ABQ) {}; \draw[line] (A.south-|Q.west)--(A.south-|Q.east) (B.south-|Q.west)--(B.south-|Q.east); \end{tikzpicture} \end{document}

enter image description here

Instead of a fit node you can also use: \draw[line] (A.north west) rectangle (Q.south east);

Ignasi
  • 136,588