# Error Filling Between Curves in Tikz

I'm trying to shade the region bounded by y=x^2, y=exp(-x), and the y-axis (in the first quadrant). This is the code I have so far:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\draw[very thin,color=gray,step=.5cm,dashed] (-0.5,-.5) grid (3,4);
\draw[->] (-1,0) -- (3.5,0) node[below right] {$x$};
\draw[->] (0,-1) -- (0,4.5) node[left] {$y$};
\draw [->,samples=100,domain=0:2] plot(\x,{(\x)^2});
\draw [->,samples=100,domain=-0.5:2] plot(\x,{exp(-1*(\x))});
\draw [fill=gray,fill opacity=0.2] {[smooth,samples=100,domain=0:0.70346] plot(\x,{exp(-1*(\x))}) } -- {[smooth,samples=100,domain=0.70346:0] plot(\x,{(\x)^2})};
\end{tikzpicture}
\end{document}


I'm getting a strange line going from the point of intersection to the origin, also an error about not being able to parse this coordinate. What am I doing wrong?

-
Your MWE does not compile for me: Package tikz Error: Cannot parse this coordinate. Please correct. –  Peter Grill Feb 29 '12 at 2:53

Your syntax is slightly wrong. You need to supply the plot options to the plot keyword, not to a group delimited by curly braces:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[very thin,color=gray,step=.5cm,dashed] (-0.5,-.5) grid (3,4);
\draw[->] (-1,0) -- (3.5,0) node[below right] {$x$};
\draw[->] (0,-1) -- (0,4.5) node[left] {$y$};
\draw [->,samples=100,domain=0:2] plot(\x,{(\x)^2});
\draw [->,samples=100,domain=-0.5:2] plot(\x,{exp(-1*(\x))});
\draw [fill=gray,fill opacity=0.2] plot [smooth,samples=100,domain=0:0.70346](\x,{exp(-1*(\x))}) -- plot [smooth,samples=100,domain=0.70346:0] (\x,{(\x)^2});
\end{tikzpicture}
\end{document}


-

Jake's answer is clearly the correct way to do this. But here is a hack technique that may be useful for more complicated cases. I first fill in the area under the exponential, which results in an area larger that we want. Then fill in white the area under the quadratic so that the fill appears only in the desired area. I draw the axis, and graphs afterwards so that they are on top.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}

% This overfills the desired area
\draw [draw=white,draw opacity=1,fill=gray,fill opacity=0.2,
smooth,samples=100,domain=0:0.70346]
plot(\x,{exp(-1*(\x))}) -- (0.70346,0) -- (0,0) -- cycle;

% This "unfills" the appropriate area
\draw [draw=white,draw opacity=1,fill=white,fill opacity=1,
smooth,samples=100,domain=0.70346:0]
plot(\x,{(\x)^2}) -- (0.70346,0) -- cycle;

\draw[very thin,color=gray,step=.5cm,dashed] (-0.5,-.5) grid (3,4);
\draw[thick, ->] (-1,0) -- (3.5,0) node[below right] {$x$};
\draw[thick, ->] (0,-1) -- (0,4.5) node[left] {$y$};
\draw [->,samples=100,thick,red,domain=0:2] plot(\x,{(\x)^2});
\draw [->,samples=100,thick,blue,domain=-0.5:2] plot(\x,{exp(-1*(\x))});
\end{tikzpicture}
\end{document}

-