I'm trying to align 2 groups of TikZ nodes but cannot figure out how to do this. I tried wrapping each group with \tikz{...}
but that didn't work (probably because of \tikz inside tikzpicture environment. Currently, I am using \matrix
but it aligns the nodes horizontally at the left (and I would like center) and doesn't use equal spacing between lines (or maybe, it does, but I would like it to change to a minimum spacing?)
Anyway, I have a feeling that my goal could be accomplished with \matrix
but I am open to other approaches to group nodes, too. For example, I haven't fully understood the {scope}
environment.
Here is my MWE illustrating the problem (I'd be also happy to hear if the MWE could be made more minimal, i.e., my tikz coding could be improved):
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
% some style definitions:
\newcommand\scriptsize{\fontsize{7pt}{7pt}\selectfont} % because of 'minimal' documentclass
% settings for lexemes
\tikzset{font=\scriptsize}
\tikzset{mynode/.style = {rectangle,minimum width=#1,draw=black!70}}
\tikzset{mynode/.default = 2mm}
% macro for drawing multiple lexemes next to each other:
\newcommand{\lexemes}[1]{%
\node (A) [mynode] {};
\xdef\lastx{A}
\foreach \x/\lbl/\d in {#1} {
\node (\x) [mynode,label=center:\lbl,right=\d of \lastx] {};
\xdef\lastx{\x}
}
}
\begin{document}
\begin{tikzpicture}[node distance=0.08 and 0.05]
\matrix [anchor=west] at (0,3) {% no labels, so rows very tight
\lexemes{C//} \\
\lexemes{C//,D//2mm,F//} \\
};
\matrix [anchor=west] at (0,2) {% label in upper row, so less tight
\lexemes{B/1/,C//} \\
\lexemes{C//,D//2mm,F//} \\
};
\matrix [anchor=west] at (0,1) {% labels in both rows, so this should be the constant row sep?
\lexemes{B/1/,C//} \\
\lexemes{B/3/,C//,D//2mm,F//} \\
};
\end{tikzpicture}
\end{document}
which produces the following result:
Now I would like to accomplish 2 things:
- Center the first and second row of each pair/matrix.
- Have an equal vertical separation between the first and second row of each matrix independent of the node label used. I would measure between the edges of the rectangles.
Best Answer
Okay, now I understand your problem. Here are the fixes:
To get the rows centered, place them in
\node
s with anchors at their center. A\matrix
aligns its columns by their "origin", which is rather tricky to change for complex constructions, but for a node is always where the anchor is.To get the spacing right, you have to make sure the
inner sep
of your labels is zero; eachlabel
is a new node, and as it turns out, its invisible extent is somewhat larger than the box you drew it in.The
\node
in point 1 also has aninner sep
, which I have to set to zero to avoid putting big spaces between every row. As it turns out, your style sets onlyminimum width
and relies oninner sep
to force the height, so to prevent the boxes from flattening out when they are contained in a scope withinner sep = 0
, I have changedminimum width=#1
tominimum size=#1
.