I want to create the following tikzpicture:
What I have at the moment is this:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}
\begin{document}
\noindent\begin{tikzpicture}[background rectangle/.style={fill=gray!25}, show background rectangle,every node/.style={inner sep=0pt,outer sep=0pt,draw}]
\begin{scope}[local bounding box=scope1]
\path[use as bounding box] (0,0) rectangle (.5\textwidth,0);
\filldraw (0,0) circle (2pt);
\node [text width=0.5\textwidth, align=left,anchor=north west](n1) {scope 1, node 1};
\node [below = 0pt of n1.south west,anchor=north west] (n2) {scope 1, node 2};
\node [below left = 0pt and 0pt of n2.north east,anchor=north west,align=left,text width=5.1cm] (n3) {scope 1, node 3 that includes longer text to be broken into lines};
\end{scope}
\begin{scope}[shift={(scope1.north east)},anchor=north west]
\node [text width=0.5\textwidth, align=left](n1) {scope 2, node 1};
\node [below = 0pt of n1.south west,anchor=north west] (n2) {scope 2, node 2};
\node [below left = 0pt and 0pt of n2.north east,anchor=north west,align=left,text width=5.1cm] (n3) {scope 2, node 3 which also includes longer text to be broken into lines};
\end{scope}
\end{tikzpicture}
\end{document}
I need help with two things:
-
How can I define the width of node
n3
so the total width ofn2
andn3
is that ofn1
(which is.5\textwidth
)? At the moment I set it to 5.1cm because it looks ok but if I change the content of node 2 then node 3's dimension should change as well. How can I make use of the bounding box I created for the first scope? -
How can I insert a separator between the two scopes (such as the one drawn in yellow in the first pic)? Why the width of the two scopes extends beyond
textwidth
? There is a gray margin on the left and right althoughouter sep=0pt
.
Best Answer
The background rectangle has a padding (whch is called
inner frame [xy]sep
), thus it shows more in gray than your actual content. Usetight background
to change this.Furthermore, the line width (of any path, not only nodes, irregardles of any outer seps) will contribute to the picture's final bounding box which also throws off the final placement of the picture a bit (and will lead to an overful hbox).
In your original setup, you can overwrite the bounding box's horizontal measurement by using
trim left=0pt, trim right=\textwidth
. In my code below, I'm using±.5\textwidth
because I create the picture centered about the origin.For creating nodes that have sizes that are relating to other nodes, they are the
fit
library which works by setting text width/height/depth which is bad if you want the nodes to actually have text.My
ext.positioning-plus
library does similar things using thefit
library but controls the size of the new node by usingminimum width
andminimum height
which is better for having text in the node but needs a bit work for text that is longer than what would fit in the node.I'm using here a very basic
text width between
key that uses thecalc
library to measure the distance between two arbitrary points in the x dimension and uses that to set thetext width
of the node. We need to subtractinner xsep
twice here (so that the final node actually covers the area with inner xsep). This is basically a way to set a fixed width instead of a minimum width. (The factor 2 is different for other shapes.)For syncing up sizes of nodes, I can offer the
ext.node-families
library of mytikz-ext
package.Every node that has
node family={height=row1}
set shares the sameminimum height
. This will align the texts in each node vertically centered.Both blue nodes as well as the yellow separater (also a node) will span the same vertical space.
Code (Node Family)
Code (w/o Node Family)
Output