# How to make a caption (via \captionof and extra margins) adhere to minipage margins?

I made use of the caption-package to define a custom layout of the captions. Their width is set to 0.8\columnwidth. As far as I understood the problem, this measure is not used when the caption is set in a minipage environment.

I'd like the captions be treated accordingly to their defined width when they are in a minipage evironment. How can I achieve that?

The catch is, it works without the extra definitions by the caption-package, but then the caption is just the vanilla ragged left style which I don't like that much.

Picture of the Status Quo

MWE

\documentclass[
11pt
]{scrartcl}

\usepackage{
tikz,
relsize,
amsmath,
booktabs,
tikz
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage[
labelfont=sf,
%labelfont=sc, %Kapitälchen, passt nicht wg. nicht-osf Ziffern
%%%%labelfont=it, %italics,
%%%labelfont=sl, %slanted,
hypcap=false,
format=hang,
%margin={2cm,2cm}
width=0.8\columnwidth
]{caption}

\usetikzlibrary{
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
>=latex
}

\begin{document}

\begin{figure}[btc]
%\begin{center}
\begin{minipage}[c]{0.48\columnwidth}
\begin{center}
\captionof{table}{Dataset for Something  with an Arbitrary Caption to Highlight the Problem}
\begin{tabular}{lrrrrr}
\toprule
Store & A & B & C & D & E \\
\cmidrule(r{0.125em}){1-1}%
\cmidrule(lr{0.125em}){2-2}%
\cmidrule(lr{0.125em}){3-3}%
\cmidrule(lr{0.125em}){4-4}%
\cmidrule(lr{0.125em}){5-5}%
\cmidrule(l{0.25em}){6-6}%
Input & 0.75 & 1 & 2 & 5 & 5\\
Output & 0.5 & 2 & 4 & 2 & 5\\
\bottomrule
\end{tabular}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[c]{0.48\columnwidth}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6,6);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[midway,below=8pt]{Output}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[above=5pt]{Input}; %Y-Achse
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x} coordinate (x axis);
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y} coordinate (y axis);
%
\draw
(0,0) coordinate (orig)
(0.75,0.5) coordinate (A)
(1,2) coordinate (B)
(2,4) coordinate (C)
(5,2) coordinate (D)
(5,5) coordinate (E)
;
%
\foreach \pt/\labpos in {A/right,D/below left,E/below}{
\fill (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
\foreach \pt/\labpos in {B/above left,C/above left}{
\fill (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
};
\end{tikzpicture}
\captionof{figure}{Graphical Illustration of Dataset with an Arbitrary Caption to Highlight the Problem}
\end{center}
\end{minipage}
\end{figure}

\end{document}

-
I wouldn't use \columnwidth for any of this but rather \textwidth. As for the caption with \linewidth seems more appropriate as minipage will set this. But it does not seem to work. Looks like width=0.8\linewidth just takes the current value at that time to be the with, it does not readjust it from time to time. A simple experiment confirms this to be true. –  daleif Aug 16 '13 at 8:50
Exactly my thoughts. They don't get readjusted, but another nested minipage or another environment would work. –  henry Aug 16 '13 at 8:56

Update third solution added using undocumented option calcwidth of caption mentioned in comment by the package author.

Two solutions

# First solution

If you need to keep you current width setting for captions in the rest of the text then you can issue a new \captionsetup command just before each \captionof command. Note that this is not as bad is it sounds as you can say

\captionsetup{width=0.8\linewidth}


in each of your cases. The point is that the value of the line width its determined by the current minipage so this gives you 0.8 of the width of the current minipage environment:

\documentclass[
11pt
]{scrartcl}

\usepackage{
tikz,
relsize,
amsmath,
booktabs,
tikz
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage[
labelfont=sf,
hypcap=false,
format=hang,
width=0.8\columnwidth
]{caption}

\usetikzlibrary{
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
>=latex
}

\begin{document}

\begin{figure}[btc]
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
\captionsetup{width=0.8\linewidth}
\captionof{table}{Dataset for Something  with an Arbitrary Caption to
Highlight the Problem}
\begin{tabular}{lrrrrr}
\toprule
Store & A & B & C & D & E \\
\cmidrule(r{0.125em}){1-1}%
\cmidrule(lr{0.125em}){2-2}%
\cmidrule(lr{0.125em}){3-3}%
\cmidrule(lr{0.125em}){4-4}%
\cmidrule(lr{0.125em}){5-5}%
\cmidrule(l{0.25em}){6-6}%
Input & 0.75 & 1 & 2 & 5 & 5\\
Output & 0.5 & 2 & 4 & 2 & 5\\
\bottomrule
\end{tabular}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6,6);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[midway,below=8pt]{Output}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[above=5pt]{Input}; %Y-Achse
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x} coordinate (x axis);
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y} coordinate (y axis);
%
\draw
(0,0) coordinate (orig)
(0.75,0.5) coordinate (A)
(1,2) coordinate (B)
(2,4) coordinate (C)
(5,2) coordinate (D)
(5,5) coordinate (E)
;
%
\foreach \pt/\labpos in {A/right,D/below left,E/below}{
\fill (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
\foreach \pt/\labpos in {B/above left,C/above left}{
\fill (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
};
\end{tikzpicture}
\captionsetup{width=0.8\linewidth}
\captionof{figure}{Graphical Illustration of Dataset with an Arbitrary Caption to Highlight the Problem}
\end{center}
\end{minipage}
\end{figure}

\end{document}


# Second solution

An alternative is to specify the caption structure via margin size rather than width, then you do not need to issue separate set-up commands each time:

\documentclass[
11pt
]{scrartcl}

\usepackage{
tikz,
relsize,
amsmath,
booktabs,
tikz
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage[
labelfont=sf,
hypcap=false,
format=hang,
margin=1cm,
]{caption}

\usetikzlibrary{
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
>=latex
}

\begin{document}

\begin{figure}[btc]
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
%\captionsetup{width=0.8\linewidth}
\captionof{table}{Dataset for Something  with an Arbitrary Caption to
Highlight the Problem}
\begin{tabular}{lrrrrr}
\toprule
Store & A & B & C & D & E \\
\cmidrule(r{0.125em}){1-1}%
\cmidrule(lr{0.125em}){2-2}%
\cmidrule(lr{0.125em}){3-3}%
\cmidrule(lr{0.125em}){4-4}%
\cmidrule(lr{0.125em}){5-5}%
\cmidrule(l{0.25em}){6-6}%
Input & 0.75 & 1 & 2 & 5 & 5\\
Output & 0.5 & 2 & 4 & 2 & 5\\
\bottomrule
\end{tabular}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6,6);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[midway,below=8pt]{Output}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[above=5pt]{Input}; %Y-Achse
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x} coordinate (x axis);
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y} coordinate (y axis);
%
\draw
(0,0) coordinate (orig)
(0.75,0.5) coordinate (A)
(1,2) coordinate (B)
(2,4) coordinate (C)
(5,2) coordinate (D)
(5,5) coordinate (E)
;
%
\foreach \pt/\labpos in {A/right,D/below left,E/below}{
\fill (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
\foreach \pt/\labpos in {B/above left,C/above left}{
\fill (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
};
\end{tikzpicture}
%\captionsetup{width=0.8\linewidth}
\captionof{figure}{Graphical Illustration of Dataset with an Arbitrary Caption to Highlight the Problem}
\end{center}
\end{minipage}
\end{figure}

\end{document}


# Third solution

The author, Axel Sommerfeldt, mentions in a comment that there is a new, undocument option to caption, which is calcwidth. Testing this, it works well in your example, at least in the form calcwidth=0.8\linewidth as package option. It recalculates the width specification at each use rather than fixing calculated value at the beginning of the document.

If you use calcwidth=0.8\columnwidth, you have to be very careful about placement of commands switching numbers of columns. As your example is a one column layout, I will stick with the simple \linewidth.

For illustration I have also added RaggedRight to the captions. This allows some hyphenation, in contrast to raggedright which will only break lines at word boundaries.

\documentclass[
11pt
]{scrartcl}

\usepackage{
tikz,
relsize,
amsmath,
booktabs,
tikz,ragged2e
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage[
labelfont=sf,
hypcap=false,
format=hang,
margin=1cm,
justification=RaggedRight,
calcwidth=0.8\linewidth
]{caption}

\usetikzlibrary{
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit}

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
>=latex
}

\begin{document}

\begin{figure}[btc]
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
\captionof{table}{Dataset for Something  with an Arbitrary Caption to
Highlight the Problem}
\begin{tabular}{lrrrrr}
\toprule
Store & A & B & C & D & E \\
\cmidrule(r{0.125em}){1-1}%
\cmidrule(lr{0.125em}){2-2}%
\cmidrule(lr{0.125em}){3-3}%
\cmidrule(lr{0.125em}){4-4}%
\cmidrule(lr{0.125em}){5-5}%
\cmidrule(l{0.25em}){6-6}%
Input & 0.75 & 1 & 2 & 5 & 5\\
Output & 0.5 & 2 & 4 & 2 & 5\\
\bottomrule
\end{tabular}
\end{center}
\end{minipage}
\hfill
\begin{minipage}[c]{0.48\linewidth}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (6,6);
%
\draw[->,thick] (-0.1,0) -- (6.5,0) node[midway,below=8pt]{Output}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[above=5pt]{Input}; %Y-Achse
%
\foreach \x in {0,1,...,6} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,6} \draw (\x cm, 0 cm) node[anchor=north]{\x} coordinate (x axis);
\foreach \y in {0,1,...,6}  \draw (0 cm, \y cm) node[anchor=east]{\y} coordinate (y axis);
%
\draw
(0,0) coordinate (orig)
(0.75,0.5) coordinate (A)
(1,2) coordinate (B)
(2,4) coordinate (C)
(5,2) coordinate (D)
(5,5) coordinate (E)
;
%
\foreach \pt/\labpos in {A/right,D/below left,E/below}{
\fill (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
\foreach \pt/\labpos in {B/above left,C/above left}{
\fill (\pt) circle (3pt) node[\labpos=3pt,fill=white]{\pt};
};
\end{tikzpicture}
\captionof{figure}{Graphical Illustration of Dataset with an Arbitrary Caption to Highlight the Problem}
\end{center}
\end{minipage}
\end{figure}

\end{document}

-
Thank you. A little note: in your first code snippet it should say \captionsetup{width=0.8\linewidth}. –  henry Aug 16 '13 at 8:58
It would be nice if it would autoadjust, but this is currently the best solution –  daleif Aug 16 '13 at 9:00
@henry Thanks. I was in the process of adding that whilst writing my second solution. –  Andrew Swann Aug 16 '13 at 9:02
@daleif Good point. A global RaggedRight might be acceptable in some circumstances. –  Andrew Swann Aug 16 '13 at 9:35
Third solution: Don't use width=0.8\columnwidth but calcwidth=0.8\columnwidth instead. The option calcwidth (and calcmargin) is available since v3.2 (2011/07/30) of the caption package but I didn't find the time to document it so far. –  Axel Sommerfeldt Aug 16 '13 at 19:02