4

I am simply trying to put a frame around equations in my beamer slides.

For this purpose, I use the package tcolorbox.

The MWE below produces boxes where equations are almost aligned at the base of the box. I would want them to be centered vertically.

However, using "valign=center" has no effect.

(I already have a solution for the horizontal problem in the second equation and just didn't include it for MWE reasons)

\documentclass{beamer}
\usepackage{tcolorbox}
\newtcolorbox{equationframe}{
 valign=center,
}
\begin{document}
\begin{frame}
\begin{itemize}
\item[]
\begin{equationframe}
\begin{equation*}
p(x,y)=\begin{cases}
\min\{q(x,y),\frac{\pi({y})q(y,{x})}{\pi(x)}\}, \quad x\neq y\\
1-\int_{x\neq y} p(x,y)
\end{cases}
\end{equation*}
\end{equationframe}
\begin{equationframe}
\begin{equation*}
\pi(x)p(x,y)=\min\{\pi(x)q(x,y),\pi(y)q(y,x)\}=\pi(y)p(y,x)%
\end{equation*}
\end{equationframe}
\end{itemize}
\end{frame}
\end{document}

EDIT:

If I use the preamble

\documentclass{beamer}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\newtcolorbox{equationframe}{
 ams nodisplayskip
}

then I get

Missing $ inserted. \end{frame}

This is not problematic, since I realized using ams align* instead of ams nodisplayskip does the vertical alignment as I want, and my initial MWE compiles well. There is only one problem I with something I didn't include in the MWE: To fit horizontally overlong equations, I used the macro

\newcommand\fiteq[1]{%
  \sbox{\mybox}{$\displaystyle#1$}%
  \ifdim\wd\mybox>.85\textwidth\resizebox{.85\textwidth}{!}{\usebox{\mybox}}%
  \else\usebox{\mybox}\fi%
}
\newsavebox{\mybox}

and then

\begin{equationframe}
\fiteq{A=......=B}
\end{equationframe}

Unfortunately, the align commands \\ and & do not work within this macro.

I just want a box that nicely contains equations. In normal latex a horizontally to long equation is not so problematic, but when there is a box around equations, and the equation runs through the boundary, it looks really stupid.

There are two options: (1) adjust the size of the box or (2) adjust the size of the equation.

The macro \fiteq{} does (2), and I really like the way it looks. It would be nice if someone could adapt the macro to work with align commands (it does work with the above solution, as long as no newlines or ampersands are used). If that is not possible, a solution that goes in the direction of (1) would be welcome too

Equations at bottom of the box

  • \tcbuselibrary{theorems}, and then use the ams nodisplayskip key gets you close. – Torbjørn T. Mar 23 '16 at 7:16
  • @TorbjørnT. when I use "ams nodisplayskip" in the definition of my tcolorbox, I get an error "Missing $ inserted". the only thing that works is "ams nodisplayskip lower", but that doesn't fix the alignment problem – Bananach Mar 23 '16 at 10:27
  • If I take your example, add \tcbuselibrary{theorems} and replace valign=center with ams nodisplayskip, it works fine here. – Torbjørn T. Mar 23 '16 at 13:39
  • @TorbjørnT. see my edit. Maybe I am missing something that is really obvious to more experienced users? Anyway, using ams align* solved the vertical alignment problem. But now, when I actually try to use the benefits the align* environment provides, I run into different problems – Bananach Mar 24 '16 at 7:49
  • just found out that "ams nodisplayskip, ams align*" works, whereas "ams align*, ams nodisplayskip" doesn't – Bananach Mar 24 '16 at 7:51
6

Load theorems library (I do it with class option most) and use ams or any other maths options.

ams set upper and lower parts to mathematical mode with \displaystyle. You don't need equation environments is these boxes. And you don't need valign.

\documentclass{beamer}
\usepackage[most]{tcolorbox}
\newtcolorbox{equationframe}{
math
}
\begin{document}
\begin{frame}
\begin{itemize}
\item[]
\begin{equationframe}
p(x,y)=\begin{cases}
\min\{q(x,y),\frac{\pi({y})q(y,{x})}{\pi(x)}\}, \quad x\neq y\\
1-\int_{x\neq y} p(x,y)
\end{cases}
\end{equationframe}
\begin{equationframe}
\pi(x)p(x,y)=\min\{\pi(x)q(x,y),\pi(y)q(y,x)\}=\pi(y)p(y,x)%
\end{equationframe}
\end{itemize}
\end{frame}
\end{document}

enter image description here

Update: ams equation*, ams align*:

Instead of math, tcolorbox also offers some other mathematical boxes: ams equation, ams align, ams gather, ...

\documentclass{beamer}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{frame}
\begin{tcolorbox}[ams equation*]
p(x,y)=\begin{cases}
\min\{q(x,y),\frac{\pi({y})q(y,{x})}{\pi(x)}\}, \quad x\neq y\\
1-\int_{x\neq y} p(x,y)
\end{cases}
\end{tcolorbox}

\begin{tcolorbox}[ams nodisplayskip, ams align*]
\pi(x)p(x,y) & =\min\{\pi(x)q(x,y),\pi(y)q(y,x)\}\\
& =\pi(y)p(y,x)%
\end{tcolorbox}
\end{frame}
\end{document}

enter image description here

  • Thanks. Now how do I get the equations centered horizontally within the box? – Bananach Mar 23 '16 at 10:06
  • solved it with "halign=flush center" and for title "fonttitle=\centering". might be better ways though – Bananach Mar 23 '16 at 10:10
  • Problem: \begin{split} does not work with your solution – Bananach Mar 23 '16 at 10:14
  • when I use "ams equation*" instead of "math", then I am back at the original problem. using "ams equation, ams nodisplayskip" gives a compile error – Bananach Mar 23 '16 at 10:26
  • the vertical alignment is okay when I use "ams align*" but then for some reason using newlines "\\" and alignment characters "&" does not work – Bananach Mar 23 '16 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.