Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
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
add comment

2 Answers

up vote 2 down vote accepted

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}

Colorbox figure with caption top-left

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

share|improve this answer
    
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
add comment

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}

enter image description here

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.