6

To my knowledge, the contents of a float environment are put into a box when the float is defined. Is it possible to proccess a user defined command/macro when the float is actually typeset, e.g. to put a watermark on that specific page (which isn't necessarily the page the float is defined on)?

Is it necessary to add something to the definition of the float enviroment or can I use some command within its body?

  • 2
    short answer: no, longer answer: you'd have to rewrite most of latex's page breaking mechanism. – David Carlisle Mar 7 '14 at 23:10
  • @DavidCarlisle isn't that worth an answer instead of a comment? And the reason it won't work is because latex can't know what the size of the float is and thus where to place it before if actually expands the macro's, isn't it? – hugovdberg Mar 7 '14 at 23:17
  • 2
    @hugovdberg nah it's a comment, if it was posted as an answer it would be automatically flagged as low quality for being too short:-) For the watermark example you don't need macros really just a \pageref to the float then add the mark when you get to that page, but the general case is harder – David Carlisle Mar 7 '14 at 23:24
  • But if using a \pageref would allow mit to place a watermark, wouldn't that also allow me to place arbitrary code on the page where the float is located (assuming that this code doesn't cause the float to move somewher else)? I would appreciate if you would extend your comment into an answer (showing e.g. the watermark example)! – porst17 Mar 8 '14 at 8:05
  • Could you provide a MWE that gives us an idea and some code of what you are attempting? The boxhandler package allows deferred typesetting of figure and table floats, but I'm not sure if what it does is adaptable to what you have in mind. – Steven B. Segletes Mar 10 '14 at 12:42
3

If you're only after a watermark on a page where a certain type of float appears, then you can tap into the shipout routine using atbegshi and condition on some counter magic with a float counter tracking mechanism:

enter image description here

\documentclass{article}
\usepackage{atbegshi,refcount,etoolbox,graphicx,xcolor}
\usepackage{lipsum,afterpage}

\newcounter{floatcntr}
\AtEndEnvironment{figure}{\refstepcounter{floatcntr}\label{floatcntr@\roman{floatcntr}}}
\newcounter{floatsearch}\setcounter{floatsearch}{1}

\AtBeginShipout{%
  \ifnum\value{page}>\getpagerefnumber{floatcntr@\roman{floatsearch}}
    \stepcounter{floatsearch}% In case there are multiple (2) floats on the same page
  \fi
  \ifnum\value{page}=\getpagerefnumber{floatcntr@\roman{floatsearch}}
    \AtBeginShipoutUpperLeft{%
      \parbox[t]{\pdfpagewidth}{\centering\bfseries\sffamily%
        \vspace{.4\pdfpageheight}%
        \resizebox{.7\pdfpagewidth}{!}{\textcolor{red!80}{DRAFT}}}
    }%
    \stepcounter{floatsearch}
  \fi
}

\begin{document}

\lipsum[1-3]

\begin{figure}[ht]
  \centering\includegraphics[width=.6\linewidth]{example-image}
  \caption{A figure}
\end{figure}

\lipsum[4-12]

\afterpage{\clearpage}% Just to flush the float
\begin{figure}[p]
  \centering\includegraphics[width=.6\linewidth]{example-image}
  \caption{A figure}
\end{figure}

\lipsum[13-25]

\end{document}

The above MWE creates a counter floatcntr that is stepped (and referenced) at the end of every figure. The reference is then compared against the page number at shipout, and some background content is displayed if there is a match.

Caveat: The above setup assumes no more than two figures per page. If more exist (highly unlikely), some modifications may be required.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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