# Difference between two Riemann sums with tkz-fct

tkz-fct is great for plotting Riemann sums, but I don't see how to plot a shaded area of the difference between the sup and inf Riemann sums. I'm particularly interested in a plot with both convex and concave regions. Here's an example:

\documentclass{standalone}
\usepackage{tkz-fct}

\begin{document}
\begin{tikzpicture}
\tkzInit[xmin=0, xmax=10, ymin=-5, ymax=5]
\tkzFct[line width=1pt, color=black, domain=0:10]{\x**(0.5) * sin(\x) + 1}
\tkzDrawRiemannSumSup[interval=0:10, number=15]
\tkzDrawRiemannSumInf[interval=0:10, number=15]
\end{tikzpicture}
\end{document}


How does one go about shading the area between the inf and sup sums, and making the rectangles between the axis and the curve transparent?

## 1 Answer

I believe the following should do the trick. There might be a better approach, but what I've done here is just copied the code for the two Riemann commands you used and tweaked them.

% arara: pdflatex
% arara: pdflatex
% arara: open
\documentclass{article}
\usepackage{tkz-fct}

\makeatletter

\def\aeDrawRiemannSumSupInfDiff{\pgfutil@ifnextchar[{%
\catcode\:=12 \ae@DrawRiemannSumSupInfDiff}{\ae@DrawRiemannSumSupInfDiff[]}}
\def\ae@DrawRiemannSumSupInfDiff[#1]{%
\begingroup
\pgfkeys{%
/tkzriemann/.cd,
interval        = 1:2,
number          = 10,
opacity         = 0.5,
line width      = 1pt}
\pgfqkeys{/tkzriemann}{#1}
\iftkz@init@NO%
\FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
\FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
\else
\FPset{\tkz@x@delta}{0}
\FPset{\tkz@y@delta}{0}%
\fi%
\FPadd\tkz@intwd{\tkz@max}{-\tkz@min}
\FPdiv\tkz@delta{\tkz@intwd}{\tkz@fct@nb}
\FPadd\tkz@fct@nb{\tkz@fct@nb}{-1}

\begin{scope}
\foreach \i in {0,1,...,\tkz@fct@nb}{%
\FPeval\x{(\tkz@min+i*\tkz@delta)}
\FPeval\tkz@fx{\tkzFcta}
\let\tkz@firstimg\tkz@fx
\let\tkz@firstx\x
\FPadd\tkz@next{\x}{\tkz@delta}
\let\tkzFctTmp\tkzFcta
\FPset\x{\tkz@next}
\FPeval\tkz@fxnext{\tkzFcta}
%\ifdim \tkz@fx pt > \tkz@fxnext pt\relax
% \draw[opacity=\tkz@fct@opacity,/tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@firstimg/\tkz@init@ystep );
%\else
%\draw[opacity=\tkz@fct@opacity,/tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@fxnext/\tkz@init@ystep );
% \fi
\draw[opacity=\tkz@fct@opacity,/tkzriemann/.cd,#1] (\tkz@firstx , \tkz@fxnext/\tkz@init@ystep ) rectangle (\tkz@next , \tkz@firstimg/\tkz@init@ystep );
}
\end{scope}
\endgroup
}

\makeatother

\begin{document}

\begin{tikzpicture}
\tkzInit[xmin=0, xmax=10, ymin=-5, ymax=5]
\tkzFct[line width=1pt, color=black, domain=0:10]{\x**(0.5) * sin(\x) + 1}
\aeDrawRiemannSumSupInfDiff[fill=green,interval=0:10, number=15]
%\tkzDrawRiemannSumInf[interval=0:10, number=15]
\end{tikzpicture}

\end{document}
`

• For some reason my code will not draw the graph of the function: even when I copy and paste your code. Nevertheless, this should be the tweak that you need. – A.Ellett Nov 8 '14 at 5:56
• Thanks! That seems exactly right. The graph of the function is being drawn for me. Perhaps it's a version issue? – Dominique Nov 8 '14 at 15:58