Graphics, Figures & TablesProblems with tikzpicture

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
paulosousa
Posts: 90
Joined: Sun Jun 24, 2012 8:48 pm

Problems with tikzpicture

Post by paulosousa »

Hello all,

I need this organogram explicit, some boxes are over others. What I must do?
I think I need to increase the horizontal distance between nodes, but which way?

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\usepackage{amsmath}
\usepackage{xspace}

\begin{document}

\begin{tikzpicture}[
    grow=right,
    level 1/.style={sibling distance=3.5cm,level distance=3.5cm},
    level 2/.style={sibling distance=3.5cm,level distance=4.5cm},
    edge from parent/.style={very thick,draw=blue!40!black!60,
        shorten >=5pt, shorten <=5pt},
    edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
    kant/.style={text width=2cm, text centered, sloped},
    every node/.style={text ragged, inner sep=2mm},
    punkt/.style={rectangle, rounded corners, shade, top color=white,
    bottom color=blue!50!black!20, draw=blue!40!black!60, very
    thick }
    ]


\node[punkt, text width=3.75em] {Critical Illness Patients}
    %Lower part lv1
    child {
        node[punkt, text width=3em] {$\sim$ TBI} 
            %child 1
        child {
            node [punkt, text width=6.5em] {\textbf{Neurological Demage}}
            %child 1
            child {node [punkt, text width=6.5em] {\textbf{Brain Death}}}
            %child 2
            child {node [punkt, text width=6.5em]{\textbf{Recovery}}}                 
                 }
        %child 2
        child {
            node [punkt, text width=6.5em]{\textbf{$\sim$ Neurological Demage}}
            %child 1
            child {node [punkt, text width=6.5em]{\textbf{Recovery}}}      
                }
                }
    %Upper part, lv1
    child {
        node[punkt, text width=3em] {TBI}
        %child 1
        child {
            node [punkt, text width=6.5em] {\textbf{ND}}
            %child 1
            child {node [punkt, text width=6.5em] {\textbf{Brain Death}}} 
            %child 2
            child {node [punkt, text width=6.5em]{\textbf{Cardiac arrest without Brain Death}}}
            %child 3
            child {node [punkt, text width=6.5em]{\textbf{Recovery}}}
                 }
        %child 2
        child {
            node [punkt, text width=6.5em]{\textbf{$\sim$ Neurological Demage}}      
            edge from parent
                node[kant, above] {}}
            edge from parent{
                node[kant, above] {}}             
             };
    
\end{tikzpicture}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problems with tikzpicture

Post by Johannes_B »

From a first glance you need to adjust the sibling distance for the different levels.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\usepackage{amsmath}
\usepackage{xspace}

\begin{document}

\begin{tikzpicture}[
		grow=right,
		node distance=1cm,
		level 1/.style={sibling distance=5.5cm,level distance=3.5cm},
		level 2/.style={sibling distance=2.5cm,level distance=4.5cm},
		level 3/.style={sibling distance=1.5cm,level distance=4.5cm},
		edge from parent/.style={very thick,draw=blue!40!black!60,
		shorten >=5pt, shorten <=5pt},
		edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
		kant/.style={text width=2cm, text centered, sloped},
		every node/.style={text ragged, inner sep=2mm},
		punkt/.style={rectangle, rounded corners, shade, top color=white,
			bottom color=blue!50!black!20, draw=blue!40!black!60, very
		thick }
	]


	\node[punkt, text width=3.75em] {Critical Illness Patients}
	%Lower part lv1
	child {
		node[punkt, text width=3em] {$\sim$ TBI}
		%child 1
		child {
			node [punkt, text width=6.5em] {\textbf{Neurological Demage}}
		%child 1
			child {node [punkt, text width=6.5em] {\textbf{Brain Death}}}
		%child 2
			child {node [punkt, text width=6.5em]{\textbf{Recovery}}}                
		}
	    %child 2
		child {
			node [punkt, text width=6.5em]{\textbf{$\sim$ Neurological Demage}}
		%child 1
			child {node [punkt, text width=6.5em]{\textbf{Recovery}}}      
		}
	}
	%Upper part, lv1
	child {
		node[punkt, text width=3em] {TBI}
	    %child 1
		child {
			node [punkt, text width=6.5em] {\textbf{ND}}
		%child 1
			child {node [punkt, text width=6.5em] {\textbf{Brain Death}}}
		%child 2
			child {node [punkt, text width=6.5em]{\textbf{Cardiac arrest without Brain Death}}}
		%child 3
			child {node [punkt, text width=6.5em]{\textbf{Recovery}}}
		}
	    %child 2
		child {
			node [punkt, text width=6.5em]{\textbf{$\sim$ Neurological Demage}}      
			edge from parent
			node[kant, above] {}}
			edge from parent{
				node[kant, above] {}}            
			};

		\end{tikzpicture}
		\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
paulosousa
Posts: 90
Joined: Sun Jun 24, 2012 8:48 pm

Problems with tikzpicture

Post by paulosousa »

