2

I'm working on a tikz picture, and failed to vertical align tikz pictures in one beamer frame. I did many google search and finally found it may be to do with inner sep of matrix. Is is possible to get the inner sep in figure 1, and used it in figure 3?

There is another problem of absolute position, if the matrix in figure 3 grows from one row to two rows, or from no arrows to have arrows, the absolute position of 3 figures all changes.

(In fact what I actually want to do is to make animation which forms a dependency tree from an initial state to a terminal state. The transitions between states are some what complicated, so I plan to draw each transition in one page, and convert one page to one static picture, and finally convert all static pictures into a gif.)

\documentclass{beamer}
\setbeamersize{text margin left=0pt}
\usepackage{tikz}
\usetikzlibrary{arrows, chains, fit, quotes, matrix, positioning}
\usepackage{mathtools}

\begin{document}
\tikzstyle{every picture}+=[remember picture]

\begin{frame}[fragile]
    \begin{tikzpicture}[
        node distance = 5mm,
        start chain = A going right,
        txt/.style = {text height=2ex, text depth=0.25ex, on chain},
        edge1/.style = {draw, -stealth', dotted, gray},
        edge2/.style = {draw, -stealth'},
                                ]
        %\draw [help lines] (-1,-1) grid (10,2);
        \node [txt] {Configuration $C_i$};
        \node [txt] {I};
        \node [txt] {booked};
        \node [txt] {a};
        \node [txt] {ticket};
        \node [txt] {to};
        \node [txt] {China};
        %
        \node (f1)  [rounded corners=3mm, line width=1mm, draw=blue!50, inner sep=0pt,
                        label={[blue!50]below:Stack $\delta$},
                        fit=(A-2) (A-3)] {};
        \node (f2)  [rounded corners=3mm, line width=1mm, draw=green!70!black, text=green!70!black, inner sep=0pt,
                        label={[green!70!black]below:Buffer $\beta$},
                        fit=(A-4) (A-7)] {};
        %
        \path   (A-3) edge[edge2,bend right=70, "nsubj" '] (A-2)
                (A-3) edge[edge1,bend  left=90, "dobj"   ] (A-5)
                (A-5) edge[edge1,bend right=70, "det"   '] (A-4)
                (A-5) edge[edge1,bend  left=70, "prep"   ] (A-6)
                (A-6) edge[edge1,bend  left=70, "pobj"   ] (A-7);
    \end{tikzpicture}

    \begin{tikzpicture}[
            node distance = 5mm,
            start chain = B going right,
        txt/.style = {fill=red!50, on chain, text=white},
        title/.style = {on chain},
        ]
        \node [title] {Action $C_i \xrightarrow{a_i} C_{i+1}$};
        \node [txt] {SHIFT};
        \node [txt] {LEFT\_ARC};
        \node [txt] {RIGHT\_ARC};
    \end{tikzpicture}

    \begin{tikzpicture}
    \matrix (m) [matrix of nodes,row sep=1em,column sep=1.3em,
        % How can I get the inner sep of (A-1) or (B-1),
        % so that the tree pictures are vertical aligned?
        inner sep=0em,
        ]
    {
        Transition sequence & $C_0$ & $C_1$ & $C_2$ & $C_3$ & $C_4$ & $C_5$ \\
        & {\phantom{$C_0$}} & $C_6$ & $C_7$ & $C_8$ &    \\
    };

    \path[-stealth]
      (m-1-2) edge node [above] {$a_0$} (m-1-3)
      (m-1-3) edge node [above] {$a_1$} (m-1-4)
      (m-1-4) edge node [above] {$a_2$} (m-1-5)
      (m-1-5) edge node [above] {$a_3$} (m-1-6)
      (m-1-6) edge node [above] {$a_4$} (m-1-7)
      (m-2-2) edge node [above] {$a_6$} (m-2-3)
      (m-2-3) edge node [above] {$a_7$} (m-2-4)
      (m-2-4) edge node [above] {$a_8$} (m-2-5);
    \end{tikzpicture}

\end{frame}

\end{document}

The example in the beamer is adapted from slides of Slav Petrov. Original picture is enter image description here

What I get now is enter image description here


EDIT:

As @Ignasi suggested, I used t option in documentclass, the absolute position of 3 figures nows looks much better, except figure 3's position is still minor changed.

  • If you use option t in documentclass or frame, the frame contents will be top aligned instead of default central alignment. It's this central alignment which moves everything when you changes matrix from one to two lines. – Ignasi Sep 1 '16 at 9:51
4

The problem with inner sep is easy to solve. You have declared inner sep=0em in your matrix. All nodes will use this inner sep, but if you want to change it to some particular node, can always do it introducing particular options to any node with syntax |[...]|. This is what I've done in next code which also shows background rectangle of every figure (backgrounds tikxlibrary) and node size (with draw) for every title node.

\documentclass{beamer}
\setbeamersize{text margin left=0pt}
\usepackage{tikz}
\usetikzlibrary{arrows, chains, fit, quotes, matrix, positioning, backgrounds}
\usepackage{mathtools}

\begin{document}
\tikzstyle{every picture}+=[remember picture]

\begin{frame}[fragile]
    \begin{tikzpicture}[show background rectangle,
        node distance = 5mm,
        start chain = A going right,
        txt/.style = {text height=2ex, text depth=0.25ex, on chain},
        edge1/.style = {draw, -stealth', dotted, gray},
        edge2/.style = {draw, -stealth'},
                                ]
        %\draw [help lines] (-1,-1) grid (10,2);
        \node [txt, draw] {Configuration $C_i$};
        \node [txt] {I};
        \node [txt] {booked};
        \node [txt] {a};
        \node [txt] {ticket};
        \node [txt] {to};
        \node [txt] {China};
        %
        \node (f1)  [rounded corners=3mm, line width=1mm, draw=blue!50, inner sep=0pt,
                        label={[blue!50]below:Stack $\delta$},
                        fit=(A-2) (A-3)] {};
        \node (f2)  [rounded corners=3mm, line width=1mm, draw=green!70!black, text=green!70!black, inner sep=0pt,
                        label={[green!70!black]below:Buffer $\beta$},
                        fit=(A-4) (A-7)] {};
        %
        \path   (A-3) edge[edge2,bend right=70, "nsubj" '] (A-2)
                (A-3) edge[edge1,bend  left=90, "dobj"   ] (A-5)
                (A-5) edge[edge1,bend right=70, "det"   '] (A-4)
                (A-5) edge[edge1,bend  left=70, "prep"   ] (A-6)
                (A-6) edge[edge1,bend  left=70, "pobj"   ] (A-7);
    \end{tikzpicture}

    \begin{tikzpicture}[show background rectangle,
            node distance = 5mm,
            start chain = B going right,
        txt/.style = {fill=red!50, on chain, text=white},
        title/.style = {on chain},
        ]
        \node [title,draw] {Action $C_i \xrightarrow{a_i} C_{i+1}$};
        \node [txt] {SHIFT};
        \node [txt] {LEFT\_ARC};
        \node [txt] {RIGHT\_ARC};
    \end{tikzpicture}

    \begin{tikzpicture}[show background rectangle,]
    \matrix (m) [matrix of nodes,row sep=1em,column sep=1.3em,
        % How can I get the inner sep of (A-1) or (B-1),
        % so that the tree pictures are vertical aligned?
        inner sep=0em,
        ]
    {
       |[draw, inner sep=.3333em]|Transition sequence & $C_0$ & $C_1$ & $C_2$ & $C_3$ & $C_4$ & $C_5$ \\
        & {\phantom{$C_0$}} & $C_6$ & $C_7$ & $C_8$ &    \\
    };

    \path[-stealth]
      (m-1-2) edge node [above] {$a_0$} (m-1-3)
      (m-1-3) edge node [above] {$a_1$} (m-1-4)
      (m-1-4) edge node [above] {$a_2$} (m-1-5)
      (m-1-5) edge node [above] {$a_3$} (m-1-6)
      (m-1-6) edge node [above] {$a_4$} (m-1-7)
      (m-2-2) edge node [above] {$a_6$} (m-2-3)
      (m-2-3) edge node [above] {$a_7$} (m-2-4)
      (m-2-4) edge node [above] {$a_8$} (m-2-5);
    \end{tikzpicture}

\end{frame}

\end{document}

enter image description here

  • What if don't define inner sep=0em in matrix, as left it to the default value. Because the inner sep=0em seems affect other nodes as well, it is too compact. – icycandy Aug 31 '16 at 10:33
  • Ah, I figured it out. If don't define inner sep=0em in matrix, I need to define |[draw, inner sep=0em]|Transition sequence in the beginning node. – icycandy Aug 31 '16 at 10:37
  • 1
    @icycandy: Be careful, a matrix is a node which surrounds other nodes and all of them share same inner sep. If you don't define inner sep=0pt you need to insert a particular inner sep=0pt for title node, because matrix node already introduced its non empty inner sep. May be this answer explains it better. – Ignasi Aug 31 '16 at 11:41

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.