0

I need to put an image inside table. I can't use figure inside table. Will not work even if I add minipage.

But can add \includegraphics OK inside table. But now I am not able to use \caption any more. It looks like \caption{} only works with figure.

The question is, how to add caption, but without using figure? Here is MWE.

First the version that does not work

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{longtable}
\begin{document}

\begin{longtable}{p{2in}}
    \begin{figure}    
       \includegraphics[width=0.3\textwidth]{example-image-a}
       \caption{my caption}
    \end{figure}
\end{longtable}
\end{document}

so removed figure. But like to use caption, or caption like thing

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{longtable}
\begin{document}

\begin{longtable}{p{2in}}
    %\begin{figure}    
       \includegraphics[width=0.3\textwidth]{example-image-a}
       \caption{my caption} %only when commenting this will it compile
    %\end{figure}
\end{longtable}
\end{document}

The idea is to get something similar to what is obtained when using figure but without using figure, since figure can't be used inside tables.

Is there something that has same semantics as \caption but does not need figure to use?

What is the best way to do this? Please note I only use lualatex to compile.

TL 2020.

3
  • @jsbibra it does not work. Not inside table. I just tried it. \documentclass[12pt]{article} \usepackage{longtable} \usepackage{subcaption} \usepackage{graphicx} \begin{document} \begin{longtable}{p{2in}} \noindent \begin{minipage}{\columnwidth} \makeatletter \def\@captype{figure} \makeatother \centering \includegraphics[width=0.3\textwidth]{example-image-a} \caption{Two figures}\label{Fig} \end{minipage} \end{longtable} \end{document}
    – Nasser
    May 23, 2020 at 2:05
  • 4
    This is not entirely trivial. You can use the caption package and use \captionof{figure}{...}. But even this requires some work, see tex.stackexchange.com/a/148646/194703.
    – user194703
    May 23, 2020 at 3:32

1 Answer 1

2

This redefines \caption locally (inside the \parbox). Every cell in a p column is a separate \parbox.

\caption@caption is the original name used by the caption package. This uses \csname to implement the @ symbol, which in turn requires \expandafters.

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{caption}

\begin{document}

\begin{longtable}{p{2in}}
   \expandafter\let\expandafter\caption\csname caption@caption\endcsname
   \centering
   \includegraphics[width=0.3\textwidth]{example-image-a}
   \captionof{figure}{my caption}
\end{longtable}
\end{document}

This creates a new name which doesn't include an @ symbol.

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{caption}

\let\normalcaption=\caption

\begin{document}

\begin{longtable}{p{2in}}
   \let\caption=\normalcaption
   \centering
   \includegraphics[width=0.3\textwidth]{example-image-a}
   \captionof{figure}{my caption}
\end{longtable}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .