TikZ Mindmap: text in the nodes undesirably covered by connectors - latex

I created a mindmap (see below). I found my text was undesirably covered by the connectors. Is there a way to put the connector a layer below the text, or put the text a layer above the connector? I know I can bypass this problem by changing the node size, or the font size, or the sibling angle. But I prefer the current appearance so I don't want to change any of the parameters above. I just want to change the layers. Is there a way to do so?
\documentclass[12pt,twoside]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[mindmap, grow cyclic, every node/.style=concept, concept color=green!80,
level 1/.append style={level distance=5cm, sibling angle=180},
level 2/.append style={level distance=3cm, sibling angle=90}]
\node{Gram positive bacteria}
child[concept color=orange!80]{ node{Firmicutes}
child{node{\textit{Listeria}}
}
child{node{\textit{Bacillus}}
}
child{node{\textit{Streptococcus}}
}
child{node{\textit{Enterococcus}}
}
}
child[concept color=purple!30,]{ node{Actinobacteria}
child{node{\textit{Streptomyces}}
}
child{node{\textit{Corynebacterium}}
}
child{node{\textit{Mycobacterium}}
}
}
;
\end{tikzpicture}
\end{document}

One cheap hack to bypass the problem described in your title is to manually hyphen text locally, where necessary. In your case, you could change lines 27 (for the sake of consistency), 30 and 32 of your code respectively to:
child[concept color=purple!30]{node{Actino\-bacteria}
...
child{node{\textit{Coryne\-bacterium}}
...
child{node{\textit{Myco\-bacterium}}

I used an extra node (mycobacterium) to write the text after the mindmap has been rendered.
\begin{tikzpicture}[mindmap, grow cyclic, every node/.style=concept, concept color=green!80,
level 1/.append style={level distance=5cm, sibling angle=180},
level 2/.append style={level distance=3cm, sibling angle=90}]
\node{Gram positive bacteria}
child[concept color=orange!80]{ node{Firmicutes}
child{node{\textit{Listeria}}
}
child{node{\textit{Bacillus}}
}
child{node{\textit{Streptococcus}}
}
child{node{\textit{Enterococcus}}
}
}
child[concept color=purple!30,]{ node{Actinobacteria}
child{node{\textit{Streptomyces}}
}
child{node{\textit{Corynebacterium}}
}
child{node (mycobacterium) {}
}
};
\node[extra concept,fill=none,draw=none] at (mycobacterium) {\textit{Mycobacterium}};
\end{tikzpicture}

Related

How can I make more space between node and edge in Latex Tree Diagram?

I want "Child" at the left to fit in better like the rest and I want more space at the right side between the last nodes and the calculations P(x) = 1/2
this is the code:
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
%document already started
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=3cm, sibling distance=1cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=1em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right]
\node[bag] {Child}
child {
node[bag] {G}
child {
node[end, label=right:
{G $P(G\cap G)=\frac{1}{2}$}] {}
edge from parent
node[below] {0.5}
}
child {
node[end, label=right:
{B $P(G\cap B)=\frac{1}{2}$}] {}
edge from parent
node[above] {$0.5$}
}
edge from parent
node[below] {$0.5$}
}
child {
node[bag] {B}
child {
node[end, label=right:
{G $P(B\cap G)=\frac{1}{2}$}] {}
edge from parent
node[below] {$0.5$}
}
child {
node[end, label=right:
{B $P(B\cap B)=\frac{1}{2}$}] {}
edge from parent
node[above] {$0.5$}
}
edge from parent
node[above] {$0.5$}
};
\end{tikzpicture}
%end of document
I don't know hat I can do. I have tried to change the width which is 1em right now. But if I increase it the diagram gets even more out of shape and if I decrease it it completely covers "Child"
I have tried to make more spaces behind the last event (before the P(blabla) thing). But that didn't chnage anything.
repetitive spaces in latex are treated like a single space. If you'd like to manually increase the space, you could e.g. use \qquad
to avoid your Child node from colliding with the lines, you can increase its text width. This will move it a bit further away from the lines
don't use \tikzstyle, this macro is obsolete.
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
% Set the overall layout of the tree
\tikzset{level 1/.style={level distance=3cm, sibling distance=2cm}}
\tikzset{level 2/.style={level distance=3cm, sibling distance=1cm}}
% Define styles for bags and leafs
\tikzset{bag/.style={text width=1em, text centered}}
\tikzset{end/.style={circle, minimum width=3pt,fill, inner sep=0pt}}
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right]
\node[bag,minimum width=2cm] {Child}
child {
node[bag] {G}
child {
node[end, label=right:
{G\qquad$P(G\cap G)=\frac{1}{2}$}] {}
edge from parent
node[below] {0.5}
}
child {
node[end, label=right:
{B\qquad $P(G\cap B)=\frac{1}{2}$}] {}
edge from parent
node[above] {$0.5$}
}
edge from parent
node[below] {$0.5$}
}
child {
node[bag] {B}
child {
node[end, label=right:
{G\qquad $P(B\cap G)=\frac{1}{2}$}] {}
edge from parent
node[below] {$0.5$}
}
child {
node[end, label=right:
{B\qquad $P(B\cap B)=\frac{1}{2}$}] {}
edge from parent
node[above] {$0.5$}
}
edge from parent
node[above] {$0.5$}
};
\end{tikzpicture}
\end{document}

How can I make SnapKit constrain the left and right edges?

Right now if I want to set the margin equally on the left and right side, it requires two lines:
label.snp.makeConstraints { make in
make.left.equalTo(Constants.margin)
make.right.equalTo(-Constants.margin)
}
The API also allows me to set all 4 (left/right/top/bottom) edges at once using:
label.snp.makeConstraints { make in
make.edges.equalTo(Constants.margin
}
How can I write an extension that will allow something like this?
label.snp.makeConstraints { make in
make.horizontalMargins.equalTo(Constants.margin)
}
The result should equate to the first code snippet.
label.snp.makeConstraints { make in
make.left.right.equalToSuperview().inset(Constants.margin)
}

Centering node vertically in horizontal ASStackLayout

I'm wonder what is the correct way to center textNode inside vertical ASSStackNode which is inside horizontal ASStackNode without setting any sizes?
I've tried different options with ASCenterLayout / ASRelativeLayout around vertical layout, flewGrow, flexShrink.
Only possible way that I see is static layout with minimum height around vertical layout. But wanna find if there are another way.
Images to better picture:
My layout structure:
What I get when one of text fields is empty:
What I want to see:
If your ASTextNode is empty or nil. Isn't mean node not exist. If you want hide node if it empty, you must write a simple logic for it. For exlude it Spec from calculation. What i use on ADK1.9:
- (ASLayoutSpec *)layoutSpecThatFits: (ASSizeRange)constrainedSize
{
NSMutableArray *arrayOfChildren = [NSMutableArray new];
[arrayOfChildren insertObject:self.titleNode];
if (self.textNode.text.length > 0) {
[arrayOfChildren insertObject:self.textNode];
}
ASStackLayoutSpec *verticalElementCoreStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:8
justifyContent:ASStackLayoutJustifyContentCenter alignItems:ASStackLayoutAlignItemsStretch
children:arrayOfChildren];
return verticalElementCoreStack;
}

Not getting expected ambiguity in view with two labels side by side

I'm laying out two UILabel side by side, where I want the right label to take just the space it needs. Like this:
------------------------------
|[left label ][right label]|
------------------------------
I'm using hugging and compression to accomplish that, and it works.
My surprise is that, when I remove hugging and compression configuration (i.e. leaving the default ones), hasAmbiguousLayout doesn't return true.
As expected, I get random results, like the right label taking more space than needed, or the left label taking all the space and leaving none for the right label.
Why is hasAmbiguousLayout not returning true?
I'm calling it from viewDidLayoutSubviews:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if view.hasAmbiguousLayout() {
print("Layout is ambiguous!")
view.exerciseAmbiguityInLayout()
} else {
print("Layout is not ambiguous") // I'm always getting this
}
}
Thank you

