17

I am trying to have two lines in inside \fbox but is not working. Below is my code

\documentclass{beamer}
\usepackage{vietnam}
\begin{document}
\begin{frame}
\frametitle{Exhaustive Search}
{\centering\fbox{ One billion searches per second $\Rightarrow 36 yrs \newline All this to recover only $10$ bits !}}
\end{frame}
\end{document}
Marijn
  • 37,699
NAASI
  • 2,809

2 Answers2

24

A \fbox doesn't admit line breaks. You have, however, several posiibilities:

Use a \parbox (or a minipage) inside the \fbox:

\documentclass{beamer}
\usepackage{vietnam}
\begin{document}
\begin{frame}
\frametitle{Exhaustive Search}
\fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\centering One billion searches per second $\Rightarrow$ 36 yrs  \\ All this to recover only $10$ bits !}}%
\end{frame}
\end{document}

enter image description here

Inside the \fbox, Use a varwidth environment from the varwidth package if you want the width to adjust automatically:

\documentclass{beamer}
\usepackage{vietnam}
\usepackage{varwidth}

\begin{document}
\begin{frame}
\centering
\fbox{\begin{varwidth}{\textwidth}
\centering
One billion searches per second $\Rightarrow$ 36 yrs  \\ All this to recover only $10$ bits !
\end{varwidth}}
\end{frame}
\end{document}

enter image description here

Or use a box defined using tcolorbox:

\documentclass{beamer}
\usepackage{vietnam}
\usepackage[most]{tcolorbox}
\tcbuselibrary{fitting}

\begin{document}
\begin{frame}
\frametitle{Exhaustive Search}
\begin{tcolorbox}[minipage,colback=white,arc=0pt,outer arc=0pt]
\centering
One billion searches per second $\Rightarrow$ 36 yrs \\ All this to recover only $10$ bits !
\end{tcolorbox}
\end{frame}
\end{document}

enter image description here

Or a TikZ \node with the align key:

\documentclass{beamer}
\usepackage{vietnam}
\usepackage{tikz}

\begin{document}
\begin{frame}
\frametitle{Exhaustive Search}
\centering
\tikz
  \node[draw,align=center]
  {One billion searches per second $\Rightarrow$ 36 yrs \\ All this to recover only $10$ bits !};
\end{frame}
\end{document}

enter image description here

Or...

Gonzalo Medina
  • 505,128
  • All your solutions are great. However, I am observing something strange in my document. The first time I use any of your solution, it works perfectly. But next time the box, itself, does not appear to be in the middle of slide. In fact it is shifted to the left side of slide. I have added my screen shot in original question – NAASI Aug 07 '15 at 15:16
  • @NAASI your description is not enough. We need to see the actual code you are using to diagnose the problem and find a solution. Please consider opening a fresh new question. – Gonzalo Medina Aug 07 '15 at 15:18
  • Its the first \centering command that is not working properly – NAASI Aug 07 '15 at 15:22
  • @NAASI Code, please. Without it we can only guess. – Gonzalo Medina Aug 07 '15 at 15:26
  • my code is visible in the snap shot that i have added. Its same as yours in solution 2. As per your suggestion, I have posted a new question http://tex.stackexchange.com/questions/259273/centering-not-working-properly – NAASI Aug 07 '15 at 15:27
  • @NAASI Ah, I hadn't seen your new question. Sorry. However, snapshots are almost useless; please provide the actual code in the form of a MWE. – Gonzalo Medina Aug 07 '15 at 15:29
  • How can I make a heading above the box in the 2nd example? maybe with colored background. – xotix Mar 04 '18 at 13:11
4

If you're going to insert manual line-breaks, you might as well construct a tabular:

enter image description here

\documentclass{beamer}
\begin{document}
\begin{frame}
  \frametitle{Exhaustive Search}
  \centering
  \fbox{\begin{tabular}{@{}c@{}}
    One billion searches per second $\Rightarrow 36$ yrs \\
    All this to recover only $10$ bits !
  \end{tabular}}
\end{frame}
\end{document}

...or using a pure tabular approach:

enter image description here

\documentclass{beamer}
\begin{document}
\begin{frame}
  \frametitle{Exhaustive Search}
  \centering
  \begin{tabular}{|c|}
    \hline
    One billion searches per second $\Rightarrow 36$ yrs \\
    All this to recover only $10$ bits ! \\
    \hline
  \end{tabular}
\end{frame}
\end{document}
Werner
  • 603,163