Table caption Width

We have table caption background image. We put this as image. But we need the table caption and images exactly same width as table body.

-
hmmm. a little more detail would help. you have a table, a caption and an image....what do you want to do with each? put the image as the caption? put the image as background to the caption? – Yossi Farjoun Dec 17 '10 at 11:01
Put the table first in a box then you its width and height. – Herbert Dec 17 '10 at 16:01

For customizing captions, the caption packages is highly recommendable. Besides a lot of other features, you can also adjust the width of captions, for example:

\captionsetup{width=6cm}


You may use this command globally in your preamble but also within a table environment, which keeps it local and thus limits the effect of this setting.

With a KOMA-Script class, without the caption package, you could use \setcapwidth:

\setcapwidth[c]{6cm}


Since you use a background image, you might know the necessary width. Even if you don't know it, the width for the caption could be automatically calculated from the width of the table or image. Then the command \settowidth comes handy.

• create a macro for the width: \newlength{\cwidth}

• get the width of the object: \settowidth{\cwidth}{object}

• set the caption widht by one of the two commands at the beginning of this answer

• print out the object

You could even write a macro for automatizing that, like

\newcommand{\autowidth}[1]{%
\settowidth{\cwidth}{#1}%
\captionsetup{width=\cwidth}%
#1%
}


This works well with images. Tables might be harder as object, but in your case you could refer to your background images.

-
Great approach (don't know why OP did not accept). However, getting the width of an object might be easy if your object is an imported file (png etc.). But when it comes to a pgfplot-generated object, how do you retrieve its dimensions? (Since some pgfplots needs a lot of code-lines it would be rather strange to insert the same code again into autowidth as its argument) – John Nov 9 '12 at 18:23
One further idea is, to use \captionsetup[table]{width=6cm} to just change the caption with for tableenvironments. – Ronny Dec 17 '12 at 8:42

Another option would be using the threeparttable or ctable packages, which will fit the width of the caption to the width of the table automatically.

-

in caption package guide:

Only fixed widths are supported here; if you are looking for a way to limit the width of the caption to the width of the figure or table, please take a look at the floatrow[8] or threeparttable[22] package.

-

Actually there ise more legal way to do this using captions package option width.

For global effect

\usepackage[width=.75\textwidth]{caption}


For local effect only in current environment:

\usepackage{caption}
\captionsetup{width=.75\textwidth}
`