4

I am using the shapes.geometric library for drawing a cylinder. I am also using shadows.blur for the blur effect. The problem is that combining the two libraries doesn't produce the expected/desired output.

Output

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % Cylinder
\usetikzlibrary{shadows.blur}
\begin{document}
  \begin{tikzpicture}[
    withoutShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
    },
    withShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
      blur shadow={
        shadow blur steps=10,
        shadow blur extra rounding=2pt, 
        shadow xshift=1pt
      }
    },
  ]
  \node[withoutShadow](c1) at (0, 0){No shadow};
  \node[withShadow, below of = c1, yshift = -1em](c2){With shadow};
  \node[withShadow, fill = white, below of = c2, yshift = -1em]
       {With shadow and \texttt{fill=white}};
  \end{tikzpicture}
\end{document}

Is there a way to have the cylinder uses custom fill option to work as expected and a valid blur around the cylinder?

5

You could use this definition of cylinder end fill, which, apart from solving your problem, also allows you to have a gradient fill.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % Cylinder
\usetikzlibrary{shadows.blur,positioning}
\tikzset{cylinder end fill/.style={path picture={
\pgftransformshift{\centerpoint}%
\pgftransformrotate{\rotate}%  
\pgfpathmoveto{\beforetop}%
\pgfpatharc{90}{-270}{\xradius and \yradius}%
\pgfpathclose
\pgfsetfillcolor{#1}%
\pgfusepath{fill}}
}}

\begin{document}
  \begin{tikzpicture}[font=\sffamily,
    withoutShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
    },
    withShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt,     
      cylinder end fill=red, 
      draw=red,
      blur shadow={
        shadow blur steps=10,
        shadow blur extra rounding=2pt, 
        shadow xshift=1pt
      }
    },
  ]
  \node[withoutShadow](c1) at (0, 0){No shadow};
  \node[withShadow, below=1em of c1,
  left color=yellow!40,right color=yellow, middle color=yellow!20,
      shading angle=0,](c2){With shadow and gradient fill};
  \node[withShadow, fill = yellow, below=1em of c2,]
       {With shadow and \texttt{fill=yellow}};
  \end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thank you for the answer! I appreciate your time :) – M. Al Jumaily Sep 10 '19 at 2:17
  • @M.AlJumaily You are very welcome! – Schrödinger's cat Sep 10 '19 at 2:25

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.