# A "to[out=x,in=y]" curve and a "plot(\x,{cos(\x)})" in tikz in the same draw command

I am trying to draw a curve followed by a plot in the same draw command. However, this doesn't work even though a line does. The following code helps understand the issue:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} %This works!
\draw[domain=-1:1] plot(\x,{cos(\x*100)}) to[out=-60,in=180] (2,2);
\end{tikzpicture}

\begin{tikzpicture} %This does too!
\draw[domain=-1:1] (-2,-2) -- plot(\x,{cos(\x*100)}) to[out=-60,in=180] (2,2);
\end{tikzpicture}

\begin{tikzpicture} %This gives errors :( Why?
\draw[domain=-1:1] (-2,-2) to[out=40,in=30] plot(\x,{cos(\x*100)}) to[out=-60,in=180] (2,2);
\end{tikzpicture}

\end{document}


Could someone explain why this does not work and how I could accomplish this?

The to operation is very different from --. From the manual section 14.13 you can see that to installs, by default, a path of the form:

--(\tikztotarget) \tikztonodes


For curved lines the pattern is similar and still uses the macros \tikztotarget, \tikztosource and \tikztotarget, which are setup by the code parsing the to operation. From the manual:

The to operation is always followed by a ⟨coordinate⟩

This is so that \tikztotarget can be appropriately setup.

From section 22.2 one learns that --plot is a specific variant of plot which is defined explicitly. Since there is not special to plot operation, the to operation simply cannot understand a direct plot after itself.

The solution is then to manually compute the initial point of the plot with for example \pgfmath and put the target coordinate explicitly after the to operation, before the plot:

\def\startPlotX{-1}
\draw[domain=\startPlotX:1]
(-2,-2)
to[out=40,in=30] (\startPlotX,{cos(\startPlotX*100)})
plot(\x,{cos(\x*100)})
to[out=-60,in=180] (2,2);

• Hmm, interesting. But why does it work then in the case of (-2,-2) -- plot? I thought the (-2,-2) gives the starting point and the begining of the domain used for \x gives the end point, from which the plot begins. What I don't understand is, why does it not work for to when it works for --. Nov 30, 2017 at 14:43
• Sorry, rushed answer, hopefully better now. Nov 30, 2017 at 15:13
• @learning2code One possibility for calculating the initial point: \begin{tikzpicture}[declare function={f(\x)=cos(\x*100);}] \draw[domain=-1:1] (-2,-2) to[out=40,in=30] (-1,{f(-1)}) plot(\x,{f(\x)}) to[out=-60,in=180] (2,2); \end{tikzpicture} (Or use cos(100*\x) directly, instead of defining f(\x).) Nov 30, 2017 at 15:20
• @TorbjørnT. and @Bordaigorl Thank you! (-2,-2)to[out=40,in=30] (-1,{cos(-1*100)})plot also works! Nov 30, 2017 at 15:41