4

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?

3

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}
  • PS: change the question title to something more fitting if you can think of anyhting. – Ingo Aug 25 '14 at 15:43
3

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}
2

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

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.