yes I missed the adjust for the level 3, thanks a lot

Meanwhile I need now to put it in a beamer presentation.
So I put almost the same code(*) in a frame but it is too big, could you please help me to decrease it a little bit? What I must do?


(*)
only this is different

Code: Select all

level 1/.style={sibling distance=5.5cm,level distance=2.5cm},
                level 2/.style={sibling distance=2.5cm,level distance=2.75cm},
                level 3/.style={sibling distance=1.5cm,level distance=3.75cm},
Screen Shot 2014-08-10 at 10.50.31.png
Screen Shot 2014-08-10 at 10.50.31.png (113.44 KiB) Viewed 8813 times
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problems with tikzpicture

Post by Johannes_B »

I just noticed you used the minimal documentclass
above. Don't use it for minimal working examples, it is not meant
for it.

I changed your original tree a bit, made it more readable. I also
changed the colors. I think it fits better with the colortheme
you use.

To make the content fit into the frame, you can either scale it,
as seen in the second frame, or extenalize the image and include
it again using \includegraphics.

Please click on »Open in writelatex« just above my code before trying to change something in your document.

Code: Select all

\begin{filecontents}{\jobname-treecode.pgf}
	%The code for the tree is in here
	\node {Critical Illness Patients}
	child {
		node  {$\sim$ TBI}
		child { node [important] {Neurological Damage}
		{ child {node [important] {Brain Death} }
			child {node [important] {Recovery} }
		}
		}
		child {
			node [important] {$\sim$ Neurological Damage}
			child {node [important] {Recovery} }
	}
}
child {
	node {TBI}
	child { node [important]  {Neurological Damage}
	child {node [important]  {Brain Death} }
	child {node [important] [text width=6em] {Cardiac arrest without Brain Death} }
	child {node [important] {Recovery} }
		}
		child { node [important] {$\sim$ Neurological Damage} }
};
\end{filecontents}
\documentclass[12pt, compress]{beamer}
\usepackage{beamerthemeshadow}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usetheme{Warsaw}
\usecolortheme[named=purple]{structure}
\usetikzlibrary{external}
\tikzexternalize
\colorlet{pauloRed}{red!60!black!50}
\begin{document}

%Now seeting all things globaly
\tikzset{
		grow=right,
		level 1/.style={sibling distance=5.5cm,level distance=3.5cm},
		level 2/.style={sibling distance=2.5cm,level distance=4.5cm,},
		level 3/.style={sibling distance=1.5cm,level distance=4.5cm,},
		edge from parent/.style={thick,
%			draw=blue!40!black!60,
			draw=pauloRed,
		shorten >=5pt, shorten <=5pt},
		edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
		every node/.style={text ragged, inner
		sep=2mm,font=\sffamily,punkt},
		punkt/.style={rectangle, rounded corners, shade, top color=white,
%			bottom color=blue!50!black!20,
			bottom color=pauloRed!20!white,
%			draw=blue!40!black!60, 
			draw=pauloRed,
		thick,  },
		important/.style={font={\slshape\sffamily}}
}
\section{Critical Illness Patients}
\begin{frame}
	\begin{tikzpicture}
		\input{\jobname-treecode.pgf}
	\end{tikzpicture}
\end{frame}

\begin{frame}
	\begin{tikzpicture}[scale=0.7,transform shape]
		\input{\jobname-treecode.pgf}
	\end{tikzpicture}
\end{frame}

\begin{frame}
	\centering
	\includegraphics[width=\textheight]{\jobname-figure0}
\end{frame}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
paulosousa
Posts: 90
Joined: Sun Jun 24, 2012 8:48 pm

Problems with tikzpicture

Post by paulosousa »

Now I am a little embarrassed !
I can only do it including a png file... \includegraphics[width=\textheight]{ing/tree.png} but still don't know the meaning of 'externalise' the image!

When you write \filecontents you mean I must create a pgf file and then call it? How can I create such file?

Sorry for all the questions, and a special thanks for all attention including the new color suggestion.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problems with tikzpicture

Post by Johannes_B »

No need to be embarrased.

You don't have to generate a .pgf-file (a text file like tex is a text file). It was just for the convenience in the minimal working example. That made sure that both frames use the same code.
Externalizing the picture means, that your latex-run starts another one in the background exporting the tikzpicture to a pdf. This will be used in subsequent runs and saves compilation time. I just it here to generate the pdf, which can also be included via \includegraphics. You can find out more about externalize in the pgf-manual.

Btw: Helper almost always post minimal working examples. You can copy them, save them and compile them (be sure to make it in an extra folder). That way you can play around a bit. Another possibility is to click on »Open in writelatex«.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Problems with tikzpicture

Post by cgnieder »

Just a remark: please don't use the minimal class for a Infominimal working example. The class was never intended to be used by anyone and it doesn't implement all basic features that a class should have. Indeed with the minimal class you even may have new issues that you didn't have in the original document. If in doubt which class to use then use the article class.

Reference: Why should the minimal class be avoided?

Regards
site moderator & package author
Post Reply