# How to put a table with caption inside a "mdframed" theorem example style box

I have a book and want to put a table with caption inside a "mdframed" theorem example style box. Without begin/end table and without caption I can do it. But with caption I can not. Below is my code:

\documentclass[openany]{book}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{changepage}
\usepackage{geometry}
\geometry{paperheight=9.8125in,paperwidth=8in, left=.5in,
right=.5in,top=.75in,bottom=.4375in }
\renewcommand{\rmdefault}{pag}
\usepackage{mdframed}
\mdfdefinestyle{exampledefault}{linewidth=2pt}
\usepackage{amsthm}
\newtheorem{my_example}{EXAMPLE}[chapter]
\begin{document}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\begin{center}
\begin{tabular}{|c|c|c|}
\hline $x$ &  $y$   &  $z$ \\
\hline $a$&$b$ &$c$ \\
\hline
\end{tabular}
{\caption{my first table}} \label{table_first}
\end{center}
\end{table}

Now I have this text box:
\begin{mdframed}[style=exampledefault]
\begin{my_example}\label{example_first}
I want to have another table here:

\begin{center}
\begin{tabular}{|c|c|c|}
\hline $a$ &  $b$   &  $c$ \\
\hline $x$&$y$ &$z$ \\
\hline
\end{tabular}
\end{center}

\end{my_example}
\end{mdframed}

\end{document}

• Maybe try with captionof from the caption package. Jun 14, 2019 at 5:13
• Unrelated to the question, but could you please explain why you need all the adjustwidth environments? Jun 14, 2019 at 19:30
• @leandriis the main reason is to put margin for reader on some special and customized pages
– Aria
Jun 15, 2019 at 15:04

I think this is a perfect use case for the place here H from the float package (because it makes it easier to change back to a float again (than using captureof constructions) should that be needed):

\documentclass[openany]{book}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{changepage}
\usepackage{geometry}
\usepackage{caption}
\usepackage{float}
\geometry{paperheight=9.8125in,paperwidth=8in, left=.5in,
right=.5in,top=.75in,bottom=.4375in }
\renewcommand{\rmdefault}{pag}
\usepackage{mdframed}
\mdfdefinestyle{exampledefault}{linewidth=2pt}
\usepackage{amsthm}
\newtheorem{my_example}{EXAMPLE}[chapter]
\begin{document}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\begin{center}
\begin{tabular}{|c|c|c|}
\hline $x$ &  $y$   &  $z$ \\
\hline $a$&$b$ &$c$ \\
\hline
\end{tabular}
{\caption{my first table}} \label{table_first}
\end{center}
\end{table}

Now I have this text box:
\begin{mdframed}[style=exampledefault]
\begin{my_example}\label{example_first}
I want to have another table here:

\begin{table}[H]
\begin{center}
\begin{tabular}{|c|c|c|}
\hline $a$ &  $b$   &  $c$ \\
\hline $x$&$y$ &$z$ \\
\hline
\end{tabular}
{\caption{my second table}} \label{table_second}
\end{center}
\end{table}

\end{my_example}
\end{mdframed}

\end{document}


But I guess some people feel strongly against it... Why should the "H" option not be used in floats?

Here is an alternative using the \captionof command:

\documentclass[openany]{book}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{changepage}
\usepackage{geometry}
\geometry{paperheight=9.8125in,paperwidth=8in, left=.5in,
right=.5in,top=.75in,bottom=.4375in }
\renewcommand{\rmdefault}{pag}
\usepackage{mdframed}
\mdfdefinestyle{exampledefault}{linewidth=2pt}
\usepackage{amsthm}
\newtheorem{my_example}{EXAMPLE}[chapter]
\begin{document}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\centering
\begin{tabular}{|c|c|c|}
\hline $x$ &  $y$   &  $z$ \\
\hline $a$&$b$ &$c$ \\
\hline
\end{tabular}
\caption{my first table} \label{table_first}
\end{table}

Now I have this text box:
\begin{mdframed}[style=exampledefault]
\begin{my_example}\label{example_first}
I want to have another table here:

\begin{center}
\begin{tabular}{|c|c|c|}
\hline $a$ &  $b$   &  $c$ \\
\hline $x$&$y$ &$z$ \\
\hline
\end{tabular}
\end{center}
\captionof{table}{another caption}

\end{my_example}
\end{mdframed}