3

I want to create a beamer presentation, where I intend to use one single TikZ graphics for 3 consecutive frames but with different node labels. I have a separate TikZ file where I use self defined commands for the label text that are redefined in the main TeX file for each frame respectively.

Problem 1: Since the labels differ in their size the placement of the graphic within the frame changes. Is there an easy way to apply TikZ coordinates relative to the frame or some other way to position the graphic equally for each frame?

Problem 2: When I redefine some label texts, partially additional margins occur. Can someone explain to me why this happens and how I can prevent this?( --> see minimal example, label at root node). I started to introduce for each label text individual label distance values for fine tuning but I really don't like this deficient work around. Has someone a more elegant solution?

Please note, that \lblAA, \lblBA, etc. are used to define the label texts and \sepAA, \sepBA, etc. were originally intend to fine tune spacing (which is set to 0cm in this example).

I appreciate any help or suggestions.

Thx in advance!

% MAIN TEX FILE
\documentclass[compress,nonav,10pt]{beamer}

\usetheme{Warsaw}

\makeatletter
\DeclareMathSizes{\@xpt}{\@xpt}{3}{2}
\makeatother

\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,shapes}

\begin{document}

\newcommand{\lblAA}{$\vec{\phi}_{1,1}$}
\newcommand{\sepAA}{0cm}
\newcommand{\lblBA}{$\vec{\phi}_{2,1}$}
\newcommand{\sepBA}{0cm}
\newcommand{\lblBB}{$\vec{\phi}_{2,2}$}
\newcommand{\sepBB}{0cm}
\newcommand{\lblCA}{$\vec{\phi}_{\log_2\mkern-5mu N,1}$}
\newcommand{\sepCA}{0cm}
\newcommand{\lblCD}{$\vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}$}
\newcommand{\sepCD}{0cm}
\newcommand{\lblDA}{$\vec{\psi}_1$}
\newcommand{\sepDA}{0cm}
\newcommand{\lblDB}{$\vec{\psi}_2$}
\newcommand{\sepDB}{0cm}
\newcommand{\lblDG}{$\vec{\psi}_{N-1}$}
\newcommand{\sepDG}{0cm}
\newcommand{\lblDH}{$\vec{\psi}_{N}$}
\newcommand{\sepDH}{0cm}

\begin{frame}{Tree}
\begin{figure}
\centering
\input{tree.tikz}
\end{figure}
\end{frame}

\renewcommand{\lblAA}{$\left \vert \left \langle \vec{\phi}_{1,1}, \vec{x} \right \rangle \right \vert > \tau_1$ ?}
\renewcommand{\sepAA}{0cm}
\renewcommand{\lblBA}{$\left \vert \left \langle \vec{\phi}_{2,1}, \vec{x} \right \rangle \right \vert > \tau_2$ ?}
\renewcommand{\sepBA}{0cm}
\renewcommand{\lblBB}{$\tau_2 < \left \vert \left \langle \vec{\phi}_{2,2}, \vec{x} \right \rangle \right \vert$ ?}
\renewcommand{\sepBB}{0cm}
\renewcommand{\lblCA}{$\left \vert \left \langle \vec{\phi}_{\log_2\mkern-5mu N,1}, \vec{x}\right \rangle \right \vert > \tau_{\log_2\mkern-5mu N}$ ?}
\renewcommand{\sepCA}{0cm}
\renewcommand{\lblCD}{$\tau_{\log_2\mkern-5mu N} < \left \vert \left \langle \vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}, \vec{x} \right \rangle \right \vert$ ?}
\renewcommand{\sepCD}{0cm}
\renewcommand{\lblDA}{$\left \langle \vec{\psi}_1, \vec{x} \right \rangle$}
\renewcommand{\sepDA}{0cm}
\renewcommand{\lblDB}{$\left \langle \vec{\psi}_2, \vec{x} \right \rangle$}
\renewcommand{\sepDB}{0cm}
\renewcommand{\lblDG}{$\left \langle \vec{\psi}_{N-1}, \vec{x} \right \rangle$}
\renewcommand{\sepDG}{0cm}
\renewcommand{\lblDH}{$\left \langle \vec{\psi}_{N}, \vec{x} \right \rangle$}
\renewcommand{\sepDH}{0cm}

\begin{frame}{Tree}
\begin{figure}
\centering
\input{tree.tikz}
\end{figure}
\end{frame}

\end{document}


% THE TIKZ FILE ("tree.tikz")
\newcommand{\dx}{1.8}
\newcommand{\dy}{3}

\begin{tikzpicture}[scale=.5, every node/.style={fill=black,circle,inner sep=0pt,circle,minimum size=.2cm}]
\normalsize

