How to use figure and table inside a minipage in LaTeX?
If you use the figure or table environment inside a minipage, the compiler will show some error.
! LaTeX Error: Not in outer par mode. ! Undefined control sequence.
Because the figure
and table
are floating environments so, you can’t use them inside a non-floating environment like minipage
(except document
).
In this tutorial, I will show you how to use images and tables inside a minipage
environment in LaTeX.
Use figure and table inside a minipage
Since the figure
and table
are floating environments, you can use these outside minipage
, and inside the minipage
use the \includegraphics
or tabular
environment.
\documentclass{article} \usepackage{graphicx} \begin{document} \begin{figure}[h] \centering \begin{minipage}{0.45\linewidth} \centering \includegraphics[width=\linewidth]{CodeSpeedy-Logo.png} \caption{Caption} \label{fig:enter-label} \end{minipage} \end{figure} \begin{table}[h] \centering \begin{minipage}{0.45\linewidth} \centering \begin{tabular}{|c|c|} \hline A & B \\ \hline C & D \\ \hline \end{tabular} \caption{Caption} \label{tab:my_label} \end{minipage} \end{table} \end{document}
Output:
Figure and table side by side with minipage
If you want to use these environments inside a minipage to place image and table side by side. In this case, you have to use the float
package and [H]
option with table
and figure
.
\documentclass{article} \usepackage{float} \usepackage{graphicx} \begin{document} \begin{minipage}{\linewidth} \centering \begin{minipage}{0.45\linewidth} \begin{figure}[H] \centering \includegraphics[width=\linewidth]{CodeSpeedy-Logo.png} \caption{Caption} \label{fig:enter-label} \end{figure} \end{minipage} \hspace{0.05\linewidth} \begin{minipage}{0.45\linewidth} \begin{table}[H] \centering \begin{tabular}{|c|c|} \hline A & B \\ \hline C & D \\ \hline \end{tabular} \caption{Caption} \label{tab:my_label} \end{table} \end{minipage} \end{minipage}
Output:
Leave a Reply