# User-specified positioning of caption

For tuning reasons that I cannot detail here, I would like to be able to control the position of the caption of figures. My current code is as follows:

\begin{figure}[ht!]
\colorbox{red!10}{%
\parbox{\textwidth-2\fboxsep-2\fboxrule}{%
\centering\includegraphics{fig1}%
\caption{}%
\label{fig:ch1-1}}}%
\end{figure}


Is is possible to move the caption (which is here only Figure 1) to the top-left corner of the colorbox. I know that the float package offers similar possibilities but would like to know if there is a solution by hand.

-
Inside the \colorbox or outside and above? – Werner Sep 5 '11 at 17:48
inside, top-left corner. Thanks – pluton Sep 5 '11 at 17:54

## 2 Answers

Here is a marginal alternative to Herbert's answer that preserves some of the original vertical/horizontal spacing:

\documentclass{article}
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{calc}% http://ctan.org/pkg/calc
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{caption}% http://ctan.org/pkg/caption
\begin{document}
\begin{figure}[ht!]
\colorbox{red!10}{%
\parbox{\textwidth-2\fboxsep-2\fboxrule}{%
\captionsetup{format=plain,singlelinecheck=false}
\caption{}\label{fig:ch1-1} \removelastskip\vspace{\dimexpr-\baselineskip-\abovecaptionskip\relax}
\centering\includegraphics{fig1}%
}%
}%
\end{figure}
\end{document}


Moving the \caption to above the \includegraphics places the caption above the image. The demo package option to graphicx is merely for illustration.

-
what about being on the top left without being above (ie no extra vertical space)? – pluton Sep 5 '11 at 18:21
@pluton: I've updated my answer to reflect this adjustment. The vertical space is reverse using -\baselineskip-10pt where 10pt is the standard space between figure and caption (taken from the caption package documentation). – Werner Sep 5 '11 at 18:36
Better use \abovecaptionskip instead of 10pt, or simply set \abovecaptionskip to 0pt before \caption. – Axel Sommerfeldt Sep 5 '11 at 18:45
the caption is still over the image when it is longer than the left side of the image. – Herbert Sep 5 '11 at 18:50
@Axel: Thanks; I originally tried \belowcaptionskip. – Werner Sep 5 '11 at 19:25

You can cretae a new macro for this:

\documentclass{article}
\usepackage[demo]{graphics}
\usepackage{xcolor,calc}
\def\Caption#1{\refstepcounter{figure}Figure~\thefigure: #1}
\begin{document}

\begin{figure}[ht!]
\colorbox{red!10}{%
\parbox{\textwidth-2\fboxsep-2\fboxrule}{%
\Caption{\label{fig:ch1-1}}\hfill
\raisebox{-\height+0.7\baselineskip}{\includegraphics{image}}}}
\end{figure}

\end{document}


-
your minipages cannot be avoided? – pluton Sep 5 '11 at 19:16
@pluton: sure, but then the caption cannot be longer than one line. – Herbert Sep 5 '11 at 19:49
yes this is true. In this special case of interest, there will be no fooo. Thanks – pluton Sep 5 '11 at 20:36
then you also do not need the caption package, see edit – Herbert Sep 5 '11 at 21:06