Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}] 
    \addplot[color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
    \addlegendentry{z}
    \addplot+[mark=none,
        domain=0:1,
        samples=100,
        pattern=flexible hatch,
        area legend,
        pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    \addlegendentry{Interval 1}
    \addplot+[mark=none,
        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;    
        \addlegendentry{Interval 2}
    \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.

share|improve this question
    
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
1  
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

2 Answers 2

up vote 7 down vote accepted

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}] 
    \addplot[color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
    \addlegendentry{z}
    \addplot+[mark=none,
        domain=0:1,
        samples=100,
        pattern=flexible hatch,
        area legend,
        pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    \addlegendentry{Interval 1}
    \addplot+[mark=none,
        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;    
        \addlegendentry{Interval 2}
    \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.