5

I am wondering if it is possible, in a figure, to put a box around just the caption and not the whole figure.

\documentclass[12pt,letter]{article}    
\usepackage[demo]{graphicx}     

\begin{document}

\begin{figure}
\includegraphics[width=6in]{foo.ps}

\caption{I sure hope I can be boxed separate from the picture...}
%some command that can put a box around the caption
\end{figure}

\end{document}

EDIT I added the [width=6in] on the \includegraphics command. It is a necessary part of the figures I'm including.

8
  • \fbox{\parbox{.7\textwidth}{\caption{hello}}} Feb 27, 2014 at 23:29
  • @DavidCarlisle, the command puts a box (beautifully) around the caption but moves the text from where I want it to be. See my edit above. Feb 27, 2014 at 23:34
  • you can move the fbox eg put \centering in the figure to centre it again. Feb 27, 2014 at 23:38
  • And don't forget to use the optional argument of \caption.
    – user11232
    Feb 27, 2014 at 23:40
  • Do you want this to happen with all your figures or just one? Should the box be of fixed width, or adjust to the width of the caption?
    – Werner
    Feb 28, 2014 at 0:23

1 Answer 1

5

The following adjustment to \@makecaption places the figure caption inside a box that adjusts to the width of the caption:

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx

\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{\fbox{#1: #2}}%
  \ifdim \wd\@tempboxa >\hsize
    \fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{#1: #2}}\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}
\makeatother

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=100pt]{example-image}
  \caption{I sure hope I can be boxed separate from the picture\ldots}
  \caption{I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.}
\end{figure}

\end{document}

The definition of \@makecaption was taken (and changed) from article.cls.


caption also allows for setting a caption style. So, if you're interested in a fixed-width caption, you can use something similar to the following:

enter image description here

\documentclass{article}
\usepackage{caption,graphicx}% http://ctan.org/pkg/{caption,graphicx}

\DeclareCaptionFormat{plain}{%
  \fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{\centering #1#2#3}}}

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=100pt]{example-image}
  \caption{I sure hope I can be boxed separate from the picture\ldots}
  \caption{I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.}
\end{figure}

\end{document}

The style is set to \centering #1#2#3 where #1 refers to the caption label, #2 the caption label separator and #3 the caption text. See section 4 Own enhancements (p 24) of the caption package documentation.

8
  • That works great. However, both this answer and the answer shown in David Carlisle's comment on my question produce the following error when I run dvipdf: Error: PDF file is damaged - attempting to reconstruct xref table... Error: Couldn't find trailer dictionary Error: Couldn't read xref table . The resulting PDF is displayed just fine with Okular, but I don't know if this error is a problem I will need to worry about. Feb 28, 2014 at 0:57
  • @Joshua: I don't know what that might be the case. Could you provide a minimal working example (MWE) that replicates this problem? If so, you could include it in your original post, or post it on PastBin.
    – Werner
    Feb 28, 2014 at 0:59
  • @Joshua Does this \begin{figure}[ht] \centering \includegraphics[width=4in,height=1cm]{foo.ps} \setbox0=\vbox{\caption{I sure hope I can be boxed separate from the picture...}}\fbox{\box0} \end{figure} suffer the same problem? Feb 28, 2014 at 0:59
  • @StevenB.Segletes No it doesn't. However, I tried the answers given here with an example that is simpler than my full .tex file and there were no erros. I will make a MWE to find out what the true problem is. Feb 28, 2014 at 1:08
  • 1
    @Joshua: Why not use a latex > dvips > ps2pdf workflow?
    – Werner
    Feb 28, 2014 at 2:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .