Beamer 和图表

Beamer 和图表

我有一个很长的图表,我想让它在一个框架中显示

\begin{frame}

\newcommand{\rect}[4]{\node[draw,minimum height=1cm,minimum width=4cm] (#3) at (#1,#2) {\begin{minipage}{3.8cm}\centering #4\end{minipage}};}
\newcommand{\prect}[3]{\node[draw,minimum height=1cm,minimum width=1.8cm] (#3) at (#1,#2) {$#3$};}
\newcommand{\diam}[4]{\node[draw,diamond,aspect=3,minimum width=4cm] (#3) at (#1,#2) {\smash{\begin{minipage}[1pt]{3.8cm}\centering #4\end{minipage}}};}


\begin{tikzpicture}[x=-.350cm,y=-.350cm]
%  \foreach \x/\y/\k in {0/0/A, 0/5/B, 0/10/C, %0/15/D, % on retire D de la liste
%    0/20/E, 0/30/Q, 16/20/F, 16/25/G, 16/30/M, 16/35/N, 16/40/O, 32/25/H, 32/30/I, 32/40/L} {
%    \rect{\x}{\y}{\k}
%  }
\tikzstyle{P}=[diamond, aspect=3,thick, draw,minimum width=2.5cm]
\tikzstyle{R}=[diamond, aspect=2,thick, draw,minimum width=2.5cm]
\tikzstyle{N}=[diamond, aspect=3,thick, draw,minimum width=2.5cm]

   \node[draw,minimum width=4cm](A) at (0,0){a};
     \node[draw,minimum width=4cm](B) at (0,5){a};
       \node[draw,minimum width=4cm](C) at (0,10){a};
         % on ajoute le node D avec son texte long
  \node[draw,minimum width=4cm](D) at (0,15){a};
         \node[draw,minimum width=4cm](E) at (0,20){a};
           \node[draw,text width=4cm](Q) at (0,30){a};
               \node[draw= blue,fill=blue!30,text width=4cm](G) at (16,25){Se};
                 \node[draw,text width=4cm](M) at (16,30){Add };
                               % \node[draw,minimum width=4cm](N) at (16,35){all nodes visited};
                                               \node[draw,minimum width=4cm](O) at (16,40){Return to the depot};
                                                \node[draw= blue,fill=blue!10,text width=4cm](H) at (32,25){a};
                                               \node[draw= blue,fill=blue!10,minimum width=4cm](I) at (32,30){a};
                                               \node[draw= blue,fill=blue!10,text width=4cm](L) at (32,40){a};

           \node[P](P) at (0,25){a};
           \node[R](R) at (0,35){Stopping criterion?};
           \node[N](N) at (16,35){a};

 % \foreach \x/\y/\k in {0/25/P, 0/35/R} {
 %   \diam{\x}{\y}{\k}
 % }
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \node[draw= blue,fill=blue!10,text width=4cm](J) at (35.1,35){a};
 \node[draw= blue,fill=blue!10,text width=1cm](K) at (27,35){a};

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%  \foreach \x/\y/\k in {35.1/35/J, 28.9/35/K} {
 %%%%%%    \prect{\x}{\y}{\k}
 %%%%%%  }
  \node[draw,minimum height=1cm,minimum width=4cm] (End) at (0,45) {End};
  \foreach \m/\n in {A/B, B/C, C/D, D/E, E/P, E/F, P/Q, Q/R, F/G, G/H, H/I, J/L, K/L, M/N} {
    \draw[thick,>=latex,->] (\m)--(\n);
  }
  \foreach \m/\n in {I/J, R/End, N/O} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{Yes};
  }
  \foreach \m/\n in {I/K} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{No};
  }
  \draw[red,thick,>=latex,->] (R.west) --++ (2,0) node[midway,above]{No} --++ (0,-30) -- (B);
  \draw[thick,>=latex,->] (N.east) --++ (-2,0) node[midway,above]{No} --++ (0,-10) -- (G.east);
  \draw[thick,>=latex,->] (O.south) -- (16,43) -- (8,43) -- (8,25) --(8,25)--(P.west);
   \draw[thick,>=latex,->] (L.south) -- (32,43) -- (25,43) -- (25,30) --(25,30)--(M.west);
\end{tikzpicture}

\end{frame}

答案1

  • 你缺少框架选项。如果没有它,你在定义中使用的fragile所有内容都会遇到麻烦#

  • 您可以使用类似scale=0.5,transform shape缩小图像的方法,但结果可能不可读。考虑更改流程图的布局,以更好地适应幻灯片的宽高比

  • \tikzstyle已弃用


\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
    
\begin{frame}[fragile]

\newcommand{\rect}[4]{\node[draw,minimum height=1cm,minimum width=4cm] (#3) at (#1,#2) {\begin{minipage}{3.8cm}\centering #4\end{minipage}};}
\newcommand{\prect}[3]{\node[draw,minimum height=1cm,minimum width=1.8cm] (#3) at (#1,#2) {$#3$};}
\newcommand{\diam}[4]{\node[draw,diamond,aspect=3,minimum width=4cm] (#3) at (#1,#2) {\smash{\begin{minipage}[1pt]{3.8cm}\centering #4\end{minipage}}};}


\begin{tikzpicture}[x=-.350cm,y=-.350cm,scale=0.5,transform shape]
%  \foreach \x/\y/\k in {0/0/A, 0/5/B, 0/10/C, %0/15/D, % on retire D de la liste
%    0/20/E, 0/30/Q, 16/20/F, 16/25/G, 16/30/M, 16/35/N, 16/40/O, 32/25/H, 32/30/I, 32/40/L} {
%    \rect{\x}{\y}{\k}
%  }
\tikzset{P/.style={diamond, aspect=3,thick, draw,minimum width=2.5cm}}
\tikzset{R/.style={diamond, aspect=2,thick, draw,minimum width=2.5cm}}
\tikzset{N/.style={diamond, aspect=3,thick, draw,minimum width=2.5cm}}

   \node[draw,minimum width=4cm](A) at (0,0){a};
     \node[draw,minimum width=4cm](B) at (0,5){Create a};
       \node[draw,minimum width=4cm](C) at (0,10){x};
         % on ajoute le node D avec son texte long
  \node[draw,minimum width=4cm](D) at (0,15){s};
         \node[draw,minimum width=4cm](E) at (0,20){s};
           \node[draw,text width=4cm](Q) at (0,30){s };  %$E(L_{k})$ of PTSPD for each and
             \node[draw,text width=4cm](F) at (16,20){s};
               \node[draw= blue,fill=blue!30,text width=4cm](G) at (16,25){s};
                 \node[draw,text width=4cm](M) at (16,30){A};
                               % \node[draw,minimum width=4cm](N) at (16,35){as};
                                               \node[draw,minimum width=4cm](O) at (16,40){Return };
                                                \node[draw= blue,fill=blue!10,text width=4cm](H) at (32,25){a};
                                               \node[draw= blue,fill=blue!10,minimum width=4cm](I) at (32,30){a};
                                               \node[draw= blue,fill=blue!10,text width=4cm](L) at (32,40){a};

           \node[P](P) at (0,25){a};
           \node[R](R) at (0,35){Stopping criterion?};
           \node[N](N) at (16,35){aa};

 % \foreach \x/\y/\k in {0/25/P, 0/35/R} {
 %   \diam{\x}{\y}{\k}
 % }
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \node[draw= blue,fill=blue!10,text width=4cm](J) at (35.1,35){a};
 \node[draw= blue,fill=blue!10,text width=1cm](K) at (27,35){a};

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%  \foreach \x/\y/\k in {35.1/35/J, 28.9/35/K} {
 %%%%%%    \prect{\x}{\y}{\k}
 %%%%%%  }
  \node[draw,minimum height=1cm,minimum width=4cm] (End) at (0,45) {End};
  \foreach \m/\n in {A/B, B/C, C/D, D/E, E/P, E/F, P/Q, Q/R, F/G, G/H, H/I, J/L, K/L, M/N} {
    \draw[thick,>=latex,->] (\m)--(\n);
  }
  \foreach \m/\n in {I/J, R/End, N/O} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{Yes};
  }
  \foreach \m/\n in {I/K} {
    \draw[thick,>=latex,->] (\m) -- (\n) node[midway,above,sloped]{No};
  }
  \draw[red,thick,>=latex,->] (R.west) --++ (2,0) node[midway,above]{No} --++ (0,-30) -- (B);
  \draw[thick,>=latex,->] (N.east) --++ (-2,0) node[midway,above]{No} --++ (0,-10) -- (G.east);
  \draw[thick,>=latex,->] (O.south) -- (16,43) -- (8,43) -- (8,25) --(8,25)--(P.west);
   \draw[thick,>=latex,->] (L.south) -- (32,43) -- (25,43) -- (25,30) --(25,30)--(M.west);
\end{tikzpicture}

\end{frame}

    
\end{document}

在此处输入图片描述

答案2

为了乐趣和欢乐,但有人可能喜欢......

  • 节点的新形状(决策节点的形状由@Qrrbrbirlbel 设计)
  • 为节点添加颜色,
  • 使用相对定位
  • 对于正交箭头使用ext.paths.ortho,
  • 主分支在链中
  • 无扩展
  • adjustwidth通过使用changepage包来扩大框架中的文本区域
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                ext.paths.ortho,  % -|- and |-| path operations
                positioning,
                shapes}
\usepackage{changepage}

\begin{document}
\begin{frame}[fragile]
    \begin{adjustwidth}{-2em}{-1em}
    \centering
\begin{tikzpicture}[auto,
   node distance = 2.8mm and 4mm,
     start chain = going below,
      arr/.style = {semithick,-{Stealth[scale=0.8]}},
     base/.style = {draw=#1, semithick, fill=#1!25,
                    text width=22mm, minimum height=5mm, align=center,
                    inner sep=3pt,
                    font=\tiny\linespread{0.84}\selectfont,
                    on chain
                    },
       be/.style = {% BeginEnd
                    base=red, text width=#1, rounded corners},
     be/.default = 22mm,
       if/.style = {% decision node: https://tex.stackexchange.com/questions/661461
                    base=teal, align=left,
                    label={[D=teal]north east:}},
            D/.style   = {diamond, draw=#1, fill=#1!50, inner sep=1mm, anchor=center},
            lbl/.style = {inner sep=2pt, font=\tiny, text=black!75},
       ra/.style = {label={[lbl, anchor=south west]east:#1}},       % right above
       rb/.style = {label={[lbl, anchor=north west]east:#1}},       % right bbelo
       la/.style = {label={[lbl, anchor=north east]west:#1}},       % lrft above
       lb/.style = {label={[lbl, anchor=south east]west:#1}},       % left below
       bl/.style = {label={[lbl, anchor=north west]south:#1}},      % below lrfz
       br/.style = {label={[lbl, anchor=north west]south:#1}},      % below right
       pc/.style = {% ProCess
                    base=#1},
     pc/.default = orange,
       px/.style args = {#1/#2}{% ProcessExtend
                    base=#1, text width=#2},
%
off chain/.code={\def\tikz@lib@on@chain{}}          % <== defined interruption of chain
                    ]
%%  branche principale, les nœuds sont en chaîne
    \begin{scope}[nodes={join=by arr}]
\node[be]   (A) {Initialisation of ACO parameters};
\node[pc]   (B) {Create $m$ ants};
\node[pc]   (C) {Place $m$ ants on the depot node};
\node[pc]   (D) {Ants start moving from depot};
\node[pc]   (E) {For each ant $k$ in colony:};
%
\node[if,
      br=Yes?]   (F) {Do this for all ants};
%
\node[pc]   (G) {Update pheromone level};
%
\node[if,
      br=Yes,
      rb=No]   (H) {Stopping criterion?};
%
\node[be=11mm, below=11mm of H]  (End)   {End};
    \end{scope}
    
%% près de la branche gauche
\node[off chain, pc,
      left=of E]    (I) {Mark all nodes as unvisited};
\node[pc=cyan]      (J) {Select next node using transition rule};

\node[pc]           (K) {Add selected node to route and mmark it as visited};
      \begin{scope}[nodes={join=by arr}]
%
\node[if,
      rb=No,
      br=Yes]       (L) {all nodes visited};
%
\node[pc]           (M) {Return to the depot};
    \end{scope}
 %% branche extrême gauche
      \begin{scope}[nodes={off chain}]
\node[pc=blue,
      left=0mm and 13mm of J]    
                    (N) {Generate a uniform random $P_{\mathrm{now}}\in[0,1]$, $P_{\mathrm{Levy}}\in[0,1]$};
%
\node[if=blue,
      la=Yes,
      rb=No,
      below=of N]   (O) {$P_{\mathrm{Levy}}\geq P_{\mathrm{threshold}}$};
%
\node[px=blue/30mm,
      below  left=of O.south]    
                    (P) {$P_{\mathrm{new}}=
                                       1-A \ast \frac{1-P_{\mathrm{levy}}}{1-P_{\mathrm{threshold}}\ast P_{\mathrm{now}}}$};
 \node[px=blue/15mm,
      below right=of O.south]   
                    (Q) {$P_{\mathrm{new}}=P_{now}$};
\node[pc=blue,
      below=of P.south -| O]  
                    (R) {The next node be selected using $P_{\mathrm{new}}$ from candidate liste};
    \end{scope}
    
%% flèches, non incluses dans la macro de "join"
 \foreach \m/\n in {I/J, J/N, N/O}
    \draw[arr] (\m) -- (\n);
 
\draw[arr] (O) -| (P);
\draw[arr] (O) -| ([xshift=4mm] Q.north);
\draw[arr] (P) |- (R);
\draw[arr] ([xshift=4mm] Q.south) |- (R);

\draw[arr] (R.south) -- ++ (0,-0.5) -|- [distance=23mm] (K);
\draw[arr] (L.east) -|- [distance=-3mm] (J);
\draw[arr] (H.east) -|- [distance=-5mm]  (B);

\draw[arr, red] (M.south) -- ++ (0,-0.5) node[below] {go to?};
\end{tikzpicture}
    \end{adjustwidth}
\end{frame}
\end{document}

在此处输入图片描述

附录:
我估计,如果将两个左分支合并为一个,并从主分支顶部附近开始,则可以获得更清晰的流程图。这样组织的流程图更加明显,互连箭头应该在哪里。但是,我不确定它们是否真的存在,如下所示。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, chains,
                ext.paths.ortho,  % -|- and |-| path operations
                positioning,
                shapes}
\usepackage{changepage}

\begin{document}
\begin{frame}[fragile]
    \begin{adjustwidth}{-2em}{-1em}
    \centering
\begin{tikzpicture}[auto,
   node distance = 2.8mm and 24mm,
     start chain = going below,
      arr/.style = {semithick,-{Stealth[scale=0.8]}},
     base/.style = {draw=#1, semithick, fill=#1!25,
                    text width=22mm, minimum height=4mm, align=center,
                    inner sep=3pt,
                    font=\tiny\linespread{0.84}\selectfont,
                    on chain
                    },
       be/.style = {% BeginEnd
                    base=red, text width=#1, rounded corners},
     be/.default = 22mm,
       if/.style = {% decision node: https://tex.stackexchange.com/questions/661461
                    base=teal, align=left,
                    label={[D=teal]north east:}},
            D/.style   = {diamond, draw=#1, fill=#1!50, inner sep=1mm, anchor=center},
            lbl/.style = {inner sep=2pt, font=\tiny, text=black!75},
       ra/.style = {label={[lbl, anchor=south west]east:#1}},       % right above
       rb/.style = {label={[lbl, anchor=north west]east:#1}},       % right bbelo
       la/.style = {label={[lbl, anchor=north east]west:#1}},       % lrft above
       lb/.style = {label={[lbl, anchor=south east]west:#1}},       % left below
       bl/.style = {label={[lbl, anchor=north west]south:#1}},      % below lrfz
       br/.style = {label={[lbl, anchor=north west]south:#1}},      % below right
       pc/.style = {% ProCess
                    base=#1},
     pc/.default = orange,
       px/.style args = {#1/#2}{% ProcessExtend
                    base=#1, text width=#2},
%
off chain/.code={\def\tikz@lib@on@chain{}}          % <== defined interruption of chain
                    ]
%%  branche principale, les nœuds sont en chaîne
    \begin{scope}[nodes={join=by arr}]
\node[be]   (A) {Initialisation of ACO parameters};
\node[pc]   (B) {Create $m$ ants};
\node[pc]   (C) {Place $m$ ants on the depot node};
\node[pc]   (D) {Ants start moving from depot};
\node[pc]   (E) {For each ant $k$ in colony:};
%
\node[if,
      br=Yes?,
      lb=No?]   (F) {Do this for all ants};
%
\node[pc]       (G) {Update pheromone level};
%
\node[if,
      br=Yes,
      rb=No]   (H) {Stopping criterion?};
\node[be=11mm, below=11mm of H]  (end)   {End};
    \end{scope}
    
%% près de la branche gauche
\node[off chain, pc,
      left=of $(A.south west)!0.5!(B.south west)$]    
                    (I) {Mark all nodes as unvisited};
      \begin{scope}[nodes={join=by arr}]
\node[pc=cyan]      (J) {Select next node using transition rule};
\node[pc=blue]      (K) {Generate a uniform random $P_{\mathrm{now}}\in[0,1]$, $P_{\mathrm{Levy}}\in[0,1]$};
%
\node[if=blue,
      la=Yes,
      rb=No]        (L)  {$P_{\mathrm{Levy}}\geq P_{\mathrm{threshold}}$};
%
    \end{scope}

      \begin{scope}[off chain]
\node[px=blue/40mm, 
      below  left=3mm and 8mm of L.south]    
                    (M) {$P_{\mathrm{new}}=
                         1-A \ast \frac{1-P_{\mathrm{levy}}}{1-P_{\mathrm{threshold}}\ast P_{\mathrm{now}}}$};
 \node[px=blue/15mm,
      below right=3mm and 8mm of L.south]   
                    (N) {$P_{\mathrm{new}}=P_{now}$};
\node[pc=blue,
      below=of M.south -| J]   
                    (O) {The next node be selected using $P_{\mathrm{new}}$ from candidate liste};
    \end{scope}
      
      \begin{scope}[nodes={join=by arr}]
\node[pc]           (P) {Add selected node to route and mmark it as visited};
%
\node[if,
      rb=No,
      br=Yes]       (Q) {All nodes visited?};
%
\node[pc]           (R) {Return to the depot};
    \end{scope}
 
%% branche extrême gauche
\draw[arr] (L) -| (M);
\draw[arr] (L) -| (N);
\draw[arr] (M) |- (O);
\draw[arr] (N) |- (O);

\draw[arr] (Q.east) -|- [distance=-17mm] (J);
\draw[arr] (F.west) -|- [distance=5mm] (I);
\draw[arr] (H.east) -|- [distance=-5mm]  (B);
\end{tikzpicture}
    \end{adjustwidth}
\end{frame}
\end{document}

在此处输入图片描述

相关内容