# Pgfplots and beamer: how to fill the area under a curve with oblique lines (hatching) as a pattern?

This is a follow up question to Jake's solution to that post.

Although I got Jake's solution there work in LaTeX document perfectly fine, I could not get it work in beamer. Here's Jake's code but using beamer:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}

\begin{axis}[
xmin=-4,xmax=4,
xlabel={z},
ymin=0,ymax=1,
axis on top,
legend style={legend cell align=right,legend plot pos=right}]
domain=0:1,
samples=100,
pattern=flexible hatch,
area legend,
pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
domain=-2:-0.5,
samples=100,
pattern=flexible hatch,
hatch distance=5pt,
hatch thickness=0.5pt,
draw=blue,
pattern color=cyan,
area legend]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}


It seems the system never finish compiling. I not sure whether it is just my system or it is a more general problem?

Meanwhile, Marco's solution there does work in beamer.

-
Please compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Jul 25 '12 at 1:20
I've added to your question the example code illustrating the problem; I hope it's OK. –  Gonzalo Medina Jul 25 '12 at 4:35
@GonzaloMedina, It is perfectly OK. Thanks. –  Causality Jul 25 '12 at 17:03

Simply add the fragile option to the frame:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\begin{document}

\begin{frame}[fragile]
\begin{tikzpicture}
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}

\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}

\begin{axis}[
xmin=-4,xmax=4,
xlabel={z},
ymin=0,ymax=1,
axis on top,
legend style={legend cell align=right,legend plot pos=right}]
domain=0:1,
samples=100,
pattern=flexible hatch,
area legend,
pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
domain=-2:-0.5,
samples=100,
pattern=flexible hatch,
hatch distance=5pt,
hatch thickness=0.5pt,
draw=blue,
pattern color=cyan,
area legend]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;

Add [fragile] after \begin{frame}[fragile] It is common for LaTeX-Tikz to hang when code is involved and [fragile] it is omitted.