1

Here's some TikZ I got off the TikZ site and beginner-hacked. It should be just an x-y graph with a Bezier curve for a function f(x), then points x1 and x2. I want a shaded rectangle x1 -- f(x1) -- f(x2) -- x2 to represent the area under the curve between x1 and x2 (but flat-topped) instead of the shaded triangle above the desired rectangle. The second dot on the Bezier needs to go away too. Specifically, I don't understand what \coordinate (O) at (0,0); nor what \scope[name intersections = {of = x and y, name = i}] \fill[gray!20] (i-1) -- (i-2 |- i-1) -- (i-2) -- cycle; is doing. What's going on with the i variable(?)? Any ideas for a good tutorial on these matters would be appreciated.

\begin{document}
\begin{tikzpicture}[
  scale=2,
  thick,
  >=stealth',
  dot/.style = 
  {draw, ho fill = white, circle, inner sep = 0pt, minimum size = 4pt}
  ]
  \coordinate (O) at (0,0);
  \draw[->] (-0.3,0) -- (7,0) coordinate[label = {below:$x$}] (xmax);7
  \draw[->] (0,-0.3) -- (0,5.5) coordinate[label = {right:$y$}] (ymax);
  \path[name path=x] (0.3,0.5) -- (6.7,4.7);
  \path[name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (6,5)};
  \scope[name intersections = {of = x and y, name = i}]
  \fill[gray!20] (i-1) -- (i-2 |- i-1) -- (i-2) -- cycle;
  \draw[red] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.8) (6,5)};
  \draw (i-1) node[dot, label = {above:$f(x_1)$}] (i-1) {} -- (i-1 |- O) node[dot, label = {below:$x_1$}] {};
  \path (i-2) node[dot] (i-2) {} -- (i-2 |- i-1) node[dot] (i-12) {};
  \draw           (i-12) -- (i-12 |- O) node[dot,
  label = {below:$x_2$}] {};
  \draw (i-1) --  (i-12);
  \path       (i-1 |- O) -- (i-2 |- O);
  \endscope
\end{tikzpicture}
\end{document}
  • Please extend your code to complete document. Also it has error in definition of dot (correct is fill not ho fill). – Zarko Jan 1 '17 at 4:21
  • Please attribute others' code fairly. At least say who and provide a link. How am I supposed to find the original from that description? – cfr Jan 1 '17 at 4:26
  • (i-1) and (i-2) are the intersections of the paths named x and y. Initially. Then they become the names of nodes later on, along with (i-12). See the manual coverage of the intersections library for further information. There are other errors beside the one @Zarko mentioned. What is that stray 7 doing, for example? – cfr Jan 1 '17 at 4:34
5

Edit: My first two assumptions was wrong :(, your problem is more simple ... so I replace the first solution with correct one :) and in addendum left the second one.

enter image description here

Next time, please provide complete document beginning with \documentclass{...} and necessary packages/libraries in preamble (see MWE below). After eliminating error in definition of dot and removing all unnecessary code, the MWE, which generate showed image is:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, backgrounds, intersections, positioning}

\begin{document}
\begin{tikzpicture}[%scale=2,
 node distance = 0pt,
             > = stealth',
          font = \footnotesize,
    dot/.style = {draw, fill = white, circle, inner sep = 0pt, minimum size = 4pt}
                    ]
\coordinate (O);
% axis
\draw[->] (-0.3,0) -- (7,0.0) node[below left] {$x$};
\draw[->] (0,-0.3) -- (0,5.5) node[below left] {$y$};
% curve
\draw[red, name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.7) (6,5)};
% intersection at x_1 and x_2
\path[name path=x] (0.3,0.5) -- (6.7,4.7);
\draw[name intersections={of=x and y, by={f1,f2}},fill=white]
(f1) circle (2pt) node[above] {$f(x_1)$}
(f2) circle (2pt) node[above left] {$f(x_2)$};
% gray area
    \begin{scope}[on background layer]
\draw[fill=gray!20,very thin] (f1) rectangle (f2 |- O);
% x-coordinates
\node[below=of f1 |- O] {$x_1$};   
\node[below=of f2 |- O] {$x_2$};
% x-coordinates
\node[below=of f1 |- O] {$x_1$};
\draw[dashed,very thin]  (f2) -- (f2 |- f1); % <-- delete if it is surplus
    \end{scope}
\end{tikzpicture}
\end{document}

The MWE is structured so it should be easy to determine functions of each code parts. In code is used:

  • coordinate O for determining position for $x_1$ and $x_2$ by (f1 |- O) and (f2 |- O) respectively (I rename intersection names to f1 and f2)
  • by intersections library are calculated intersection of line with name x and curve with name y (read cfr comment above, why you not drawn it?)
  • coordinates of intersections are used for placings labels of intersection as well of labels of x-coordinates on x-axis

I slightly redesign your images (use of dashed lines). Library backgrounds is used that grayed area is on layer behind of the curve,positioning for placement of nodes with labels $x_1$ and $x_2$.

Addendum: if you like to shadow area under curve, like

enter image description here

then the code is slightly more sophisticated. First we draw curve as closed path and than with \clip chop out the part between $x_1$ and $x_2$.

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, backgrounds, intersections}

\begin{document}
\begin{tikzpicture}[%scale=2,
         > = stealth',
      font = \footnotesize,
dot/.style = {draw, fill = white, circle, inner sep = 0pt, minimum size = 4pt}
                    ]
\coordinate (O);
% axis
\draw[->] (-0.3,0) -- (7,0.0) node[below left] {$x$};
\draw[->] (0,-0.3) -- (0,5.5) node[below left] {$y$};
% curve  
\draw[red, name path=y] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.7) (6,5)};
% intersection at x_1 and x_2  
\path[name path=x] (0.3,0.5) -- (6.7,4.7);
\draw[name intersections={of=x and y, by={f1,f2}},fill=white]
(f1) circle (2pt) node[above] {$f(x_1)$}
(f2) circle (2pt) node[above left] {$f(x_2)$};
% gray area
    \begin{scope}[on background layer]
\clip   (f1 |- O) rectangle (f2);
\fill[gray!20] plot[smooth] coordinates {(-0.3,2) (2,1.5) (4,2.7) (6,5)} 
                            |- (O) -| (-0.3,2);
    \end{scope}
\draw[dashed,very thin] (f1 |- O) node[below] {$x_1$} -- (f1)
                        (f2 |- O) node[below] {$x_2$} -- (f2);
\end{tikzpicture}
\end{document}
  • Thanks much for the good tips. However, I'd like the shaded part to be a rectangle with sides $x_1 - f(x_1)$ by $f(x_1) - f(x_2)$, i.e., the rectangle under the curve. (Also, how does one do MathJax/Latex on this forum?) I would show you the original image I'm trying to get, but it got taken down. . . . – 147pm Jan 1 '17 at 16:15
  • sorry, I do not understand where. can you edit your question and add hand drawn sketch, what you like to be shaded? – Zarko Jan 1 '17 at 17:02
  • Check out borgauf.macsyma.org/p0.pdf "Figure 1" – 147pm Jan 1 '17 at 18:31
  • oh, so simple ... :) see edited answer. – Zarko Jan 1 '17 at 18:59

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.