What to do if label is bigger than parent UIVIew?

I have two multiline UILabels. The parent UIView only has a certain size (height) but the labels can contain a long string. What I want is that the first label is always shown (e.g. abbreviated with ... if it gets too long). If there is room the second label should be shown. Again if it's too long it should get abbreviated with ...
This is the constraint I'm currently using: V:|-5-[title]-0-[description]-(>=0)-|
In some cases the second label gets cut off like this:
What should I do? Do I have to change the constraint? Can I hide the second label somehow? But how should I detect when the label is cut off?
In another case the second label wasn't shown at all. The second label should also not overflow the parent UIView. How should one handle such cases?
I believe rounding occurs somewhere in iOS because the label would fit (one line and then at the end with ...) if there would be 0.75pt more space. So it draws it but it is cut off because of the missing 0.75pt space.
Now I took the following approach: Calculate if the second label (reference text which should be about one line) would fit in the parent view? And if not hide it. I only have code in C# but you should get the idea:
public override void LayoutSubviews ()
{
base.LayoutSubviews ();
NSString referenceTextToMeasure = new NSString ("Lorem ipsum dolor sit amet.");
// take the font of the description label which was cut off
CGRect referenceTextRect = referenceTextToMeasure.GetBoundingRect (
new CGSize(this.Frame.Width, nfloat.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin,
new UIStringAttributes () { Font = descriptionLabelFont },
new NSStringDrawingContext ()
);
// 5 is the spacing constraint I have on the top
if ((this.Frame.Height - TitleLabel.Frame.Height - 5) < referenceTextRect.Height )
DescriptionLabel.Hidden = true;
}
Seems to work quite well, though a full test has to be made. You could also use a fixed height instead of referenceTextRect.Height and you would save some calculations. Nevertheless, the way I used allows of adapting the fonts so everything is dynamically calculated.

Resources