2

In the MWE we can see shadowed nodes with texts. If we don't add a watermark, the result is as desired on the first TiKZ node, but with a watermark, the fill=write option required by the copy shadow option fills the node with white taking off a region of the water mark, so I added the text opacity and fill opacity options (shown on the second TiKZ picture), but in this case the shadow is dropped, predictably due to the way shadows on TiKZ works.

How can I achieve the look of the first node with its shadows and also transparency to not overlap the watermark?

MWE (requires example-grid-100x100bp.jpg from mwe package):

\documentclass{article}
\usepackage{tikz}\usetikzlibrary{shadows}
\usepackage{graphicx}
\usepackage[absolute]{textpos}
\parindent0pt
\begin{document}
 \begin{textblock*}{\textwidth}(0pt,0pt)
  \includegraphics[scale=2]{example-grid-100x100bp.jpg}
 \end{textblock*}
 \begin{tikzpicture}
  \node[draw=black, fill=white, line width=1pt, rounded corners=3pt, copy shadow={top color=black, bottom color=black, shadow xshift=2pt, shadow yshift=-2pt}]{test1};
 \end{tikzpicture}
 \begin{tikzpicture}
  \node[draw=black, fill=white, fill opacity=0, text opacity=1, line width=1pt, rounded corners=3pt, copy shadow={top color=black, bottom color=black, shadow xshift=2pt, shadow yshift=-2pt}]{test2};
 \end{tikzpicture}
\end{document}
2
  • what do you mean by the watermark? May 16, 2020 at 15:45
  • @UlrikeFischer I mean whatever image behind the TiKZ picture; in my case I'll place a watermark with textblock but generally speaking it doesn't have to be a watermark. Thank you! May 17, 2020 at 1:10

1 Answer 1

2

I hope not to have misread the question. You can use a reverse clip. But unless one wants to go too hacky, this may mean that one has to draw the node twice. So the steps here are

  1. Draw node without shadow and save node boundary path.
  2. Protect node boundary path.
  3. Draw node with shadow and fill opacity=1.

One could try to hack the shadow mechanism but not sure if this is worth the effort.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}
% based on 
% https://tex.stackexchange.com/a/38995/121799 
% https://tex.stackexchange.com/a/76216 
% https://tex.stackexchange.com/a/59168/194703 
% https://tex.stackexchange.com/q/448920/194703  
\makeatletter 
\tikzset{ 
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}} 
} 
\tikzset{even odd clip/.code={\pgfseteorule}, 
protect/.code={ 
\clip[overlay,even odd clip,reuse path=#1] 
(-6383.99999pt,-6383.99999pt) rectangle (6383.99999pt,6383.99999pt); 
}} 
\usepackage[absolute]{textpos}
\parindent0pt
\begin{document}
 \begin{textblock*}{\textwidth}(0pt,0pt)
  \includegraphics[scale=2]{example-grid-100x100bp.jpg}
 \end{textblock*}
 \begin{tikzpicture}
  \node[draw=black, fill=white, line width=1pt, rounded corners=3pt, copy shadow={top color=black, bottom color=black, shadow xshift=2pt, shadow yshift=-2pt}]{test1};
 \end{tikzpicture}
 \begin{tikzpicture}
  \node[draw=black, fill=white, fill opacity=0, text opacity=1, line width=1pt,
    rounded corners=3pt,save path=\tmprotect,
    %copy shadow={top color=black, bottom color=black, shadow xshift=2pt, shadow yshift=-2pt}
    ]{test2};
  \tikzset{protect=\tmprotect}  
  \node[draw=black, fill=white, text opacity=1, line width=1pt, rounded corners=3pt, copy shadow={top color=black, bottom color=black, shadow xshift=2pt, shadow yshift=-2pt}]{test2};  
 \end{tikzpicture}
\end{document}

enter image description here

3
  • Thank you! it works for me; now I'm trying to understand! Why all the code after makeatletter? specially, what does those dimensions mean? May 17, 2020 at 1:13
  • @EmilioLazo This does an even odd clip using the node boundary and an insanely large box so that the shadow will only be drawn outside of the node (and inside the insanely large box, which is chosen so large that you do not clip anything that you want to keep).
    – user194703
    May 17, 2020 at 1:59
  • cat Thank you! I did implement your solution on the real case. I hope you alive! May 17, 2020 at 16:22

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.