如何将 TikZ 树置于 Beamer 幻灯片的中心?

如何将 TikZ 树置于 Beamer 幻灯片的中心?

我使用以下代码在 Beamer 幻灯片中创建了一棵 tikz 树:

\documentclass[aspectratio=54, 10pt]{beamer}
\usetheme[progressbar=frametitle]{metropolis}
\setbeamertemplate{frame numbering}[fraction]
\useoutertheme{metropolis}
\useinnertheme{metropolis}
\usefonttheme[onlymath]{serif}
\usecolortheme{spruce}
\setbeamercolor{background canvas}{bg=white}

\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{ragged2e,mathtools,tikz}
\usetikzlibrary{trees}
\addtobeamertemplate{block begin}{}{\justifying}

\metroset{block=fill}
\useinnertheme{tcolorbox}
\usetikzlibrary{decorations.pathreplacing}

\justifying

\title{Probabilité}
\subtitle{Probabilité Conditionnelle}
\date[]{}

\begin{document}
\maketitle

\begin{frame}{Correction Exercice 5}
    On peut représenter cette situation dans le diagramme suivant, avec les événements \alert{$S$: Le candidat tire l'épreuve de statistique} et \alert{$R$: Le candidat réussit le concours}.
% Set the overall layout of the tree
    \tikzstyle{level 1}=[level distance=2.5cm, sibling distance=3cm]
    \tikzstyle{level 2}=[level distance=3.5cm, sibling distance=1.7cm]
    % Define styles for bags and leafs
    \tikzstyle{bag} = [text width=4em, text centered]
    \tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
    \begin{center}
        \begin{tikzpicture}[ sloped]
            \node[circle, draw] {}
                child {
                    node[bag] {$S$}        
                        child {
                            node[end, label=below:
                                {$P(R|S)=1/3$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|S)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                            edge from parent 
                            node[above] {}
                            node[below]  {}
                    }
                child {
                    node[bag] {$E_2$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_2)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_2)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                child {
                    node[bag] {$E_3$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_3)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_3)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                child {
                    node[bag] {$E_4$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_4)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_4)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                ;
        \end{tikzpicture}
    \end{center} 
\end{frame}

\end{document}

但是树并不完全在幻灯片内:

在此处输入图片描述

我如何才能让树在幻灯片内完美居中并可见?提前谢谢您。

答案1

作为暂时获得更多空间的快速方法,您可以使用 beamercolumns环境,它将有效地删除文本边距:

\documentclass[aspectratio=54, 10pt]{beamer}
\usetheme[progressbar=frametitle]{moloch}% modern fork of the metropolis theme
\setbeamertemplate{page number in head/foot}[totalframenumber]
\usefonttheme[onlymath]{serif}
\usecolortheme{spruce}
\setbeamercolor{background canvas}{bg=white}

\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{ragged2e,mathtools,tikz}
\usetikzlibrary{trees}
\addtobeamertemplate{block begin}{}{\justifying}

\useinnertheme{tcolorbox}
\usetikzlibrary{decorations.pathreplacing}

\justifying

\title{Probabilité}
\subtitle{Probabilité Conditionnelle}
\date[]{}

\begin{document}

\begin{frame}
\frametitle{Correction Exercice 5}
    On peut représenter cette situation dans le diagramme suivant, avec les événements \alert{$S$: Le candidat tire l'épreuve de statistique} et \alert{$R$: Le candidat réussit le concours}.
% Set the overall layout of the tree
    \tikzset{
      level 1/.style={level distance=2.5cm, sibling distance=3cm},
      level 2/.style={level distance=3.5cm, sibling distance=1.7cm},
      bag/.style={text width=4em, text centered},
      end/.style={circle, minimum width=3pt,fill, inner sep=0pt}
    }
    \begin{columns}
    \begin{column}{\paperwidth}
    \centering
    \small
        \begin{tikzpicture}[sloped]
            \node[circle, draw] {}
                child {
                    node[bag] {$S$}        
                        child {
                            node[end, label=below:
                                {$P(R|S)=1/3$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|S)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                            edge from parent 
                            node[above] {}
                            node[below]  {}
                    }
                child {
                    node[bag] {$E_2$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_2)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_2)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                child {
                    node[bag] {$E_3$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_3)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_3)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                child {
                    node[bag] {$E_4$}        
                    child {
                            node[end, label=below:
                                {$P(R|E_4)$}] {}
                            edge from parent
                            node[above] {$R$}
                            node[below]  {}
                             }
                        child {
                            node[end, label=below:
                                {$P(\overline{R}|E_4)$}] {}
                            edge from parent
                            node[above] {$\overline{R}$}
                            node[below]  {}
                            }
                    edge from parent         
                        node[above] {}
                        node[below]  {}
                }
                ;
        \end{tikzpicture}
        \end{column}
    \end{columns} 
\end{frame}

\end{document}

在此处输入图片描述

答案2

我会按如下方式重绘您的图像:

\documentclass{beamer}

\usepackage[french]{babel}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{frame}{Correction Exercice 5}
    On peut représenter cette situation dans le diagramme suivant, avec les événements \alert{$S$: Le candidat tire l'épreuve de statistique} et \alert{$R$: Le candidat réussit le concours}.
% Set the overall layout of the tree
    \begin{center}
        \begin{tikzpicture}[sloped,
every label/.append style = {font=\scriptsize, text width=3em, align=center},
%
           level distance = 22mm, 
           level 1/.style = {sibling distance=24mm},
           level 2/.style = {sibling distance=12mm},   
    bag/.style = {text width=4em, text centered},
    end/.style = {circle, minimum width=3pt, fill, inner sep=0pt}
                            ]
\node[circle, draw] {}
    child { node[bag]    {$S$}
        child { node[end,label=below: {$P(R|S)$ $=1/3$}] {}
                    edge from parent node[above] {$R$}}
        child { node[end,label=below: {$P(\bar{R}|S)$}] {}
                    edge from parent node[above] {$\bar{R}$}
                }
            }
    child { node[bag]    {$E_2$}
        child { node[end,label=below: {$P(R|E_2)$}] {}
                    edge from parent node[above] {$R$}}
        child { node[end,label=below: {$P(\bar{R}|E_2)$}] {}
                    edge from parent node[above] {$\bar{R}$}
                }
            }
    child { node[bag]    {$E_3$}
        child { node[end,label=below: {$P(R|E_3)$}] {}
                    edge from parent node[above] {$R$}}
        child { node[end,label=below: {$P(\bar{R}|E_3)$}] {}
                    edge from parent node[above] {$\bar{R}$}
                }
            }
    child { node[bag]    {$E_4$}
        child { node[end,label=below: {$P(R|E_4)$}] {}
                    edge from parent node[above] {$R$}}
        child { node[end,label=below: {$P(\bar{R}|E_4)$}] {}
                    edge from parent node[above] {$\bar{R}$}
                }
            }
%
            ;             
\end{tikzpicture}
    \end{center}
\end{frame}
\end{document}

在此处输入图片描述

相关内容