6

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
8

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}

enter image description here


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}
\usepackage{adjustbox}
\newlength\mylength

\usepackage{lipsum}
\begin{document}

\lipsum

\begin{figure}
\adjustimage{height=5cm, gstore width=\mylength, center}{example-image}
%alternative: \adjustbox{gstore width=\mylength,center}{\includegraphics[height=10cm]{example-image}}
\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
  • 1
    @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
4

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} 

enter image description here enter image description here

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.