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.

I would like to indicate an inequality constraint using a hatched pattern. This is to be done in a pgfplot, that is itself part of a beamer frame.

Here is an MWE:

\documentclass{beamer}

\usepackage{tikz,pgfplots} 
    \usetikzlibrary{patterns}

\begin{document} 
    \begin{frame}{Test}
        \begin{tikzpicture}[remember picture]
            \begin{axis}[
                width=\textwidth,
                minor tick num=1,
                axis y line=center,
                axis x line=middle,
                ]
                \addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
                \draw[red, thick] (axis cs:-2, 0) -- (axis cs:-2, 9);
                \draw[pattern=north west lines, thin, red] (axis cs:-2,0)
                rectangle (axis cs:-1.75, 9);
            \end{axis}
        \end{tikzpicture}
    \end{frame} 
\end{document}

The resulting output, however, is: A solid line, not a pattern

As you can see, a solid line is drawn rather than a pattern. How can I fix this?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

This is unrelated to the use of beamer. To specify the colour for the pattern, you have to use draw=red, pattern color=red instead of just red:

\documentclass{article}

\usepackage{pgfplots} 
\usetikzlibrary{patterns}

\begin{document} 
        \begin{tikzpicture}
            \begin{axis}[
                width=\textwidth,
                minor tick num=1,
                axis y line=center,
                axis x line=middle,
                ]
                \addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
                \draw[pattern=north west lines, draw=red, pattern color=red] (axis cs:-2,0)
                rectangle (axis cs:-1.75, 9);
            \end{axis}
        \end{tikzpicture}
\end{document}
share|improve this answer
    
The wood for the trees... Thanks! –  Ingo Aug 25 at 15:41
    
PS: change the question title to something more fitting if you can think of anyhting. –  Ingo Aug 25 at 15:43

Use pattern color=red should do the trick

enter image description here

Code

\documentclass{beamer}

\usepackage{tikz,pgfplots} 
    \usetikzlibrary{patterns}

\begin{document} 
    \begin{frame}{Test}
        \begin{tikzpicture}[remember picture]
            \begin{axis}[
                width=\textwidth,
                minor tick num=1,
                axis y line=center,
                axis x line=middle,
                ]
                \addplot[smooth, cyan, mark=none, domain=-3:3, samples=40, thick] {x^2};
                \draw[red, thick] (axis cs:-2, 0) -- (axis cs:-2, 9);
                \draw[pattern=north west lines, thin, pattern color=red] (axis cs:-2,0)
                rectangle (axis cs:-1.75, 9);
            \end{axis}
        \end{tikzpicture}
    \end{frame} 
\end{document}
share|improve this answer

The simplest solution in PSTricks just for fun.

\documentclass{beamer}

\usepackage{pst-plot}

\begin{document} 
\begin{frame}{Test}
\psgraph(0,0)(-3,0)(3,9){5cm}{6cm}
    \psplot[algebraic,linecolor=blue]{-3}{3}{x^2}
    \psframe[dimen=m,fillstyle=vlines,hatchcolor=red](-2,0)(-1.5,9)
\endpsgraph
\end{frame} 
\end{document}

enter image description here

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.