Example: Difference quotient

Published 2010-01-19 | Author: Peter Kint

Note: Requires the development version of PGF due to the use of the spy library.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Difference quotient

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% DIFFERENCE QUOTIENT                            %
% Author: Peter Kint                             %
% Date: 28 dec 2009                              %
% Using some PGF/TikZ features like              %
%      'layer'                                   %
%      'decorate'                                % 
%       and the wonderful new 'spy'-feature      % 
% Note: This example requires PGF > 2.0 or a recent
% development version of PGF.
% declare extra background layer, (main = default)
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
    [line cap=round,line join=round,x=2cm,y=2cm,
     %using the 'spy' to magnify a part of the picture
     spy using outlines={rectangle,lens={scale=3}, size=8cm, connect spies},
     %using the decoration 'brace' (=a curly brace as path replacement)
%main layer
%creating the grid
  \draw [color=lightgray,dash pattern=on 1pt off 1pt, xstep=2cm,ystep=2cm]
                                                 (-0.1,-0.1) grid (2.3,2.3);
%creating the ticks and xy-axis nodes
  \draw[-latex,color=darkgray,thin] (-0.2,0) -- (2.4,0);
   \foreach \x in {,1,2}
   \draw[shift={(\x,0)},color=darkgray,thin] (0pt,1pt) -- (0pt,-1pt)
                                   node[below] {\footnotesize $\x$};
  \draw[-latex,color=darkgray] (0,-0.2) -- (0,2.6);
      \foreach \y in {,1,2}
      \draw[shift={(0,\y)},color=darkgray,thin] (1pt,0pt) -- (-1pt,0pt)
                                    node[left] {\footnotesize $\y$};
  \draw[color=black] (0pt,-8pt) node[left] {\footnotesize $0$};
%some function
  \draw [blue] (0.22,0.67)--(0.58,0.42)
         node [midway, above right]{\tiny$\frac{\Delta y}{\Delta x}$};
  \draw [darkgray,ultra thin] (0,0.67)-- (0.22,0.67);
  \draw [darkgray,ultra thin] (0,0.42)-- (0.58,0.42);
  \draw [darkgray,ultra thin] (0.22,0.67)-- (0.22,0.0);
  \draw [darkgray,ultra thin] (0.58,0.42)-- (0.58,0.0);
%creating the curly braces with decorate
  \draw [decorate,color=red] (0,0.42) -- (0,0.67)
   node [midway,anchor=east,inner sep=2pt, outer sep=1pt]{\tiny$\Delta y$};
  \draw [decorate,color=green!80!black] (0.58,0.0)--(0.22,0.0)
   node [midway,anchor=north,inner sep=1pt, outer sep=1pt]{\tiny$\Delta x$};
%using the 'spy' to magnify a piece of the picture
  \spy [blue] on (0.4,0.4)
             in node [left] at (6.5,1);
%filling the background layer (all previous commands belong to main layer)
  \begin{pgfonlayer}{background layer}
   \fill[yellow!20] (-0.5,-1.5) rectangle (7,3.75);
   \node at (1,3)[blue]{\Large$ \mathrm{Difference \, Quotient}$};


  • #1 oleg, January 21, 2010 at 1:39 p.m.

    unfortunately this example don't work

    ! I can't find file tikzlibraryspy.code.tex'. <argument> ...nput tikzlibrary\pgf@temp .code.tex \catcode\@ =\csname tikz... l.29 \usetikzlibrary{spy}

    Please type another input file name: ! Emergency stop.

    Could you help me?

  • #2 Kjell Magne Fauske, January 21, 2010 at 2:10 p.m.

    As stated in the example description, this example requires a recent development version of PGF. It will not compile with PGF 2.0. You can find recent snapshots of PGF at http://www.texample.net/tikz/builds/

  • #3 Hugues VERMEIREN, January 27, 2010 at 11:55 a.m.

    This is a deep PGF improvement! I had just realized a figure with a magnifying glass in a particular case (good exercise anyway) and now I see PGF will do it all alone with simple and elegant commands. Quite impressive.

  • #4 Damoun, March 5, 2010 at 4:01 p.m.

    Hi there,

    I get the following error when running your code:

    ! Package pgfkeys Error: I do not know the key '/tikz/path picture' and I am go ing to ignore it. Perhaps you misspelled it.

    I already have the latest pgf, and can't seem to find what causes the error...


  • #5 Rafael, May 13, 2010 at 10:06 a.m.

    I have the same problem as Damoun. Do I need some update or anything else? Thanks

  • #6 alfC, June 11, 2010 at 11:01 p.m.

    do you know how to make the background rectangle to adjust to the picture? also is there a way to add "margin" or a space to the picture?


  • #7 skarred, June 30, 2010 at 8:17 a.m.

    Hello Damoun,

    Regarding your post on March 5, 2010 at 4:01 p.m., I encountered the same problem today.

    Failed Attempt to Get around the Error

    I saw a few posts on different sites regarding the Package pgfkeys Error: I do not know the key '/tikz/path picture' and I am go ing to ignore it. Perhaps you misspelled it. problem, bit I dod not find any solutions (they might be out there, I just did not did not find any), I tried

    Road to a fix

    1. upgrading to v 1.67 2010/03/23 17:02:29 but got lots of errors with the pgfmath... files.
    2. read through Wright and Feuersanger's article Implementing key-value input: an introduction (bibliographic info and article here), I got some idea of what to look for in v 1.67 that was missing in v 1.27 installed in Ubuntu Karmic. It boils down to making some changes in tikz.code.tex.
    3. replaced my installed tikz.code.tex v 1.27 with v 1.67 not expecting much, but it was easy enough to try
    4. I made some changes in my working file (v 1.27) following the code in (v 1.67). I got the example working and plan to use this idea in an article...

    Here are the relevant files

    1. The original v 1.27 file
    2. The original v 1.27 file
    3. The Modified File. This is a copy of the one installed in /usr/share/texmf/tex/generic/pgf/frontendlayer/tikz on my machine
    4. The (handmade) diff file

    The Mandatory Disclaimer

    I do not know if I broke anything else in pgf/tikz at this time -- I would keep backups of original (or be prepared to reinstall your working version) etc. etc.

    Or just sit tight for a couple of days so that I can let you know if ....

    I hope this helps, skarred

  • #8 Mark, September 18, 2010 at 3:01 p.m.

    Hi Thanks for the comment, i tried everything written here and still getting this error "! I can't find file tikzlibraryspy.code.tex'. <argument> ...nput tikzlibrary\pgf@temp .code.tex \catcode\@ =\csname tikz... l.29 \usetikzlibrary{spy}

    Please type another input file name: "

    can someone help me with this

    I am using winedt as tex compiler and Miktek 2.9. I don't knowwhat version of Pgf i have. How can i check and how can i get the development version of pgf.

    with best wishes

  • #9 Lara, May 31, 2013 at 7:28 a.m.

    Great post.

Adding comments is currently not enabled.

Scientific and technical areas


LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

Creative Commons License