# How to color this region produced by tikz?

I'm using xelatex and I want to color the region enclosed by y=f(x), y=g(x) and the lines x=a and x=b in the code below (just like the picture below). How can I do that?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}
\draw [->,>=triangle 45] (-2,0) -- (5,0) node [right] {$x$};
\draw [->,>=triangle 45] (0,-2.2) -- (0,3.2) node [above] {$y$};
\draw[very thick] (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) ;
\draw[very thick] (-1.5,-1.5) to [out=350,in=240] (4.5,1) ;
\draw[very thick] (4.2,0)--(4.2,2.2);
\node at (4.2,0) [below] {$b$};
\draw[very thick] (-1,0)--(-1,-1.55);
\node at (-1,0) [above] {$a$};
\node at (1.5,2.2) [above] {$y=f(x)$};
\node at (2,-1.5) [right] {$y=g(x)$};
\end{tikzpicture}

\end{document}


-
Have you checked the results for [tikz-pgf] color region? There might be some questions and answers that will help you. Furthermore, have you already tried any attempt to fill the region? –  Qrrbrbirlbel Mar 4 '13 at 18:48
Do you know the equations for f(x) and g(x) (or could you make one up)? –  Jake Mar 4 '13 at 18:51
@Jake No. I don't have their formulas. –  Vahid Damanafshan Mar 4 '13 at 18:59

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}
\begin{scope}
\clip(-1,-5)rectangle(4.2,5);
\clip (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) --++(0,-5)--(-1,-5)--cycle;
\fill[red] (-1.5,-1.5) to [out=350,in=240] (4.5,1)--++(0,5)--(-1,5)--cycle;
\end{scope}
\draw [->,>=triangle 45] (-2,0) -- (5,0) node [right] {$x$};
\draw [->,>=triangle 45] (0,-2.2) -- (0,3.2) node [above] {$y$};
\draw[very thick] (-1.5,.5) to [out=300,in=180] (-.5,-.5)
to [out=0,in=180] (3,2.5)
to [out=360,in=140] (4.5,2) ;
\draw[very thick] (-1.5,-1.5) to [out=350,in=240] (4.5,1) ;
\draw[very thick] (4.2,0)--(4.2,2.2);
\node at (4.2,0) [below] {$b$};
\draw[very thick] (-1,0)--(-1,-1.55);
\node at (-1,0) [above] {$a$};
\node at (1.5,2.2) [above] {$y=f(x)$};
\node at (2,-1.5) [right] {$y=g(x)$};
\end{tikzpicture}

\end{document}


You can make the code even better by using named coordinates and reuse of paths.

I still haven't figured out an automatic way to determine the extremal values of the curves, so the path appendices

--++(0,-5)--(-1,-5)--cycle

and

--++(0,5)--(-1,5)--cycle

can be replaced by automatically correct values.

-
@user2553 Thanks for your reply, but I want to color the region like the picture above (I added a picture). –  Vahid Damanafshan Mar 4 '13 at 19:37
Sorry, there was some copyediting error. Corrected the answer. –  Toscho Mar 4 '13 at 19:41
@user2553 thanks for your update. Could you please explain what scope and clip do? –  Vahid Damanafshan Mar 4 '13 at 19:51
scope defines a "subenvironment" that is usually uses to set tikz parameters for several \draw, etc. In this example, it's only used to restrict the clip. Without it, the clip would affect the whole tikzpicture after the clip-command. clip defines a region outside of which nothing will be drawn. In my answer, the first clip restricts the drawing area to -1≤x≤4.2. The second clip restricts the drawing area to y≤f(x). –  Toscho Mar 4 '13 at 19:55
@user2553 A problem: when using your code, I get extra spaces above and below the graph, but If I delete \begin{scope} \clip(-1,-5)rectangle(4.2,5); \clip (-1.5,.5) to [out=300,in=180] (-.5,-.5) to [out=0,in=180] (3,2.5) to [out=360,in=140] (4.5,2) --++(0,-5)--(-1,-5)--cycle; \fill[red] (-1.5,-1.5) to [out=350,in=240] (4.5,1)--++(0,5)--(-1,5)--cycle; \end{scope}, everything will be OK. I'm aware of \vspace{...}, but I want to know the correct way to solve this issue. –  Vahid Damanafshan Mar 4 '13 at 20:02