\documentclass{beamer} \usetheme{CambridgeUS} \usepackage{listings} \lstset{language=[LaTeX]TeX, morekeywords={draw,clip}, keywordstyle=\color{blue}, commentstyle=\color{red}, frame=single} \begin{document} \begin{frame}[fragile] \frametitle{Clip to a Rectangle} A much simpler solution is to establish a clipping region. \bigbreak \begin{lstlisting}[escapeinside=!~] \begin{tikzpicture} \draw[help lines] (-5,-5) grid (5,5); \draw[thick,<->] (-5,0) -- (5,0) node[right] {$x$}; \draw[thick,<->] (0,-5) -- (0,5) node[above] {$y$}; \begin{scope} \clip (-5,-5) rectangle (5,5); \draw [thick,blue] plot (\x,{\x*(\x+2)*(\x-2)}); \end{scope} \end{tikzpicture} \end{lstlisting} \bigbreak Compile \verb+workshop5.tex+ and note the difference. \end{frame} \end{document}