# hobby: using tangency information at a point when drawing paths

Consider the following minimum not working example, where the first figure is drawn using "standard TikZ" methods, and the second using the hobby package:

\documentclass{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{calc, hobby}
\tikzset{tangent/.style = {inagle={(180+#1)},
Hobbyfinish,
designated Hobby path=next,
outangle=#1}}

\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);

\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);

\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);

\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);

\draw[<->] (y) node[left] {$f(x)$} -- (0,0) --  (x) node[below] {$x$};

\draw (sp0) .. controls (csp0) and (cep0).. (ep0);
\draw[dashed] (cep0) -- (csp1);
\draw[dotted] let \p1 = (ep0) in (ep0) -- (0, \y1);
\draw[dotted] let \p1 = (ep0) in (ep0)-- (\x1, 0);

\draw (sp1) .. controls (csp1) and (cep1).. (ep1);
\draw (sp2) .. controls (csp2) and (cep2).. (ep2);

\draw let \p1 = (ep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_0$};
\draw let \p1 = (cep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_1$};
\draw let \p1 = (ep0) in (1pt, \y1) -- (-3pt, \y1) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\caption{Newton's method in action: diagram drawn using standard'' TiKZ methods.}
\label{fig:newton_method_1}
\end{figure}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);

\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);

\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);

\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);

\draw[<->] (y) node[left] {$f(x)$} -- (0,0) --  (x) node[below] {$x$};

% Using \pgfmathanglebetweenpoints to calculate the angle for tangent
% tangent takes a degree unit angle
\draw (ep2) to [curve through ={(sp0) .. ([tangent=\pgfmathanglebetweenpoints{\pgfpointanchor{cep0}{center}}
{\pgfpointanchor{ep0}{center}}]ep0)}] (ep1) ;

\draw[dashed] (cep0) -- (csp1);
\draw[dotted] let \p1 = (ep0) in (ep0) -- (0, \y1);
\draw[dotted] let \p1 = (ep0) in (ep0)-- (\x1, 0);

\draw let \p1 = (ep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_0$};
\draw let \p1 = (cep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_1$};
\draw let \p1 = (ep0) in (1pt, \y1) -- (-3pt, \y1) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\caption{Newton's method in action: diagram drawn using the \texttt{hobby} package.}
\label{fig:newton_method_2}
\end{figure}
\end{document}


I get the following errors:

line 79: Package pgfkeys Error: I do not know the key '/tikz/inagle', to which you past. ...\pgfpointanchor{ep0}{center}}]ep0)}] (ep1)
line 79: Package pgfkeys Error: I do not know the key '/tikz/Hobbyfinish' and I am going to ignore it. Perhaps you misspelled it. ...\pgfpointanchor{ep0}{center}}]ep0)}] (ep1)
line 79: Package pgfkeys Error: I do not know the key '/tikz/outangle', to which you pchor {ep0}{center}}', and I am going to ignore it. Perhaps you misspelled it. ...\pgfpointanchor{ep0}{center}}]ep0)}] (ep1)


I got the tangent code from hobby's documentation -- see page 17. I am trying to set things up so that I can use the tangent command in all my documents, which is why I am trying to use tikzset. That way, I can insert the snippet into my global *.sty file.

How do I sort out the errors?

There are a couple of problems with your code. The first is that you've not copied the code correctly from the example in the documentation. In particular, you've misspelt the keys; they should be:

\tikzset{
tangent/.style = {
in angle={(180+#1)},
Hobby finish,
designated Hobby path=next,
out angle=#1
}
}


Note the spaces.

More seriously, though, is the use of \pgfmathanglebetweenpoints. This doesn't leave the answer in the stream, it stores the result in \pgfmathresult. So to use it as you want you have to calculate it beforehand and only put the answer in the tangent key.

\pgfmathanglebetweenpoints{\pgfpointanchor{cep0}{center}}{\pgfpointanchor{ep0}{center}}
\let\angle=\pgfmathresult
\draw (ep2) to [curve through ={(sp0) .. ([tangent=\angle]ep0)}] (ep1) ;


(The middle line is because \pgfmathresult is easily overwritten.)

In full,

\documentclass{article}
%\url{http://tex.stackexchange.com/q/225878/86}
\usepackage{lmodern}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{calc, hobby}
\tikzset{
tangent/.style = {
in angle={(180+#1)},
Hobby finish,
designated Hobby path=next,
out angle=#1
}
}

\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);

\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);

\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);

\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);

\draw[<->] (y) node[left] {$f(x)$} -- (0,0) --  (x) node[below] {$x$};

\draw (sp0) .. controls (csp0) and (cep0).. (ep0);
\draw[dashed] (cep0) -- (csp1);
\draw[dotted] let \p1 = (ep0) in (ep0) -- (0, \y1);
\draw[dotted] let \p1 = (ep0) in (ep0)-- (\x1, 0);

\draw (sp1) .. controls (csp1) and (cep1).. (ep1);
\draw (sp2) .. controls (csp2) and (cep2).. (ep2);

\draw let \p1 = (ep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_0$};
\draw let \p1 = (cep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_1$};
\draw let \p1 = (ep0) in (1pt, \y1) -- (-3pt, \y1) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\caption{Newton's method in action: diagram drawn using standard'' TiKZ methods.}
\label{fig:newton_method_1}
\end{figure}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\coordinate (y) at (0,3);
\coordinate (x) at (5,0);

\coordinate (sp0) at (1.5, 0);
\coordinate (ep0) at (3.5, 2);
\coordinate (csp0) at (2.5, 1);
\coordinate (cep0) at (2.75, 0);

\coordinate (sp1) at (ep0);
\coordinate (ep1) at (4.5, 3);
\coordinate (csp1) at (4.25, 4);
\coordinate (cep1) at (4.25, 2);

\coordinate (sp2) at (sp0);
\coordinate (ep2) at (0, -1);
\coordinate (csp2) at (0.5, -1);
\coordinate (cep2) at (0.5, -1);

\draw[<->] (y) node[left] {$f(x)$} -- (0,0) --  (x) node[below] {$x$};

% Using \pgfmathanglebetweenpoints to calculate the angle for tangent
% tangent takes a degree unit angle
\pgfmathanglebetweenpoints{\pgfpointanchor{cep0}{center}}{\pgfpointanchor{ep0}{center}}
\let\angle=\pgfmathresult
\draw (ep2) to [curve through ={(sp0) .. ([tangent=\angle]ep0)}] (ep1) ;

\draw[dashed] (cep0) -- (csp1);
\draw[dotted] let \p1 = (ep0) in (ep0) -- (0, \y1);
\draw[dotted] let \p1 = (ep0) in (ep0)-- (\x1, 0);

\draw let \p1 = (ep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_0$};
\draw let \p1 = (cep0) in (\x1,1pt) -- (\x1,-3pt) node[anchor=north] {$x_1$};
\draw let \p1 = (ep0) in (1pt, \y1) -- (-3pt, \y1) node [anchor=east] {$f(x_0)$};
\end{tikzpicture}
\caption{Newton's method in action: diagram drawn using the \texttt{hobby} package.}
\label{fig:newton_method_2}
\end{figure}
\end{document}

• Thanks very much for this answer. It fixed all the errors I was getting. I still don't have output, because I have this error: TeX capacity exceeded, sorry [parameter stack size=10000]. ...h ={(sp0) .. ([tangent=\angle]ep0)}] (ep1), but I am fairly sure its unrelated? – user89 Feb 1 '15 at 0:44
• I don't get that error when I run the above code, so I can't help you there. – Loop Space Feb 1 '15 at 7:30