58

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?

Werner
  • 603,163
Naema
  • 2,305
  • 2
    The magic word is MWE: Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Sep 19 '14 at 15:13
  • 2
    The boxhandler package will automatically make captions the same width as the figure/table, unless overridden (no width need be specified). However, it requires you to enter figures/tables in macro, rather than environment form. Examples: http://tex.stackexchange.com/questions/101212/caption-format-changing/101217#101217 and/or http://tex.stackexchange.com/questions/110393/too-wide-figure-caption/110453#110453 – Steven B. Segletes Sep 19 '14 at 15:34

3 Answers3

69

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.

Werner
  • 603,163
  • 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 '14 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 '14 at 15:51
17

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

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
13

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

Bernard
  • 271,350