# How to set width of caption to width of figure?

How can I set the width of the caption to be the same as the width of the pic when I give a height of a picture and have the width deduced automatically by aspect ratio?

\begin{figure}
\includegraphics[height=10cm]{pic.png}
\caption{My very long caption ....}
\end{figure}


In this case the pic is on the left (let's assume only 12cm wide) and the caption is running over the complete text width.

• Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Mar 21 '18 at 13:51

You need to store the image in a savebox and then wrap the caption into a minipage with the width of the box.

\documentclass{article}
\usepackage{graphicx}
\newsavebox\mysavebox

\usepackage{lipsum} % for example text only
\begin{document}

\lipsum

\begin{figure}
\centering
\sbox\mysavebox{\includegraphics[height=5cm]{example-image}}%
\usebox\mysavebox
\par
\begin{minipage}{\wd\mysavebox}
\caption{My very long long long long long long long long long long long long long long long long long long long caption }
\end{minipage}
\end{figure}

\lipsum

\end{document}


The adjustbox package simplifies this approach and also avoids "bad box" warnings if the image is larger then the usual text width.

\documentclass{article}
\usepackage{graphicx}
\newlength\mylength

\usepackage{lipsum}
\begin{document}

\lipsum

\begin{figure}
\par% or empty line, needed to get caption below the image, not to the rigth
\adjustbox{minipage=\mylength,center}{\caption{My very long long long long long long long long long long long long long long long long long long long caption }}
\end{figure}

\lipsum

\end{document}

• Martin, out of curiosity: would this approach work for a tabular too? – gusbrs Mar 21 '18 at 13:56
• It should, as tabular creates a TeX box that can be put in a savebox. – AlexG Mar 21 '18 at 13:58
• That's very nice. I'd been looking for something like this. Thanks! – gusbrs Mar 21 '18 at 14:00
• @gusbrs: This works with all content. If there are multiple paragraphs the content needs to be wrapped in a minipage environment beforehand. With \begin{adjustbox}{height=10cm}\begin{tabular}...\end{tabular}\end{adjustbox} you could even scale the tabular or any other content. However, scaling text is discouraged as the font might not be good readable when scaled. Different font sizes use different letter designs for this. – Martin Scharrer Mar 21 '18 at 14:07
• This solution works perfectly. Great. – Robert M. Münch Apr 3 '18 at 8:37

There are two other solutions: with the measuredfigure environment, from threeparttable, and the \ffigbox command from floatrow; use asoptional argument \ffigbox[\FBwidth]{image}{caption}. One advantage is that if you want the caption to be only a little wider than the figure width, you just have to change[\FBwidth] to, say, [1.25\FBwidth]:

\documentclass{article}
\usepackage{graphicx}
\usepackage{threeparttable, floatrow}
\newsavebox\mysavebox

\usepackage{lipsum} %
\begin{document}
%
\lipsum[2]
\begin{center}
\begin{measuredfigure} \centering
\includegraphics[height=5cm]{example-image}
\caption{My very long long long long long long long long long long long long long long long long long long long caption }
\end{measuredfigure}
\end{center}

\lipsum[3]
\begin{figure} \centering
\ffigbox[1.25\FBwidth]%
{\includegraphics[height=5cm]{example-image}}
{ \caption{My very long long long long long long long long long long long long long long long long long long long caption }}
\end{figure}

\end{document}