7

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}

enter image description here

3
  • 1
    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? 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. Mar 4 '13 at 18:59
13

enter image description here

\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.

7
  • @user2553 Thanks for your reply, but I want to color the region like the picture above (I added a picture). 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? Mar 4 '13 at 19:51
  • 1
    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. Mar 4 '13 at 20:02

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.