如何缩放算法以适合一个框架并同时位于框架的中心?

如何缩放算法以适合一个框架并同时位于框架的中心?

最初,算法太长,无法放入框架中。如果我使用\scalebox{0.8}{},它可以放入框架中,但看起来不像在框架中居中。这是代码和输出。第一张图片是原始代码,第二张图片是带有比例框的代码。有什么方法可以使算法既融入又居中在框架中。(我认为A的帽子最后一行很难看。)

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\usepackage{algorithm2e}

\begin{document}
\begin{frame}{Hello world}
  \begin{center}
    \begin{minipage}{0.8\linewidth}
    %\scalebox{0.8}{ 
      \begin{algorithm}[H]
    \DontPrintSemicolon
    \KwSty{type} brow : \KwSty{int}$[M+1]$\;
    \KwSty{type} bcol : \KwSty{int}$[N+1]$\;
    \KwSty{type} val : \KwSty{real}$[k]$\;
    \KwSty{type} val\_ptr : \KwSty{int}$[K+1]$\;
    \KwSty{type} ind : \KwSty{int}$[K]$\;
    \KwSty{type} ptr : \KwSty{int}$[M+1]$\;
    \everypar={\nl}
    \ForEach{block row $I$}{
      $i_0 \leftarrow brow[I]$\;
      $r \leftarrow brow[I+1]$\;
      Let $\hat{y} \leftarrow y_{i_0:(i_0+r-1)}$\;
      \For{$b=ptr[I] \KwTo ptr[I+1]$}{
        $J \leftarrow ind[b]$\;
        $j_0 \leftarrow bcol[J]$\;
        $c \leftarrow bcol[J+1]-bcol[J]$\;
        Let $\hat{x} \leftarrow x_{j_0:(j_0+c-1)}$\;
        Let $\hat{A} \leftarrow a_{i_0:(i_0+r-1),j_0:(j_0+c-1)}$\;
        Perform $r \times c$ block multiply, $\hat{y} \leftarrow \hat{y}+\hat{A} \cdot \hat{x}$\;
      }
    }
      \end{algorithm}
      %}
    \end{minipage}
  \end{center}

\end{frame}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

只需将minipage其反转\scalebox,即嵌入minipage内部\scalebox

梅威瑟:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\usepackage{algorithm2e}

\begin{document}
\begin{frame}{Hello world}
  \begin{center}
    \scalebox{0.75}{
    \begin{minipage}{0.7\linewidth}
      \begin{algorithm}[H]
    \DontPrintSemicolon
    \KwSty{type} brow : \KwSty{int}$[M+1]$\;
    \KwSty{type} bcol : \KwSty{int}$[N+1]$\;
    \KwSty{type} val : \KwSty{real}$[k]$\;
    \KwSty{type} val\_ptr : \KwSty{int}$[K+1]$\;
    \KwSty{type} ind : \KwSty{int}$[K]$\;
    \KwSty{type} ptr : \KwSty{int}$[M+1]$\;
    \everypar={\nl}
    \ForEach{block row $I$}{
      $i_0 \leftarrow brow[I]$\;
      $r \leftarrow brow[I+1]$\;
      Let $\hat{y} \leftarrow y_{i_0:(i_0+r-1)}$\;
      \For{$b=ptr[I] \KwTo ptr[I+1]$}{
        $J \leftarrow ind[b]$\;
        $j_0 \leftarrow bcol[J]$\;
        $c \leftarrow bcol[J+1]-bcol[J]$\;
        Let $\hat{x} \leftarrow x_{j_0:(j_0+c-1)}$\;
        Let $\hat{A} \leftarrow a_{i_0:(i_0+r-1),j_0:(j_0+c-1)}$\;
        Perform $r \times c$ block multiply, $\hat{y} \leftarrow \hat{y}+\hat{A} \cdot \hat{x}$\;
      }
    }
      \end{algorithm}
    \end{minipage}%
    }
  \end{center}

\end{frame}
\end{document} 

输出:

在此处输入图片描述

答案2

对于包含文本的内容,选择合适的字体大小比缩放内容更好,请参阅为什么不缩放包含文本的元素寻求一些解释。

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\usepackage{algorithm2e}

\begin{document}


\begin{frame}{Hello world}
  \begin{center}
    \begin{minipage}{0.8\linewidth}
        \scriptsize
      \begin{algorithm}[H]
    \DontPrintSemicolon
    \KwSty{type} brow : \KwSty{int}$[M+1]$\;
    \KwSty{type} bcol : \KwSty{int}$[N+1]$\;
    \KwSty{type} val : \KwSty{real}$[k]$\;
    \KwSty{type} val\_ptr : \KwSty{int}$[K+1]$\;
    \KwSty{type} ind : \KwSty{int}$[K]$\;
    \KwSty{type} ptr : \KwSty{int}$[M+1]$\;
    \everypar={\nl}
    \ForEach{block row $I$}{
      $i_0 \leftarrow brow[I]$\;
      $r \leftarrow brow[I+1]$\;
      Let $\hat{y} \leftarrow y_{i_0:(i_0+r-1)}$\;
      \For{$b=ptr[I] \KwTo ptr[I+1]$}{
        $J \leftarrow ind[b]$\;
        $j_0 \leftarrow bcol[J]$\;
        $c \leftarrow bcol[J+1]-bcol[J]$\;
        Let $\hat{x} \leftarrow x_{j_0:(j_0+c-1)}$\;
        Let $\hat{A} \leftarrow a_{i_0:(i_0+r-1),j_0:(j_0+c-1)}$\;
        Perform $r \times c$ block multiply, $\hat{y} \leftarrow \hat{y}+\hat{A} \cdot \hat{x}$\;
      }
    }
      \end{algorithm}
    \end{minipage}
  \end{center}
\end{frame}

\end{document}

但是如果你不介意次优结果的话,你可以使用最邪恶的投影仪选项之一shrink

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\usepackage{algorithm2e}

\begin{document}

% This solution is evil!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
% use it at your own risk!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\begin{frame}[shrink]{Hello world}
  \begin{center}
    \begin{minipage}{0.8\linewidth}
      \begin{algorithm}[H]
    \DontPrintSemicolon
    \KwSty{type} brow : \KwSty{int}$[M+1]$\;
    \KwSty{type} bcol : \KwSty{int}$[N+1]$\;
    \KwSty{type} val : \KwSty{real}$[k]$\;
    \KwSty{type} val\_ptr : \KwSty{int}$[K+1]$\;
    \KwSty{type} ind : \KwSty{int}$[K]$\;
    \KwSty{type} ptr : \KwSty{int}$[M+1]$\;
    \everypar={\nl}
    \ForEach{block row $I$}{
      $i_0 \leftarrow brow[I]$\;
      $r \leftarrow brow[I+1]$\;
      Let $\hat{y} \leftarrow y_{i_0:(i_0+r-1)}$\;
      \For{$b=ptr[I] \KwTo ptr[I+1]$}{
        $J \leftarrow ind[b]$\;
        $j_0 \leftarrow bcol[J]$\;
        $c \leftarrow bcol[J+1]-bcol[J]$\;
        Let $\hat{x} \leftarrow x_{j_0:(j_0+c-1)}$\;
        Let $\hat{A} \leftarrow a_{i_0:(i_0+r-1),j_0:(j_0+c-1)}$\;
        Perform $r \times c$ block multiply, $\hat{y} \leftarrow \hat{y}+\hat{A} \cdot \hat{x}$\;
      }
    }
      \end{algorithm}
    \end{minipage}
  \end{center}
\end{frame}



\end{document}

相关内容