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

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}

Related

How to scroll to a certain item in a Column(Modifier.verticalScroll())?

I have a Column with TextViews with indices from -10 to 10.
Column(
modifier = modifier
.verticalScroll(
state = scrollState,
enabled = isScrollEnabled,
reverseScrolling = reverseScrollDirection
)) {
for(i in -10..10) {
TextView("Index $i")
}
}
Naturally, this Column start with -10 and is incrementing by 1 until 10; so I have to scroll forward all the 21 indices (picture left hand side).
How can I align 0 to the beginning, so that I start the view scrollable either 10 back until -10 or 10 forward until 10 (see attached picture right hand side)?
Column doesn't have a concept of item, it has just a bunch of composable functions called inside of it. Because of that, ScrollState only has a method to scroll to position in pixels.
If you know the height of your items, you can calculate the pixel position and pass it to rememberScrollState(). Otherwise, you can use LazyColumn which can scroll to specific item.

How spacing is calculated in HStack

In SwiftUI when a horizontal array of Circles are made like this:
HStack(spacing : 4) {
Foreach(0..<5) { index in
Circle()
}
}
How the (horizontal) spacing value of 4 is applied? Is it between the centre of two circles or from their edges instead?
Building on top of #Asperi's comment: the spacing is applied in the same way it's applied to Text or Button standard views, which is between the frames of the views.
If you click on an element in the preview (this doesn't work when on live preview mode), you can see the frame of an element outlined in blue. The spacing is applied between the edges of the frames of each view.

Using directlua command inside tikzpicture (pgf-pie) causes image not to render

Using \directlua command inside tikzpicture (pgf-pie) causes image not to render:
\documentclass[margin=5mm,preview]{standalone}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture} % works
\pie[radius=2, rotate=100, sum=auto]
{
3/test , 2/two , 3/three , 4/four
}
\end{tikzpicture}
\begin{tikzpicture} % does not work
\pie[radius=2, rotate=100, sum=auto]
{
\directlua{tex.sprint("3/test , 2/two , 3/three , 4/four")}
}
\end{tikzpicture}
\end{document}
The result can be view here
How gets \directlua expanded? And what do I need to change to create tikzpicture pie charts with variables from lua?

How to make the tree-like graph in Latex?

I need to use LaTeX to create the following graph. I am just completely stuck with no idea how to get started. It seems there are some packages like TikZ that I could use to plot tree type of images, however I could not find anything to my example.
Notice that some of those are arrows, while some are just line segments. And most examples I found are vertical structure, but this one is horizontal.
Any tips would be appreciated.
To provide my best suggestion I used trees library from TikZ.
The code:
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{trees,arrows}
\begin{document}
\tikzstyle{level 1}=[level distance=30mm, sibling distance=30mm]
\tikzstyle{level 2}=[level distance=30mm, sibling distance=15mm]
\tikzstyle{level 3}=[level distance=20mm]
\begin{tikzpicture}[grow=right,->,>=angle 60]
%\begin{scope}[yshift=0]
\node {$A_{1,1}$}
child {node {$A_{2,2}$}
child {node {$A_{3,2}$}
child[-] {node{$S^{<4>}$}}
}
child {node{$A_{3,1}$}
child[-] {node{$S^{<3>}$}}
}
}
child {node {$A_{2,1}$}
child {node{$A_{3,2}$}
child[-] {node{$S^{<2>}$}}
}
child {node{$A_{3,1}$}
child[-] {node{$S^{<1>}$}}
}
};
%\end{scope}
\begin{scope}[yshift=-6cm]
\node {$A_{1,2}$}
child {node {$A_{2,2}$}
child {node {$A_{3,2}$}
child[-] {node{$S^{<8>}$}}
}
child {node {$A_{3,1}$}
child[-] {node{$S^{<7>}$}}
}
}
child {node {$A_{2,1}$}
child {node {$A_{3,2}$}
child[-] {node{$S^{<6>}$}}
}
child {node {$A_{3,1}$}
child[-] {node{$S^{<5>}$}}
}
};
\end{scope}
\end{tikzpicture}
\end{document}
And the output, very similar to yours.
Notice how no arrows at level 3 are provided by the syntax child[-] instead of child (default is ->,>=angle 60).

TikZ Mindmap: text in the nodes undesirably covered by connectors

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}

Resources