[Tex/LaTex] algorithm2e and Beamer – How to make algorithms wider with smaller font

algorithm2ealgorithmsbeamerwidth

I would like to make the algorithm2e environments wider in a beamer document. Indeed I must you a \scalebox to see all of my algorithm. Is there a way to have wider algorithms with smaller size font than the standard text WITHOUT using scalebox ? The size in my M(merely not)WE looks good for me.

OUTPUT

enter image description here

M(merely not)WE

\documentclass{beamer}
    \usepackage[utf8]{inputenc}

    \usepackage[french]{babel}

    \usepackage[french, lined]{algorithm2e}

    \usepackage{beamerthemesplit}


\begin{document}

\frame{
    \frametitle{Premier algorithme}

\scalebox{0.65}{
\begin{algorithm}[H]
    \Begin{
        \ForEach{Case $C$ de la grille $G$}{
            \If{$| poss(C) | \geq 2$}{
                \ForEach{$p \in poss(C)$}{
                    $isole \leftarrow Vrai$
                    \\
                    \ForEach{Bloc $B$ contenant la case $C$}{
                        \ForEach{Case $C_B \neq C$ du bloc $B$}{
                            \If{$p \in poss(C_B)$}{
                                $isole \leftarrow Faux$
                                \\
                                Ne plus tester d'autres cases $C_B$.
                            }
                        }
                        \If{$isole = Vrai$}{
                            $g_{ij} \leftarrow \{ p \}$ où $(i,j) = coord(C)$
                            \\
                            $G \leftarrow \texttt{MettreAJour}(G)$
                            \\
                            Ne plus tester les blocs $B$ contenant $C$
                        }
                    }
                    \If{$isole = Vrai$}{
                        Ne plus tester les possibilités restantes de la case $C$
                    }
                }
            }
        }
        \Return $G$
    }
\end{algorithm}
}   
}

\end{document}

Best Answer

The problem here is obvious... :)

You're scaling down the regularly allowed \textwidth algorithm to 65% of its original size. Since this scaling is done after setting the algorithm, the traditional line breaks still remain, making it seem like the algorithm is "cut short" on the right hand side.

Here's an abbreviated version of your algorithm showing the traditional line break:

enter image description here

Here's an update, where the algorithm is first set in a minipage of width 1.53846\textwidth, which makes \scalebox{0.65} result in a box of width \textwidth (that is, 1.53846 x 0.65 ~ 1):

enter image description here

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[lined]{algorithm2e}
\usepackage{beamerthemesplit}
\begin{document}

\begin{frame}
  \frametitle{Premier algorithme}

  \scalebox{0.65}{%
  \begin{algorithm}[H]
    \Begin{
      \ForEach{Case $C$ de la grille $G$}{
        \If{$| poss(C) | \geq 2$}{
          \ForEach{$p \in poss(C)$}{
            $isole \leftarrow Vrai$
            \If{$isole = Vrai$}{
              Ne plus tester les possibilités restantes de la case $C$
            }
          }
        }
      }
      \Return $G$
    }
  \end{algorithm}}
\end{frame}   

\begin{frame}
  \frametitle{Premier algorithme}

  \scalebox{0.65}{\begin{minipage}{1.53846\textwidth}
  \begin{algorithm}[H]
    \Begin{
      \ForEach{Case $C$ de la grille $G$}{
        \If{$| poss(C) | \geq 2$}{
          \ForEach{$p \in poss(C)$}{
            $isole \leftarrow Vrai$
            \If{$isole = Vrai$}{
              Ne plus tester les possibilités restantes de la case $C$
            }
          }
        }
      }
      \Return $G$
    }
  \end{algorithm}%
  \end{minipage}}
\end{frame}   

\end{document}

If you wish to retain the natural look of the algorithm without using \scalebox, algorithm2e provides \SetAlFnt{<font>} where you can set the font size (or other font formatting options) for the entire algorithm. For example, \SetAlFnt{\footnotesize} will decrease the font size to \footnotesize.