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.
Best Answer
You need to store the image in a savebox and then wrap the caption into a minipage with the width of the box.
The
adjustbox
package simplifies this approach and also avoids "bad box" warnings if the image is larger then the usual text width.