TransWikia.com

Add a watermark overlayed to floats (figures, tables ... but mostly tables)

TeX - LaTeX Asked by Milleuros on April 19, 2021

I would like to put a watermark, e.g. "DRAFT", but only overlayed and on specific figures and tables.

Looking around, I tried to use the background package. At the beginning of my document:

usepackage[some]{background}
SetBgScale{1}
SetBgContents{parbox{10cm}{%
  Huge Draft:  today[14cm]rotatebox{180}{Huge Draft:  today}}}
SetBgColor{gray}
SetBgAngle{270}
SetBgOpacity{0.2}

Then inside the table environment:

begin{table}
BgThispage
small
begin{center}
begin{tabular}
[...]

The result is however that the watermark appears one page before the table, not on the same page. I tried adding the [H] option to the table, hoping to force LaTeX but to no avail.

Then I tried using the xwatermark package. While not optimal, it gives me an option to apply a watermark on specific pages:

usepackage[printwatermark]{xwatermark}
newwatermark[pagex={178,179},fontfamily=bch,color=gray!25,angle=45,scale=3,12xpos=0,ypos=0]{DRAFT}

However when I compile with PDFLatex, XeLatex or LuaLatex:

 ! Package ltxkeys Error: The above keys are undefined and couldn't be set.

A workaround I have for figures is to open them in Inkscape and directly write my watermark on top of it. But I don’t have this option for the table.

Do you have a solution? Thanks!

2 Answers

(Too big for a comment)

tikzmark{}s inside the floats shows promise:

tikzfloat

Someone more knowledgeable in TikZ might be able to help.

MWE

documentclass{article}
usepackage{graphicx}
usepackage{tikz}
usetikzlibrary{tikzmark}
begin{document}
x
newpage
xx 

begin{figure}
tikzmark{tmTL}
begin{center}
includegraphics{example-image-duck}
tikzmark{tmBR}
end{center}
caption{Caption}
label{fig:eg}
end{figure}

Some text

begin{table}
begin{center}
begin{tabular}{ccc}
hline 
tikzmark{ttmTL} xxx & xxx & xxx 
xxx & xxx & xxx 
xxx & xxx & xxxtikzmark{ttmBR}  
hline 
end{tabular}
caption{Caption}
label{tab:eg}
end{center}
end{table}

tikz[overlay,remember picture]draw[->,red,thick] (pic cs:tmTL) to [edge label={huge DRAFT}] (pic cs:tmBR) ;

tikz[overlay,remember picture]draw[->,red,thick,] (pic cs:ttmTL) to [edge label={huge DRAFT}]  (pic cs:ttmBR) ;
end{document}

Correct answer by Cicada on April 19, 2021

This uses lrbox and varwidth to measure the size of the contents, llap to overlay it, and resizebox to scale the overlaid image.

documentclass{article}
usepackage{xcolor}
usepackage{graphicx}
usepackage{adjustbox}% for resizebox
usepackage{varwidth}

newsavebox{draftbox}
savebox{draftbox}{color{red}huge Draft}% create image for overlay

newenvironment{draft}{begin{lrbox}{0}varwidth[b]{linewidth}}%
  {endvarwidthend{lrbox}usebox0
  llap{resizebox{wd0}{ht0}{useboxdraftbox}}}

begin{document}

begin{figure}
centering
begin{draft}
includegraphics{example-image-duck}
caption{Caption}
label{fig:eg}
end{draft}
end{figure}

Some text

begin{table}
centering
begin{draft}
begin{tabular}{ccc}
hline 
xxx & xxx & xxx 
xxx & xxx & xxx 
xxx & xxx & xxx 
hline 
end{tabular}
caption{Caption}
label{tab:eg}
end{draft}
end{table}
end{document}

This version uses the entire width and puts the overlay into the background. Note that centering is now inside draft.

documentclass{article}
usepackage{xcolor}
usepackage{graphicx}
usepackage{adjustbox}% for resizebox

newsavebox{draftbox}
savebox{draftbox}{color{red}huge Draft}% create image for overlay

newenvironment{draft}{begin{lrbox}{0}minipage[b]{linewidth}}%
  {endminipageend{lrbox}%
  leavevmoderlap{resizebox{wd0}{ht0}{useboxdraftbox}}usebox0}

begin{document}

begin{figure}
begin{draft}
centering
includegraphics{example-image-duck}
caption{Caption}
label{fig:eg}
end{draft}
end{figure}

Some text

begin{table}
begin{draft}
centering
begin{tabular}{ccc}
hline 
xxx & xxx & xxx 
xxx & xxx & xxx 
xxx & xxx & xxx 
hline 
end{tabular}
caption{Caption}
label{tab:eg}
end{draft}
end{table}
end{document}

Answered by John Kormylo on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP