2

I'm trying to combine two different suggestions people made about an earlier question I asked. I'm getting a bunch of errors, but I'm not sure what the problem is. The code is:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}   
\usetikzlibrary{calc}
\def\mycurve#1{(0,0) to[out=70, in=180] (5,1.8)#1}
\begin{document}
  \begin{tikzpicture}
\clip (-0.1,-0.1) rectangle (5,3);                                   
\draw[help lines,->] (0,0) -- (4.2,0);                               
\draw[help lines,->] (0,0) -- (0,3);   % draw axis lines             
\draw[gray,dashed] (0,2) -- (4.2,2);   % draw asymptote              
\draw[domain=0.5:4.6,very thick,red,->,samples=400] \mycurve;% draw plot

\foreach \x  in {0.25,0.5,...,4}                                                
 {
    \draw[help lines,->] (\x,0) -- ($(\x,\mycurve)-(0,0.6pt)$);
  }

\end{tikzpicture}                                                          
\end{document}

I think the problem is at the end, where I try to make the vertical lines relative to the curve; it works if the curve is defined as a hyperbola, as in \def\mycurve#1{{1/(-(\x+#1))+2}}, but if I define an arbitrary curve it doesn't. Can I not do that, or did I make a code mistake somewhere?

crmdgn
  • 2,628
  • 1
    You have defined \mycurve with an argument, but don't supply one, so #1 becomes the closing parenthesis ), messing everything up. – egreg Jun 24 '14 at 14:46
  • \mycurve takes an argument but you use it without one like in \mycurve)...here TeX will think ) is your argument and TikZ will not find the closing parens – Bordaigorl Jun 24 '14 at 14:46
  • @egreg you bit me to it =) \mycurve; is problematic too – Bordaigorl Jun 24 '14 at 14:47
  • Moreover $(\x,(0,0) to[out=70, in=180] (5,1.8))-(0,0.6pt)$) is not legal. – egreg Jun 24 '14 at 14:54
  • Related to your last paragraph: apparently, \mycurve does not define a "curve", but rather a numerical function of \x and an arbitrary argument. This numerical function can be understood by \draw for a plot, and in a coordinate definition (if there is a \x in the context). But indeed you cannot replace it with an "arbitrary curve" which will not make sense in either of these contexts. – T. Verron Jun 24 '14 at 14:59
  • So, to make sure I've got all this: it sounds like I can either define the curve as (0,0) to[out=70, in=180] (5,1.8) or define the vertical arrows in relation to the curve, but I can't do both. Is that right? – crmdgn Jun 24 '14 at 15:22
  • PS @egreg where's the illegality in that line you quoted? – crmdgn Jun 24 '14 at 15:23
  • 1
    @crmdgn I tried it and it produces tons of errors. – egreg Jun 24 '14 at 15:27

1 Answers1

1

TikZ checks the to-do list on the path by reading the characters in the stream, it is almost always problematic to have a macro sitting by itself. Here, you have intertwined a few problems;

When you define \mycurve#1{} you define a macro which will keep looking for an argument until it hits something that calms TeX down. In pacman notation

\mycurveᗧ••••••••ᗣ{other definition stuff} 

it will keep eating until hits one of those ghost thingies. If the stuff is not grouped such as \mycurve{lots of things that will replace #1} then it will get stuck in the first ). I'm sure there is a duplicate somewhere to explain it better so I'm skipping that. As an example, you are delaying the ; with your definition;

\mycurve;

since #1 is replaced by ; which is the same thing if you define your macro with

\def\mycurve{same definition without #1}

However, then you use

\mycurve)

then it delays the parenthesis and you are putting a to path inside a coordinate declaration. You can simply try

\draw (0,0) -- (1,to (3,5));

That's not allowed and also I don't even know what it might mean. But!

When you define it as

\def\mycurve#1{{1/(-(\x+#1))+2}}

then TikZ is smart enough to understand that it might be a calculation going on there which would lead to a number and it would use it for the y entry of that coordinate. However you still supply a parenthesis to \mycurve, so it becomes

{1/(-(\x+)))+2}

and it shouldn't work. So I don't think that's what you have used previously.

percusse
  • 157,807