1

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}
\begin{adjustwidth}{2.25in}{0pt}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\begin{adjustwidth}{2.25in}{0pt}
\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{adjustwidth}
\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{adjustwidth}
 \end{document}
  • Maybe try with captionof from the caption package. – leandriis Jun 14 at 5:13
  • Unrelated to the question, but could you please explain why you need all the adjustwidth environments? – leandriis Jun 14 at 19:30
  • @leandriis the main reason is to put margin for reader on some special and customized pages – Aria Jun 15 at 15:04
2

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}
\begin{adjustwidth}{2.25in}{0pt}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\begin{adjustwidth}{2.25in}{0pt}
\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{adjustwidth}
\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{adjustwidth}
 \end{document}

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

2

Here is an alternative using the \captionof command:

enter image description here

\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}
\begin{adjustwidth}{2.25in}{0pt}
\chapter{first chapter}
\section{first section}
This is my first table:
\begin{table}[h!]
\begin{adjustwidth}{2.25in}{0pt}
\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{adjustwidth}
\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}

\end{adjustwidth}
 \end{document}

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.