1

This question:

How to set the pagestyle on the page a particular float ends up on?

Almost answers my question, but not quite. The floatpag macro \thisfloatpagestyle can set a custom footer for a specific float, but only if the float appears on a float-only page.

How can I get a custom footer to occur on whatever page a particular float lands, when that page isn't a float-only page?

-Ryan

1
  • You could do it by using \pageref, comparing its output with the current page and deciding upon this. Code would be in the page header/footer for more than a single page, though.
    – Skillmon
    Feb 5, 2019 at 23:59

1 Answer 1

1

The following uses the approach lined out in my comment. It needs at least two runs to get it right.

\documentclass[]{article}

\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{refcount}
\makeatletter
\newcommand*\float@foot{}
\newcommand\floatfoot@remove[1]
  {%
    \begingroup
      \long\def\floatfoot@remove@##1#1##2\endfloatfoot@remove@
        {%
          \gdef\float@foot{##1##2}%
        }%
      \expandafter\floatfoot@remove@\float@foot\endfloatfoot@remove@
    \endgroup
  }
\newcommand\floatfoot[2]
  {%
    \expandafter\g@addto@macro\expandafter\float@foot\expandafter
      {%
        \csname float@foot@#1\endcsname
      }%
    \expandafter\gdef\csname float@foot@#1\endcsname
      {%
        \ifcsname r@#1\endcsname
          \ifnum\getpagerefnumber{#1}=\value{page}
            #2%
            \expandafter\floatfoot@remove\csname float@foot@#1\endcsname
          \fi
        \fi
      }%
  }
\fancyhead[C]{\float@foot}
\makeatother

\usepackage{duckuments} % only for dummy content (also randomizes the ducks)

\usepackage{hyperref} % only to test whether it works with hyperref loaded

\pagestyle{fancy}

\begin{document}
\blindduck[1-5]
\begin{figure}% >>>
  \centering
  \includegraphics{example-image-duck}%
  \caption
    {%
      a duck%
      \label{fig:duck1}%
    }%
  \floatfoot{fig:duck1}{Page containing a duck}% might be used anywhere before
\end{figure}% <<<
\begin{figure}% >>>
  \centering
  \includegraphics{example-image-duck}%
  \caption
    {%
      a duck%
      \label{fig:duck2}%
    }%
  \floatfoot{fig:duck2}{Page containing the second duck}%
\end{figure}% <<<

\end{document}

enter image description here

2
  • Wow that is complex! I wouldn't have come up with that in a million years. Thank you.
    – Ryan B.
    Feb 7, 2019 at 0:21
  • @R.Battelle only problem could be the parts of the document using different page numbering, because the page counter gets reset there. But if you use \floatfoot after those points everything should be fine. Also you might want to introduce some code to handle two different entries on a single page (how should they be displayed?).
    – Skillmon
    Feb 7, 2019 at 8:15

You must log in to answer this question.

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