\coordinate (N11) at (0,0);
\node[label={[label distance=\sepAA]above:{\lblAA}}] at (N11) {};

\coordinate (N21) at ($ (N11) + (-\dx, -\dy) $);
\node[label={[label distance=\sepBA]west:{\lblBA}}] at (N21) {};
\draw[-, thick] (N21) -- (N11);

\coordinate (N22) at ($ (N11) + (\dx, -\dy) $);
\node[ label={[label distance=\sepBB]east:{\lblBB}}] at (N22) {};
\draw[-, thick] (N22) -- (N11);

\coordinate (N31) at ($ (N21) + (-\dx, -\dy) $);
\node[label={[label distance=\sepCA]west:{\lblCA}}] at (N31) {};
\draw[dash pattern=on 1pt off 2pt, thick] (N21) -- ($ (N21) + .33*(-\dx, -\dy) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N21) + .66*(-\dx, -\dy) $) -- (N31);
\draw[dash pattern=on 1pt off 2pt, thick] (N21) -- ($ (N21) + .33*(\dx, -\dy) $);

\coordinate (N34) at ($ (N22) + (\dx, -\dy) $);
\node[ label={[label distance=\sepCD]east:{\lblCD}}] at (N34) {};
\draw[dash pattern=on 1pt off 2pt, thick] (N22) -- ($ (N22) + .33*(-\dx, -\dy) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N22) + .66*(\dx, -\dy) $) -- (N34);
\draw[dash pattern=on 1pt off 2pt, thick] (N22) -- ($ (N22) + .33*(\dx, -\dy) $);
  %\draw[-, thick] (N22) -- (N34);

\coordinate (N41) at ($ (N31) + (-\dx, -\dy) $);
\node[ label={[label distance=\sepDA]below:{\lblDA}}] at (N41) {};
\draw[-, thick] (N31) -- (N41);

\coordinate (N42) at ($ (N31) + (\dx, -\dy) $);
\node[ label={[label distance=\sepDB]below:{\lblDB}}] at (N42) {};
\draw[-, thick] (N31) -- (N42);

\coordinate (N47) at ($ (N34) + (-\dx, -\dy) $);
\node[ label={[label distance=\sepDG]below:{\lblDG}}] at (N47) {};
\draw[-, thick] (N34) -- (N47);

\coordinate (N48) at ($ (N34) + (\dx, -\dy) $);
\node[ label={[label distance=\sepDH]below:{\lblDH}}] at (N48) {};
\draw[-, thick] (N34) -- (N48);

\draw[dash pattern=on 1pt off 2pt, thick] ($ (N42) + .6*(\dx, 0)$) -- ($ (N47) + .6*(-\dx, 0) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N31) + .9*(\dx, 0)$) -- ($ (N34) + .9*(-\dx, 0) $);

\end{tikzpicture}
  • The problem is that you used every node/.style with circle and labels are really nodes, so if you increase the label text, the node increases its radius, pushing the tree down. – Gonzalo Medina Jan 26 '14 at 23:43
  • Do you need each tree on a separate frame or could they be on separate slides of a single frame? – Gonzalo Medina Jan 27 '14 at 0:29
  • Different slides would be okay. I wasn't aware that one can have multiple slides per frame.I simply want to navigate through 3 slides and want to have the tree at the exact same position per slide and only want to let change the label texts. What would you suggest then? – tmp Jan 27 '14 at 9:26
  • Thank you Gonzalo Medina. Your comment was very helpful for me. I changed the "\begin{tikzpicture}[...]" line as follows: \begin{tikzpicture}[scale=.5, every node/.style={fill=black,inner sep=0pt,minimum size=.15cm}]. This results in square tree nodes which is totally fine for me and prevents the large margins as explained by Gonzalo Medina. Now, there is still a minor repositioning of the tree, which one could accept, but if someone has a clean and easy solution for perfectly "static" positioning this would be nice. – tmp Jan 27 '14 at 13:21
  • I've added an answer, showing one possible way to solve the problems. – Gonzalo Medina Jan 29 '14 at 1:50
2

The problems mentiones come from the fact that you used every node/.style with circle, and the labels are really nodes, so if you increase the label text, the node increases its radius, pushing the tree down.

