3

I am trying to add captions for figures inside a table but am unable to do so. I have looked into this solution but having flexible X columns is not working as described.

The current output looks something like this. enter image description here

The offending code.

\documentclass[margin=2in]{article}

\usepackage{tabularray}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}
    \begin{figure}[h]
        \begin{tblr}{X[2,r] X[-1,r]}
            \includegraphics[width=2in]{example-image-a}
            \captionof{figure}{Figure A}
            &
            \includegraphics[width=2in]{example-image-b}
        \end{tblr}
    \end{figure}
\end{document}

Error output:

You can't use `\hrule' here except with leaders. \end
Missing number, treated as zero. \end
Illegal unit of measure (pt inserted). \end
Missing \endgroup inserted. \end
Missing } inserted. \end
Use of \??? doesn't match its definition. \end
Extra \endgroup. \end
Too many }'s. \end

I have also tried with caption and caption* in place of captionof. Any help is appretiated.

2 Answers 2

3

You could put them in a \parbox:

\documentclass[margin=2in]{article}

\usepackage{tabularray}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}
    \begin{figure}[h]
        \begin{tblr}{X[2,r] X[-1,r]}
            \parbox[t]{\linewidth}{%
            \includegraphics[width=2in]{example-image-a}
            \captionof{figure}{Figure A}}
            &
            \includegraphics[width=2in]{example-image-b}
        \end{tblr}
    \end{figure}
\end{document}
3
  • It works but the caption for the image is out of alignment for me. Can you confirm if that is the case for you as well? Could you also explain why \parbox does in this context?
    – Rashiq
    Nov 8, 2021 at 14:52
  • Add \centering inside the \parbox. The caption is (almost) always centered. similar to \makebox[\linewidth]{...}. Nov 8, 2021 at 16:18
  • I understand. \centering centers the image. However, I wanted the caption to be under the image as I need l and r of the cells to function. Using \centering with l or r causes latex to ignore the cell alignment. Is there a way around the problem?
    – Rashiq
    Nov 9, 2021 at 9:07
5

You need to add two libraries: counter (for correct caption numbering), varwidth (for eliminate your error) and tblr option measure = vbox:

\documentclass{article}

\usepackage{graphicx}
\usepackage[skip=1ex]{caption}

\usepackage{tabularray}              % version 2021P
\UseTblrLibrary{counter,varwidth}    % <===

\begin{document}
    \begin{figure}[ht]
\begin{tblr}{colspec={Q[c, wd=2in]  Q[c, wd=2in]},
             measure = vbox}         % <===
\includegraphics[width=\linewidth]{example-image-a}
\caption{Figure A}
    &   \includegraphics[width=\linewidth]{example-image-b} 
\end{tblr}
    \end{figure}
\end{document}

enter image description here

Edit:

Note, for above solution you need to use tabularray package version 2021P, which support/contain libraries counter and varwidth and should include tblr option measure = vbox . Older versions (before 2021N) doesn't support/ enables all aforementioned.

If you have older version of package (it is still in fast development) I recommend you to upgrade it. Beside mentioned recent version has some bug fixes too and also improved documentation.

Instead of Q[c, wd=1in] (width is taken from width of your images), you can use X[c] or X[h]. In this case compilation will throw warnings.

7
  • This isn't working for me. I directly tried running the code in the answer instead of augmenting it to fit the MWE. I receive the following errors: Package caption Error: vskip undefined. \caption@ProcessOptions*{caption}; Failed to reprocess entire contents. Can you provide some insigts regarding the matter?
    – Rashiq
    Nov 8, 2021 at 15:13
  • @Rashiq, which version of tabularray package you have installed? I tested suggested solution with version 2021P (recent one) and it works fine. Try to upgrade package.
    – Zarko
    Nov 8, 2021 at 15:27
  • @Zarko it is an error from the caption package. The option name is skip not vskip. Beside this I get warnings from varwidth with your code. Nov 8, 2021 at 16:23
  • @UlrikeFischer, you are right. skip is corrected now. About warnings: They are just warning, result is correct and present. Hopefully the package author will see this answer and your comment and give some suggestion about them.
    – Zarko
    Nov 8, 2021 at 16:35
  • @UlrikeFischer, if I replace column type X[h] with X[h, wd=2in] or with Q[h,wd=2in], warnings disappear. Apparently automatic calculation of columns width beside that gives correct result also throw warnings (that column width calculation has some problems). This can be considered as (not harmful) bug ;-)
    – Zarko
    Nov 8, 2021 at 19:25

You must log in to answer this question.

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