Search code examples
latextext-alignment

Caption Centered To Image Rather Than Page


There isn't exactly any code here, since I just want to centre \begin{figure} and \end{figure}'s captions to the entre of the image included rather than to the centre of the page, if i centre the image to the left, the caption still appears to the centre of the page (without using minipage as that is the only way i know to do it right now).

Here's an example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\begin{document}
\begin{figure}[h]
    \includegraphics[width=0.5\textwidth]{image.png}
    \caption{Caption}
\end{figure}
\end{document}

What I get from compiling it:

What I get from compiling it.

I want this be centered to the image itself, and not the page, without using minipage.

Here is the source image btw, just some image I found on google


Solution

  • One possible approach using the varwidth package:

    \documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}
    \usepackage{varwidth}
    \begin{document}
    \begin{figure}[h]
    \begin{varwidth}{\linewidth}
        \includegraphics[width=0.5\textwidth]{example-image-duck}
        \caption{Caption}
    \end{varwidth}    
    \end{figure}
    \end{document}
    

    enter image description here