3

I have the following image and I want to re-create it in LaTeX using Tikz.

enter image description here

So far, I've got something like this.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \draw[->] (-2,0) -- (4,0) node[right] {$x$};
    \draw[->] (0,-1) -- (0,4) node[above] {$y$};
    \draw[scale=1,domain=-2:2.5,smooth,variable=\x,blue] plot ({\x},{(e^\x) / 4});
\end{tikzpicture}

\end{document}

enter image description here

How do I add the delta x and delta y in the visual with color?

3
  • Welcome to TEX.SE. Coordinate system perpendicular in the TikZ and PGF manual should help (syntax with |- and -|). This is page 141 in version 3.1.4b of the manual. For the delta x and delta y, use the node operation. Ugh, this is automatically-generated code... – frougon Feb 1 '20 at 10:37
  • Hi, thank you for the suggestion. I just updated my post. I think I have the lines down, but there is still some discrepancies with where I'm at. – Joseph Konan Feb 1 '20 at 10:40
  • I'm taking a closer look at your reference. It seems to answer my question. I'll post a follow up with the answer. – Joseph Konan Feb 1 '20 at 10:52
4

Here is a way to automate the drawing of these “delta things” for any function and make it parametrizable:

\documentclass[tikz, border=2mm]{standalone}

\tikzset{
  myDelta/.cd,
  xmax/.initial=10, % This has to be set in the picture (one can't guess here)
  ymin/.initial=0,
  text x/.initial={$\delta x$},
  text y/.initial={$\delta y$},
  label style/.style = {color=black, font=\normalfont\scriptsize},
  x label style/.style = {below}, % allows overriding for the x label
  y label style/.style = {right}, % allows overriding for the y label
  line style/.style = {thin, dashed, color=green!60!blue},
  % Convenient shortcuts for appending keys to the previous styles
  labels/.style={/tikz/myDelta/label style/.append style={#1}},
  x labels/.style={/tikz/myDelta/x label style/.append style={#1}},
  y labels/.style={/tikz/myDelta/y label style/.append style={#1}},
  lines/.style={/tikz/myDelta/line style/.append style={#1}},
}

\newcommand{\drawDelta}[4][]{ % <-- Spaces here don't matter because this is
  \begingroup                 % <-- used in the tikzpicture (and not in node
  \tikzset{myDelta/.cd, #1}   % <-- texts).
  \edef\xMax{\pgfkeysvalueof{/tikz/myDelta/xmax}}
  \edef\yMin{\pgfkeysvalueof{/tikz/myDelta/ymin}}
  \pgfmathsetmacro\valuei{#2(#3)}
  \pgfmathsetmacro\valueii{#2(#4)}
  %
  \draw[myDelta/line style]
    ({0.5*((#3) + (#4))}, \yMin) node[myDelta/label style,
                                       myDelta/x label style]
      {\pgfkeysvalueof{/tikz/myDelta/text x}}
    ({#3}, \yMin) -- ({#3}, \valuei) -- (\xMax, \valuei)
    (\xMax, {0.5*(\valuei + \valueii)}) node[myDelta/label style,
                                             myDelta/y label style]
      {\pgfkeysvalueof{/tikz/myDelta/text y}}
    (\xMax, \valueii) -- ({#4}, \valueii) -- ({#4}, \yMin);
  \endgroup
}

\begin{document}

\begin{tikzpicture}[font=\normalfont\scriptsize,
                    declare function={f(\x) = exp(\x)/4;},
                    myDelta/xmax=4, myDelta/ymin=0]
  % Set up a little shortcut
  \edef\xMax{\pgfkeysvalueof{/tikz/myDelta/xmax}}

  \draw[->] (-2,0) -- (\xMax,0) node[right] {$x$};
  \draw[->] (0,-1) -- (0,4) node[above] {$y$};
  \draw[domain=-2:2.5, smooth, variable=\x, red!90!black] plot (\x, {f(\x)});

  % Rely on the default values for the first (delta x, delta y) drawing
  \drawDelta{f}{1.1}{1.2}
  % Override or complement the default values for the second one
  \drawDelta[lines={thin, color=blue!20, solid},
             labels={fill=blue!20, rounded corners},
             x labels={yshift=-2pt},
             text x=$\Delta x$, text y=$\Delta y$] {f}{1.8}{2.3}
\end{tikzpicture}

\end{document}

screenshot

2

Consider the following. Suggested improvements are appreciated.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    \draw[->] (-2,0) -- (4,0) node[right] {$x$};
    \draw[->] (0,-1) -- (0,4) node[above] {$y$};
    \draw[scale=1,domain=-2:2.5,smooth,variable=\x,blue] plot ({\x},{(e^\x) / 4});

    \draw[red] (1.0, 0) -- (1.0, 0.679570457) node[above] {};
    \draw[red] (1.1, 0) -- (1.1, 0.751041506) node[above] {};
    \draw[red] (1.0, 0.679570457) -- (4.0, 0.679570457) node[above] {};
    \draw[red] (1.1, 0.751041506) -- (4.0, 0.751041506) node[above] {};

    \draw[purple] (2.0, 0) -- (2.0, 1.847264025) node[above] {};
    \draw[purple] (2.1, 0) -- (2.1, 2.041542478) node[above] {};
    \draw[purple] (2.0, 1.847264025) -- (4.0, 1.847264025) node[above] {};
    \draw[purple] (2.1, 2.041542478) -- (4.0, 2.041542478) node[above] {};

    \draw[red] (1.05,-0.2) node(dx1) {$\delta x$};
    \draw[purple] (2.05,-0.2) node(dx2) {$\delta x$};

    \draw[red] (4.2, 0.715305982) node(dy1) {$\delta y$};
    \draw[purple] (4.2, 1.94440325) node(dy2) {$\delta y$};

\end{tikzpicture}

\end{document}

enter image description here

1
  • Glad to see you managed to do it yourself. You can check out my answer for ideas. :-) – frougon Feb 2 '20 at 0:33
0

It is probably less complete, it seems simpler to me when using the intersection module,

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{SIunitx}


\begin{document}

\begin{tikzpicture}
    \draw[->] (-2,0) -- (4,0) coordinate(ffh) node[right] {$x$};
    \draw[->] (0,-1) -- (0,4) coordinate(ffv) node[above] {$y$};
    \draw[scale=1,domain=-2:2.5,smooth,variable=\x,blue,name path=Fx] plot ({\x},{(e^\x) / 4});

\def\dx{0.2}

\draw[dashed,red,thin,name path=V1] (0.5,0)coordinate(h1) --++(0,2);
\draw[dashed,red,thin,name path=V2] ($(h1)+(\dx,0)$) coordinate(h2) --++(0,2);
\draw[red]  ($(h1)+(0,-0.5em)$) -- node[below]{$\delta x$}($(h2)+(0,-0.5em)$) ;
\coordinate[name intersections={of=Fx and V1, by=i1}];
\draw (i1) --(i1-|ffh)coordinate(v1);
\coordinate[name intersections={of=Fx and V2, by=i2}];
\draw (i2) --(i2-|ffh)coordinate(v2);
\draw[red] (v1) --node[right]{$\delta y$} (v2);

\draw[dashed,red,thin,name path=V1] (1.5,0)coordinate(h1) --++(0,2);
\draw[dashed,red,thin,name path=V2] ($(h1)+(\dx,0)$) coordinate(h2) --++(0,2);
\draw[red]  ($(h1)+(0,-0.5em)$) -- node[below]{$\delta x$}($(h2)+(0,-0.5em)$) ;
\coordinate[name intersections={of=Fx and V1, by=i1}];
\draw (i1) --(i1-|ffh)coordinate(v1);
\coordinate[name intersections={of=Fx and V2, by=i2}];
\draw (i2) --(i2-|ffh)coordinate(v2);
\draw[red] (v1) --node[right]{$\delta y$} (v2);


\end{tikzpicture}

\end{document}

enter image description here

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.