5

is there a way to control the caption format used for figures, depending on where the figure has been placed? In particular, i would like to have different formatting of figure captions for figures that are placed to the top, and figures that are placed on the bottom of a page.

In my MWE i define a format that uses a horizontal line under the caption text. This is ok if the figure is on the top of the page and the caption is below the figure. But when the figure is at the bottom, i would put the caption above the figure - and then i'd like to use a caption format that has the horizontal line above the text.

Do you think something like that is possible, even automatically?

Cheers Janos

P.s. in order to compile my MWE, you'd need an image called "blackbox".

\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage{caption}
\usepackage{lipsum}
\DeclareCaptionFormat{figureFormat}{#1#2#3\hrulefill}
\captionsetup[figure]{format=figureFormat}
\begin{document}
    \begin{figure}[!t]
        \centering
        \label{fig:1}
        \includegraphics[width=\columnwidth]{blackbox}
        \caption{This figure is on the top. The caption should be on the bottom,
        and the horizontal line under it.}
    \end{figure}
    \lipsum[1]
    \lipsum[1]
    \begin{figure}[!b]
        \centering
        \caption{This figure is on the bottom, so it caption is above it. Now
        the horizontal line should be also above this text...}
        \label{fig:2}
        \includegraphics[width=\columnwidth]{blackbox}
    \end{figure}
\end{document}
1
  • For placing the rules, LaTeX provides \topfigrule and \botfigrule which place rules (or whatever) below or above (respectively) a top or bottom placed figure. But since the float is put in a box and then placed, there is no way AFAIK to change the placement of the caption dependent on the placement of the float.
    – Alan Munn
    Commented Aug 2, 2014 at 23:37

2 Answers 2

4

As mentioned in the comments, I don't think there's a way to change the caption position automatically. However, LaTeX provides two commands that can place the rule automatically for you: \topfigrule and \botfigrule. Here's an example, but as with Steven's solution, this still requires you to specify what placement the figure will be to get the caption correct. (Also, remember to makes sure your labels follow the caption command or are inside it.)

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{lipsum}
\newcommand{\topfigrule}{\vspace{5pt} \hrule \vspace{-5pt}}
\newcommand{\botfigrule}{\vspace{-5pt} \hrule \vspace{5pt}}
\begin{document}
\lipsum[1]
    \begin{figure}[t]
        \centering
        \includegraphics[width=\columnwidth]{blackbox}
        \caption{This figure is on the top. The caption should be on the bottom,
        and the horizontal line under it.}
        \label{fig:1}
    \end{figure}
\lipsum[1]
\begin{figure}[b]
    \centering
    \caption{This figure is on the bottom, so it caption is above it. Now
    the horizontal line should be also above this text...}
    \label{fig:2}
    \includegraphics[width=\columnwidth]{blackbox}
\end{figure}
\lipsum[1]
\end{document}
1
  • Thanks @Alan! I think i can live with manually specifying the positioning for now, and \topfigrule and \botfigrule is in that case what i need. Thanks also for pointing me to be careful with labels. That is something i was struggling with quite heavily the other day!
    – Janos
    Commented Aug 3, 2014 at 9:23
3

Well, if you explicitly define what is a top vs. bottom figure with the [!t] or [!b] specifier, it is only one extra step to redefine \captionsetup to tfigureFormat or bfigureFormat, accordingly, as shown in my MWE.

Alan Munn points out that this solution is not automatic, but only works if the placement of the figures is explicit. He references Frank Mittelbach's excellent answer at How to influence the position of float environments like figure and table in LaTeX? to point out that an [ht] specifier could automatically change figure placement to either top or bottom, for which my technique would be unprepared to cope.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{lipsum}
\DeclareCaptionFormat{tfigureFormat}{#1#2#3\hrulefill}
\DeclareCaptionFormat{bfigureFormat}{\hrulefill\par\medskip#1#2#3}
\begin{document}b
    \captionsetup[figure]{format=tfigureFormat}
    \begin{figure}[!t]
        \centering
        \label{fig:1}
        \includegraphics[width=\columnwidth]{blackbox}
        \caption{This figure is on the top. The caption should be on the bottom,
        and the horizontal line under it.}
    \end{figure}
    \lipsum[1]
    \lipsum[1-4]
    \captionsetup[figure]{format=bfigureFormat}
    \begin{figure}[!b]
        \centering
        \caption{This figure is on the bottom, so it caption is above it. Now
        the horizontal line should be also above this text...}
        \label{fig:2}
        \includegraphics[width=\columnwidth]{blackbox}
    \end{figure}
\end{document}
5
  • This assumes that you know where each figure ends up in advance. I think @noshky is looking for a more automatic placement.
    – Alan Munn
    Commented Aug 2, 2014 at 23:07
  • @AlanMunn I thought about that. However, unless !t or !b is explicitly specified, the figures will not change from top to bottom (or vice versa) in the course of a document (to my knowledge). So I answered on the assumption that a placement change had to be explicit. Commented Aug 2, 2014 at 23:09
  • If you specify [tb] (order irrelevant) for a float, then TeX will place it either at the top or the bottom, depending on space and and other floats. So the placement of any particular float can change as the rest of the document changes. See Frank Mittelbach's extensive answer to this question How to influence the position of float environments like figure and table in LaTeX? for all the details.
    – Alan Munn
    Commented Aug 2, 2014 at 23:34
  • @AlanMunn Thank you for this. Revised answer reflects your concern. Commented Aug 2, 2014 at 23:51
  • @Steven Thanks a lot! I like the pragmatic implication, if i have to specify the positioning of the caption anyway (which i seem to have to), then i can as well specify the format. It seems to be simpler with AlanMunn's proposed solution, but thanks for showing me how to specify the formats. This might be eventually more powerful.
    – Janos
    Commented Aug 3, 2014 at 9:22

You must log in to answer this question.

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