I want to create a diagram in tikz using sth like the following code:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning,fit}
\begin{document}
%\input{content/renalBrainshake/gfx/flowchart.tikz}
\begin{tikzpicture}[every node/.style={draw, fill=blue, font=\sffamily\tiny}, align=center, node distance = 2.1cm]
%%%%% COHORT NODES
%% DIABETICS
\node (n1) [] {node1\\ line 2};
\node (n2) [right of = n1] {node2\\ line 2};
\node (n3) [right of = n2] {node3};
\node (long)[fit = (n1)(n3),
above = 1cm of n1.west,
anchor= south west, inner sep=0] {long};
\end{tikzpicture}
\end{document}
There are 3 problems:
- I want the three nodes
n1
,n2
,n3
all to have the same height, without having to guess the optimal height usingminimum height
- I want the long node to fit the width of the three other nodes, but the height of the
long
node should be automatic according to the text in it - The text in
long
should be vertically centered, but here it is not
Does anybody have a good solution to any of this?
Best Answer
Use
fit
also forn3
andlabel=center:text
for text in nodesn3
andlong
.Update:
In case you want to adjust the height of
long
node, a partial solution could be:new
fit
options andinner xsep=0
define its longitudinal dimension and minimum height={height("long")} (fromcalc
tikzlibrary) defines the vertical.Another solution for nodes with taller contents could be to use an auxiliary node which fixes height and a
fit
node which covers it. The aux node can be created with:It's a regular node which is placed where we want it (
above = 1cm of n1.west, anchor=south west
) and with adjusted size to contain its text.Now we can use its anchors to define the new node as long as previous n1, n2 and n3 and adjusted height to its contents.
As you can see
fit = {(n1.west|-aux.south)(n3.east|-aux.north)}
andinner sep=-.5\pgflinewidth
define news node size.And
opacity=.5
has been include to also seeaux
node. Comment out this line or delete it before producing the final result.All fitting nodes in following code include
inner sep=-.5\pgflinewidth
. This way,fit
nodes will have the exact size of referenced ones. Withinner sep=0pt
, fitting nodes include reference anchors inside its borders, which means that the resultant nodes are half line width larger than fitted ones.The complete code is: