49

I am having trouble making the caption beneath a figure to be of same width of the figure (less than the width of the paragraphs in the same page). How can I force the caption width to be the same as the figure width?

2

3 Answers 3

58

The caption package provides a width parameter than can be set for each figure individually. This way you can adjust the width to suit the width of your figure:

enter image description here

\documentclass{article}

\usepackage[margin=1in]{geometry}% Just for this example
\usepackage{lipsum}% Just for this example
\usepackage{graphicx,caption}

\begin{document}

\begin{figure}[t]
  \centering
  \captionsetup{width=.8\linewidth}
  \includegraphics[width=.8\linewidth, height=3\baselineskip]{example-image-a}
  \caption[First figure]{\lipsum*[2]}
\end{figure}

\lipsum[1]

\begin{figure}[t]
  \centering
  \captionsetup{width=.9\linewidth}
  \includegraphics[width=.9\linewidth, height=3\baselineskip]{example-image-b}
  \caption[Second figure]{\lipsum*[2]}
\end{figure}

\end{document}

If all your figures have the same width, you can set the option \captionsetup{width=<len>} globally.

2
  • 2
    Thanks werner for the quick answer.! LaTeX Error: Option clash for package geometry. I get this error when I use the package {lipsum}, do you have any idea?
    – Naema
    Sep 19, 2014 at 15:33
  • 2
    @Naema: Both geometry and lipsum were just for this example. The latter is really not necessary in your document as it just creates dummy text, Lorem Ipsum-style. The former you might use, but then there should be no problem.
    – Werner
    Sep 19, 2014 at 15:51
14

Instead of using a predetermined width, one can measure the width using a savebox and put the caption inside a minipage.

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}

\begin{document}
\begin{figure}
\sbox0{\includegraphics{example-image}}% measure width
\centering
\begin{minipage}{\wd0}
  \usebox0
  \caption{\blindtext}
\end{minipage}
\end{figure}

\end{document}

demo

10

Probably the simplest solution is to put the figure in a measuredfigure environment, from the threeparttable package. Note however this environment is fragile and might require being protected:

\documentclass[12pt, a4paper, twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{heuristica}
\usepackage[textwidth =15cm]{geometry}

\usepackage{graphicx}
\usepackage{caption, threeparttable}
\captionsetup{labelfont = sc, textfont = it}

\begin{document}
\begin{figure}
\centering\captionsetup{format = hang}
\begin{measuredfigure}
\includegraphics{Piero_di_Cosimo_3}
\caption{Portrait of Simonetta Vespucci\label{sen}}
 \end{measuredfigure}
 \end{figure}

\end{document}

enter image description here

Another, more powerful, possibility, consists in using the floatrow environment and setting its optional width argument as \FBwidth (= float box width):

\documentclass [11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ebgaramond}
\usepackage{graphicx, caption} %
\usepackage{floatrow}

\usepackage{lipsum}
\captionsetup{font = small, labelsep = period}

\begin{document}

\lipsum[11]
\begin{figure}[!htb] % <-- see difference between your code and this MWE
\centering
\begin{floatrow}[1]
\ffigbox[\FBwidth]{\caption{Max Ernst: \emph{Two Children are treatened by a Nightingale} (1924)} \label{max_ernst}}%
{\includegraphics[scale = 0.75]{ernst-nightingale}}
\end{floatrow}
\end{figure}
\lipsum[14]

\end{document} 

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.