15

I am trying to make a box with shadow, but the filling should be transparent. However, my problem is that the shadow is showing through the transparent filling, see

enter image description here

The code is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shadows}

\begin{document}
    \begin{tikzpicture}[baseline=(char.base)]
    \node(char)[draw,fill=white,rectangle, rounded corners = 2pt,drop shadow={opacity=.5, shadow xshift=0.1cm},% blur shadow = {shadow blur steps = 5}
    fill opacity = 0.4] {Shadow};
    \end{tikzpicture}
\end{document}

How can I make the box with transparent filling, but the shadow not showing through the transparent filling?

14

To avoid seeing the shadow in the transparent area, you can put the button inside a knockout transparency group. If the background and the shadow are in the same knockout group, the group "knocks out" the shadow:(I added some background text to show the transparency of the button and text opacity to increase the contrast)

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{tikz}
\usetikzlibrary{shadows}

\begin{document}
    \begin{tikzpicture}[baseline=(char.base)]
      \node at (0,0) {\fontsize{5pt}{0pt}\selectfont\parbox{4cm}{\kant[1]}};
      \begin{scope}[transparency group=knockout]
        \node(char)[draw,fill=white,rectangle, rounded corners = 2pt,drop shadow={opacity=.5, shadow xshift=0.1cm},% blur shadow = {shadow blur steps = 5}
        fill opacity = 0.4, text opacity = 0.8] {Shadow};
      \end{scope}[transparency group=knockout]
    \end{tikzpicture}
\end{document}

enter image description here

Warning: Knockout groups are not supported by a lot of PDF viewers. Especially Acrobat is the only viewer I know supporting this.

3

A tcolorbox with a beamer style is a handy option of a more realistic appearance of translucent glass with shadow:

mwe

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\tcbset{beamer,boxrule=1pt,tikz={opacity=0.8,transparency group},fontupper=\bfseries\sffamily}
\begin{document}
\lipsum[1]
\vspace{-12em}\centering
\tcbox{Shadow}
\vspace{3em}
\tcbox[shadow={1mm}{-1mm}{-.5mm}%
{fill=cyan!75,opacity=0.15}]{Shadow}
\end{document}

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.