2

Consider the source code for the "difference quotient" example presented on TeXample:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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.
\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usetikzlibrary{spy}
\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations}
\date{}
\begin{document}
\pagestyle{empty}
%
% declare extra background layer, (main = default)
\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}
\definecolor{darkgray}{rgb}{0.25,0.25,0.25}
\definecolor{lightgray}{rgb}{0.75,0.75,0.75}
%
\begin{tikzpicture}
    [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)
     decoration={brace,amplitude=2pt}]
%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[smooth,samples=1000,domain=0.0:2.2]
   plot(\x,{8*\x-32.4*\x^2+53.48*\x^3-42.11*\x^4+17.594*\x^5
                            -3.99*\x^6+0.465713*\x^7-0.0217374*\x^8});
  \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}$};
  \end{pgfonlayer}
\end{tikzpicture}
\end{document}

It compiles beautifully, and results in the following output:

enter image description here

I tried to "frankenstein" the code, and use particular parts of it. Specifically:

\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$};

...but I noticed it doesn't work the same way as in the example "right out of the box" in my code, so I am missing some detail in the example. For instance, consider this tiny example:

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{decorations}

\begin{document}

\begin{tikzpicture}
    \draw [decorate,decoration={brace,amplitude=10pt}] (1,0)--(10,0)
    node [midway,anchor=north,inner sep=10pt, outer sep=10pt]{$\Delta x$};
\end{tikzpicture}

\end{document}

It just steals the brace code from the "difference quotient" example, and blows up the dimensions for a nice big picture. Here is the output:

enter image description here

Note that the output is the mirror of the "difference quotient" example's x-axis brace, but the code isn't different. If I add the mirror tag, it looks the same, then:

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{decorations}

\begin{document}

\begin{tikzpicture}
    \draw [decorate,decoration={brace,amplitude=10pt,mirror}] (1,0)--(10,0)
    node [midway,anchor=north,inner sep=10pt, outer sep=10pt]{$\Delta x$};
\end{tikzpicture}

\end{document}

enter image description here

Why can the "difference quotient" example get away with not using the mirror tag?

  • \cases pairs a \left\{ with a \right.. Perhaps this is similar. – John Kormylo May 18 '15 at 19:07
  • 3
    If you draw from (a) to (b) that's one path and if you draw from (b) to (a) that's another from the decoration point of view. draw from 10 to 0. – percusse May 18 '15 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.