[Tex/LaTex] Right align a caption with an image

captionshorizontal alignment

So, I am doing an outline where, as you go down the subdivisions, the left margin pushes progressively to the right, yet all the text stays flush on the right hand side. For aesthetic reasons, I keep images associated with text as the same width as the text.

The problem is, when I add a caption, the caption takes up the entire width of the page (not the width of the accompanying text). I have managed a work around, by not using the caption command, but, it is really inelegant. There must be a way to accomplish what I want while using the caption command.

enter image description here

Best Answer

Load the threeparttable, ragged2e and caption packages and enclose your figure in a measuredfigure environment, then use this code:

\begin{figure}
\centering\captionsetup{justification=RaggedLeft}
\begin{measuredfigure}
\caption{Some caption}
................
\end{measuredfigure}
\end{figure}