在 Beamer Latex 中缩放 tikz 图形

在 Beamer Latex 中缩放 tikz 图形

我有以下示例:

\documentclass{beamer}

\mode<presentation> {
\usetheme{Madrid}
\usecolortheme[RGB={0,0,0}]{structure}
}

\usepackage{amsmath,amsfonts,graphicx}
\usepackage{algpseudocode}
\usepackage{tikz, nth}

\usetikzlibrary{arrows.meta,decorations,decorations.pathreplacing,calc,bending,positioning, chains}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{frame}
\frametitle{Example: AVL Trees}
\uncover<+->{{\bf 2. }
Consider the following tree $T$:
\resizebox{0.30cm}{!}{
\begin{tikzpicture}[%
  transform canvas={scale=0.6},
  level distance=45 pt,
  every node/.style={circle,draw},
  level 1/.style={sibling distance=200 pt},
  level 2/.style={sibling distance=100 pt},
  level 3/.style={sibling distance=60 pt}%
]
  \node {55}
    child {node {46}
      child {node {13}
        child {node {7}}
        child {node {42}
          child {node {17}}
          child {node {45}}
        }
      }
      child {node {49}
        child {edge from parent[draw=none]}
        child {node {51}}
      }
    }
    child {node {82}
      child {node {59}}
      child {node {92}
        child {node {89}}
        child {edge from parent[draw=none]}
      }
    }
  ;
\end{tikzpicture}
}
\begin{enumerate}[(a)]
\item{Show that $T$ is an AVL tree by writing in the balance factors at each node.}
\item{Starting from an empty tree, in what order should we add the integers $7,13,\dots,92$ to obtain the tree above?}
\end{enumerate}
}
\end{frame}
\end{document}

最初,树不能很好地适应幻灯片,因此我按照其他几篇帖子的建议使用了 resizebox。但是,这会导致随后与逐项列表的放置相关的其他几个问题。这是为什么呢?

答案1

transform canvas似乎出于某种原因把事情搞乱了,但你不需要那样做,也不需要resizebox。只需减小节点、和 的字体大小level distance即可sibling distance

\documentclass{beamer}

\usepackage{tikz}

\begin{document}
\begin{frame}
\frametitle{Example: AVL Trees}
\textbf{2.}
Consider the following tree $T$:
\begin{tikzpicture}[%
  level distance=15 pt,
  every node/.style={circle,draw,font=\scriptsize},
  level 1/.style={sibling distance=150 pt},
  level 2/.style={sibling distance=70 pt},
  level 3/.style={sibling distance=60 pt}%
]
  \node {55}
    child {node {46}
      child {node {13}
        child {node {7}}
        child {node {42}
          child {node {17}}
          child {node {45}}
        }
      }
      child {node {49}
        child {edge from parent[draw=none]}
        child {node {51}}
      }
    }
    child {node {82}
      child {node {59}}
      child {node {92}
        child {node {89}}
        child {edge from parent[draw=none]}
      }
    }
  ;
\end{tikzpicture}

\medskip

\begin{enumerate}[(a)]
\item Show that $T$ is an AVL tree by writing in the balance factors at each node.
\item Starting from an empty tree, in what order should we add the integers $7,13,\dots,92$ to obtain the tree above?
\end{enumerate}

\end{frame}
\end{document}

在此处输入图片描述

答案2

您还可以使用forest优化树大小的方法。我更改了幻灯片分布并将其内容分布在两列中。

在此处输入图片描述

\documentclass{beamer}

\mode<presentation> {
\usetheme{Madrid}
\usecolortheme[RGB={0,0,0}]{structure}
}

\usepackage{forest}

\begin{document}
\begin{frame}
\frametitle{Example: AVL Trees}
\begin{columns}
\column{.5\textwidth}
\textbf{2.} Consider the tree $T$ (at right):
\begin{enumerate}[(a)]
\item{Show that $T$ is an AVL tree by writing in the balance factors at each node.}
\item{Starting from an empty tree, in what order should we add the integers $7,13,\dots,92$ to obtain the tree at right?}
\end{enumerate}
\column{.5\textwidth}
\begin{forest}
    for tree={circle,draw}
  [55
        [46
            [13
                [7]
                [42
                    [17] [45]]]
            [49 [,phantom] [51]]]
        [82
            [59]
            [92
                [89][,phantom]]]]
  \end{forest}
\end{columns}
\end{frame}
\end{document}

相关内容