Below I show a possible solution for both problems mentioned:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\newcommand\NodeC[3]{%
  \temporal<2>{#1}{#2}{#3}
}

\begin{document}

\begin{frame}
\frametitle<1-3>{A tree with changing labels}
\begin{tikzpicture}[scale=0.8,
  remember picture,
  overlay,
  node distance=1.5cm and 0.75cm,
  mynode/.style={
    fill=black,
    circle,
    inner sep=0pt,
    circle,
    minimum size=.2cm}
]

% The nodes and some auxiliary coordinates
\node[mynode] at ([yshift=-0.3\textheight]current page.north) (root) {};
\node[mynode,below left=of root] (L) {};
\node[mynode,below right=of root] (R) {};
\node[mynode,below left=of L] (LL) {};
\node[mynode,below right=of R] (RR) {};
\node[mynode,below left=of LL] (LLL) {};
\node[mynode,below right=of LL] (LLR) {};
\node[mynode,below left=of RR] (RRL) {};
\node[mynode,below right=of RR] (RRR) {};

\coordinate[below right=of L] (LR);
\coordinate[below left=of R] (RL);

\path (L) -- coordinate[pos=0.3333] (aux1) coordinate[pos=0.6666] (aux2) (LL);
\path (R) -- coordinate[pos=0.3333] (aux3) coordinate[pos=0.6666] (aux4) (RR);
\path (L) -- coordinate[pos=0.3333] (aux5) (LR);
\path (R) -- coordinate[pos=0.3333] (aux6) (RL);

% Uncomment the follwing lines if you want to see 
% placement of the aux. coordinates
%\foreach \i in {1,...,6}
  %\node at (aux\i) {aux\i};

% The solid lines
\draw (L) -- (root) -- (R);
\draw (LLL) -- (LL) -- (LLR);
\draw (RRL) -- (RR) -- (RRR);

% The dashed lines
\draw[dash pattern=on 1pt off 2pt, thick] (L) -- (aux1);
\draw[dash pattern=on 1pt off 2pt, thick] (aux2) -- (LL);
\draw[dash pattern=on 1pt off 2pt, thick] (L) -- (aux5);
\draw[dash pattern=on 1pt off 2pt, thick] (R) -- (aux3);
\draw[dash pattern=on 1pt off 2pt, thick] (aux4) -- (RR);
\draw[dash pattern=on 1pt off 2pt, thick] (R) -- (aux6);
\draw[dash pattern=on 1pt off 2pt, thick] (L|-LL) -- (R|-RR);
\draw[dash pattern=on 1pt off 2pt, thick] ([xshift=5pt]aux5|-LLR) -- ([xshift=-5pt]aux6|-RRL);

% The labels
\node[above=3pt of root.north] 
  {\NodeC{$\vec{\phi}_{1,1}$}{$\vert \langle \vec{\phi}_{1,1}, \vec{x} \rangle \vert > \tau_1$ ?}{$\dfrac{a}{b}$}};

% level 1
\node[left=3pt of L,anchor=east] 
  {\NodeC{$\vec{\phi}_{2,1}$}{$\vert \langle \vec{\phi}_{2,1}, \vec{x} \rangle \vert > \tau_2$ ?}{$\dfrac{a}{b}$}};
\node[right=3pt of R,anchor=west] 
  {\NodeC{$\vec{\phi}_{2,2}$}{$\tau_2 < \vert \langle \vec{\phi}_{2,2}, \vec{x} \rangle \vert$ ?}{$\dfrac{a}{b}$}};

%level N
\node[left=3pt of LL,anchor=east] 
  {\NodeC{$\vec{\phi}_{\log_2\mkern-5mu N,1}$}{$\vert \langle \vec{\phi}_{\log_2\mkern-5mu N,1}, \vec{x}\rangle \vert > \tau_{\log_2\mkern-5mu N}$ ?}{$\dfrac{a}{b}$}};
\node[right=3pt of RR,anchor=west] 
  {\NodeC{$\vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}$}{$\tau_{\log_2\mkern-5mu N} < \vert \langle \vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}, \vec{x} \rangle \vert$ ?}{$\dfrac{a}{b}$}};

% leaves
\node[below=3pt of LLL]  
  {\NodeC{$\vec{\psi}_1$}{$\langle \vec{\psi}_1, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of LLR]  
  {\NodeC{$\vec{\psi}_2$}{$\langle \vec{\psi}_1, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of RRL]  
  {\NodeC{$\vec{\psi}_{N-1}$}{$\langle \vec{\psi}_{N-1}, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of RRR]  
  {\NodeC{$\vec{\psi}_N$}{$\langle \vec{\psi}_N, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

Some remarks

  1. Instead of \inputting the tree three times in different frames, I used overlay specification to have just one tree on three slides with changing labels.

  2. I used \temporal to have the different labels for the three slides.

  3. Using tikz-qtree or forest, the code can be greatly simplified.

  4. I placed the root at an absolute coordinate using a shifting of the current page.north anchor.

  5. The other nodes were placed using the positioning library.

| improve this answer | |
  • 1
    @tmp You're welcome! Glad I could help. – Gonzalo Medina Jan 29 '14 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.