tikzpicture 在 beamer 中的绝对位置

tikzpicture 在 beamer 中的绝对位置

我正在处理 tikz 图片,但无法在一个 beamer 框架中垂直对齐 tikz 图片。我在谷歌上搜索了很多次,最后发现这可能与矩阵的内部分离有关。是否可以在图 1 中获取内部分离,并在图 3 中使用它?

还有一个绝对位置的问题,如果图3中的矩阵从一行增长到两行,或者从无箭头到有箭头,3个图的绝对位置都会发生变化。

(实际上我真正想要做的是制作一个从初始状态到终止状态的依赖树的动画。状态之间的转换有些复杂,所以我计划在一页中绘制每个转换,并将一页转换为一张静态图片,最后将所有静态图片转换为 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}

beamer 中的示例改编自斯拉夫·佩特洛夫的幻灯片. 原图是 在此处输入图片描述

我现在得到的是 在此处输入图片描述


编辑:

正如@Ignasi 所建议的,我使用了t选项documentclass,3 个图的绝对位置现在看起来好多了,除了图 3 的位置仍然有微小的变化。

答案1

的问题inner sep很容易解决。您已inner sep=0em在矩阵中声明。所有节点都将使用此inner sep,但如果您想将其更改为某个特定节点,可以随时使用语法向任何节点引入特定选项|[...]|。这是我在下一个代码中所做的,它还显示了background rectangle每个图形(backgroundstikxlibrary)和draw每个标题节点的节点大小(带有)。

\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}

在此处输入图片描述

相关内容