9

I have already emailed the author Alain Matthes regarding this question. But for the benefit of those who might be interested, and since Alain is also a very active member in this site, I am also posting the question here.

In my attempt to answer the question Definite Integral Animation Examples, I borrowed Alain's code in his answer in Tikz-PGF: Draw integral test plot. The code works fine but shows an unexpected behavior when passing the value 1 to the number option as can be seen in the figure.

enter image description here

Here, there should be no rectangle after x=8. However, the code works fine when number is set greater than 1.

Here is the complete code for the figure.

\documentclass[tikz]{standalone}
\usepackage{tkz-fct}   

\begin{document}
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmax=8,ymax=4]
\tkzAxeXY[ticks=false]
\tkzGrid   
\tkzFct[color = red, domain =0.125:8]{4./x}
\tkzDrawRiemannSumInf[fill=green!60,
                     opacity=.2,
                     color=green,
                     line width=1pt,
                     interval=1:8,
                     number=1]
 %\foreach \x/\t in {1.5/$a_1$,2.5/$a_2$,3.5/$a_3$,7.5/$a_7$}
 %\node[green!50!black] at (\x,{4/(\x+1)-0.25}){\t};
\end{tikzpicture}
\end{document}

Can somebody verify this and give a workaround?

hpesoj626
  • 17,282

1 Answers1

8

It looks like it's due to the \foreach list having {0,1,...,x} and hence forcing 1 to be processed even if x is zero, instead of having {0,...,x} hence spining only once. And I guess there is a small typo with \i where it is i in the code. I'm not sure about the second point.


EDIT: Indeed it's not a typo but a rather peculiar syntax. From fp readme file.

ATTENTION: Do not use macro names with \ for its own. Use only the name or the macro surrounded by (, and ) instead, i.e. do not write \value{} but value or (\value). This is needed to avoid problems with a prefix "-" of numbers.


Here is the fixed version.

\documentclass[tikz]{standalone}
\usepackage{tkz-fct}   
\makeatletter
\def\tkz@DrawRiemannSumInf[#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,...,\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  
   }    
   \end{scope}  
\endgroup
} 

\makeatother



\begin{document}
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmax=8,ymax=4]
\tkzAxeXY[ticks=false]
\tkzGrid   
\tkzFct[color = red, domain =0.125:8]{4./x}
\tkzDrawRiemannSumInf[fill=green!60,
                     opacity=.2,
                     color=green,
                     number=1,
                     line width=1pt,
                     interval=1:8,
                    ]
 %\foreach \x/\t in {1.5/$a_1$,2.5/$a_2$,3.5/$a_3$,7.5/$a_7$}
 %\node[green!50!black] at (\x,{4/(\x+1)-0.25}){\t};
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807