11

Originally, the algorithm is too long to fit in the frame. If I use the \scalebox{0.8}{}, it can fit in the frame but doesn't look like to be centered in the frame. Here is the code and output. First picture is for the original code, second is for the code with scalebox. Are there any methods to make the the algorithm to both fit in and be centered in the frame. (And I think the hat of A in the last line is ugly.)

\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}

enter image description here

enter image description here

Yulong Ao
  • 723

2 Answers2

11

Simply invert minipage with \scalebox, i.e. embed the minipage inside \scalebox.

MWE:

\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} 

Output:

enter image description here

karlkoeller
  • 124,410
  • The answer is OK for me. But I have a question why the hat of A in your picture is looks better than mine. Is there something should I notice? – Yulong Ao Jan 04 '15 at 09:17
  • @CodingTheLife No idea. Do you get any warning? – karlkoeller Jan 04 '15 at 09:19
  • Yes. Here is the warning: || LaTeX Font Warning: Font shape `OT1/cmss/m/n' in size <4> not available ||LaTeX Font Warning: Size substitutions with differences. – Yulong Ao Jan 04 '15 at 09:41
  • @CodingTheLife No, that has nothing to do with it. BTW, to remove that warning add the line \let\Tiny\tiny in the preamble. Try upgrading your TeX distribution. – karlkoeller Jan 04 '15 at 09:55
  • @karlkoeller I use block inside scalebox, but that produces error. see this https://paste.ubuntu.com/p/kVSPBRm6j9/ expected output https://pasteboard.co/H8VivSp.png – alhelal Feb 23 '18 at 05:21
2

For content that contains text, choosing an appropriate font size is to be preferred over scaling the content, see Why not scale elements that contain text for some explanation.

\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}

However if you don't mind a sub-optimal result, you could use one of the most evil beamer options: 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}