I want to make a graph of the function 1/x with the area under the curve shaded, but I have absolutely no idea how to do that since the curve isn't continuous. Optimally, the negative area and positive area would be shaded with different colors. I would also like there to be a label that says f(x)=1/x. I made this image in Desmos to show an example of the image I'm trying to get: graph of the area under the curve f(x)=1/x


2 Answers 2


You are a new contributor so it would be better if I direct you to a more professional tool than tkz-fct. My package is only useful for small graphs. I think pgfplots is what you need. I think you will get answers with this one.

To be complete with tkz-fct you need to install gnuplot. This is what does the calculations. Otherwise you can do what you need with just TikZ.

updated : with xstep different from 1 use x and not x


    \tkzFct[color=blue, domain=0:4]{1./\x}
    \tkzFct[draw=none, domain=0:4]{0}
    \tkzDrawAreafg[color=blue!10,domain = 0.01:4]
    \tkzFct[draw=none, domain=-4:0]{0}
    \tkzFct[color=red ,domain=-4:0]{1./\x}  
  \tkzDrawAreafg[color=red!20,domain = -4:-0.01]

enter image description here

Complement :

You can mix tkz-fct and TikZ


\shade[top color=gray!80,bottom color=gray!20] (1,0)%
    plot[id=ln,domain=1:2.718] function{log(x)} |-(1,0); 
\draw[color=blue] plot[id=ln,domain=0.2:4,samples=200]function{log(x)};
\tkzText[draw,color= black,fill=brown!50](2,-1)%
    {$\mathcal{A} = \int_1^{\text{e}}\ln(x)\text{d}x =\big[x\ln(x)\big]_{1}^{\text{e}} = \text{e}$}
\tkzText[draw,color= black,fill=brown!50](2,0.3){$\mathcal{A}$}

enter image description here

  • Sorry, I'm completely new to this. I have gnuplot installed but I get an error in my log file that says runsystem(gnuplot "Cauchy Principal Value.tkzfonct.gnuplot")...disabled (restri cted). so I don't think it is connecting to TeXShop properly. How do I solve this?
    – mlxd41
    Jan 18 at 7:41
  • 1
    @mlxd41 1. Go to the Preferences. 2. Go to Tab "Engine" 3. Go to Field "pdfTeX" 4. In the LaTeX Input Field add --shell-escape at the end. Something like this :pdflatex --file-line-error --synctex=1 --shell-escape Jan 18 at 8:13
  • 1
    @mlxd41 It is necessary to authorize the call to gnuplot. It's possible that you will have an alert then you must agree to the shell-escape Jan 18 at 8:16
  • It worked, though I had to change the page from the default light grey color to white so that you can't see the white highlight box around each number on the graph axis. Thanks for your help!
    – mlxd41
    Jan 18 at 15:36
  • 1
    @mlxd41 Sorry I'm too busy with geometry and I don't use my old packages much anymore so I forget ... When you use xstep you have to write \x and not x. Jan 18 at 16:29

Here is a plain Asymptote way that you can compile online. Plain things are suitable for beginners. Here is a link on overleaf. If you want to compile on desktops, make sure having Asymptote installed.

enter image description here

// copy http://asymptote.ualberta.ca/ and click Run
usepackage("amsmath");  // for \dfrac
import graph;
import math; // for grid
real f(real x){return 1/x;}
int a=4;

real s=.1;
path p=graph(f,s,a);
path q=graph(f,-a,-s);
pen penp=magenta, penq=deepcyan;


label(Label("p.v. $\displaystyle\int_{-a}^a\dfrac{1}{x}\,dx=0$",FillDraw(yellow,black)),(-2,2));

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.