如何对齐两张 tikzpictures?

如何对齐两张 tikzpictures?

我绘制了两个并排的图表,如下所示:

\documentclass[standalone]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shadows}
\begin{document}
\begin{frame}
    \begin{center}
    \begin{tikzpicture}[scale=0.6]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2.5,-1.5) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (1,-3) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (4,0) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (5.5,-1.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1.5) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, 0) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [->] node {} (5);
    \path (4) edge [->] node {} (3);
    \path (5) edge [loop above] node {} (5);
    \path (6) edge [->] node {} (5);
    \end{tikzpicture}
    \hspace{1cm}
    \vspace{-1cm}
    \begin{tikzpicture}[scale=0.6]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2,1) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (0.5, -0.5) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (3.8,2.5) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (3.8, 0.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, -0.5) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [loop above] node {} (3);
    \path (4) edge [->] node {} (3);
    \path (5) edge [->] node {} (3);
    \path (6) edge [->] node {} (5);
    \end{tikzpicture}
\end{center}
\end{frame}
\end{document}

我不知道如何让根节点垂直对齐。似乎无论我如何调整绝对位置,这两个图总是与底部对齐,并且由于它们的高度不同,这意味着根节点没有垂直对齐。

答案1

我认为最简单的解决方案是将基线设置为您想要对齐的任何节点[baseline=(anchor)]

\documentclass[standalone]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shadows}
\begin{document}
\begin{frame}
    \begin{center}
    \begin{tikzpicture}[scale=0.6, baseline=(5)]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2.5,-1.5) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (1,-3) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (4,0) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (5.5,-1.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1.5) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, 0) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [->] node {} (5);
    \path (4) edge [->] node {} (3);
    \path (5) edge [loop above] node {} (5);
    \path (6) edge [->] node {} (5);
    \end{tikzpicture}
    \hspace{1cm}
    \vspace{-1cm}
    \begin{tikzpicture}[scale=0.6, baseline=(3)]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2,1) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (0.5, -0.5) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (3.8,2.5) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (3.8, 0.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, -0.5) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [loop above] node {} (3);
    \path (4) edge [->] node {} (3);
    \path (5) edge [->] node {} (3);
    \path (6) edge [->] node {} (5);
    \end{tikzpicture}
\end{center}
\end{frame}
\end{document}

答案2

\begin{scope} [shift = {(10cm,-1cm)}]使用类似的范围在同一个 TikZ 环境内移动第二张图片,然后将单张图片插入到 beamer 中要容易得多。

\documentclass[standalone]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shadows}
\usepackage{array}
\begin{document}
\begin{frame}
    \begin{center}
    \begin{tikzpicture}[scale=0.6]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2.5,-1.5) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (1,-3) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (4,0) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (5.5,-1.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1.5) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, 0) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [->] node {} (5);
    \path (4) edge [->] node {} (3);
    \path (5) edge [loop above] node {} (5);
    \path (6) edge [->] node {} (5);
    %
    \begin{scope}[shift={(10cm,-1cm)}]
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2,1) {1};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (0.5, -0.5) {2};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (3.8,2.5) {3};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (3.8, 0.5) {4};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1) {5};
    \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, -0.5) {6};
    \path (1) edge [->] node {} (3);
    \path (2) edge [->] node {} (1);
    \path (3) edge [loop above] node {} (3);
    \path (4) edge [->] node {} (3);
    \path (5) edge [->] node {} (3);
    \path (6) edge [->] node {} (5);
    \end{scope}
    \end{tikzpicture}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

答案3

一个快速的解决方案是column对每个 tikzpicture 使用一个并将它们垂直对齐到顶部:

\documentclass[standalone]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, shadows}
\usepackage{default}
\begin{document}
\begin{frame}
    \begin{columns}[T]
        \begin{column}{.49\linewidth}
            \begin{tikzpicture}[scale=0.6]
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2.5,-1.5) {1};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (1,-3) {2};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (4,0) {3};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (5.5,-1.5) {4};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1.5) {5};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, 0) {6};
                \path (1) edge [->] node {} (3);
                \path (2) edge [->] node {} (1);
                \path (3) edge [->] node {} (5);
                \path (4) edge [->] node {} (3);
                \path (5) edge [loop above] node {} (5);
                \path (6) edge [->] node {} (5);
            \end{tikzpicture}
        \end{column}
        \begin{column}{.49\linewidth}
            \begin{tikzpicture}[scale=0.6]
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (1) at (2,1) {1};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (2) at (0.5, -0.5) {2};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (3) at (3.8,2.5) {3};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (4) at (3.8, 0.5) {4};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (5) at (5.5, 1) {5};
                \node[shape=circle,draw=black,fill=white, drop shadow,minimum size=1cm] (6) at (7, -0.5) {6};
                \path (1) edge [->] node {} (3);
                \path (2) edge [->] node {} (1);
                \path (3) edge [loop above] node {} (3);
                \path (4) edge [->] node {} (3);
                \path (5) edge [->] node {} (3);
                \path (6) edge [->] node {} (5);
            \end{tikzpicture}
        \end{column}
    \end{columns}
\end{frame}
\end{document}

预览

相关内容