[Tex/LaTex] Tikz fade with transparency so background is visible in beamer

beamertikz-pgftransparency

I am writing a presentation in beamer. I have a background image that I have included with \setbeamertemplate{background canvas}{...}. Within a regular slide, I now would like to create a rectangle that fades from a color to fully transparent in Tikz. If I create a rectangle with fading, i.e. \tikz{\fill[left color=RED,right color=transparent] (0,0) rectangle (\linewidth,1.5em);} It creates the rectangle, but rather than being see-through at the right end, it is black.

Is there a way to define opacity in fade, such that the background image becomes visible?

M.W.E

\documentclass{beamer}
  \usepackage{tikz}
  \usepackage{graphicx}
  \usetikzlibrary{shapes.arrows, fadings}

  \setbeamertemplate{background canvas}{\includegraphics[width=\paperwidth]{picture.jpg}}

  \begin{document}
    \begin{frame}
      \tikz{\fill[left color=red,right color=transparent] (0,0) rectangle (\linewidth,1.5em);}
    \end{frame}
  \end{document}

(Use any random picture for the background)

Best Answer

You need to set up a fading via \tikzfading and then use the fade path option to the fill command:

Sample output

\documentclass{beamer}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{shapes.arrows, fadings}

\setbeamertemplate{background canvas}{\includegraphics[width=\paperwidth]{example-image-a.jpg}}

\tikzfading[name=fade right,
  left color=transparent!0, right color=transparent!100]

\begin{document}
\begin{frame}
  \tikz{\fill[red,path fading=fade right] (0,0) rectangle (\linewidth,1.5em);}
\end{frame}
\end{document}

See the section "Creating Fadings" (currently 23.4.1) of the pgfmanual.