1

Can anyone explain to me why the output of the following code changes when I remove +\x1 ?

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{calc}

\begin{tikzpicture}
  \draw[->] (-1,0) -- (1,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,5) node[above] {$y$};
  \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] 
    let 
        \p1 = (0,0)
    in
        plot ({\f+\x1},{\f*\f});
\end{tikzpicture}

\end{document}

In my understanding, \x1should contain the first coordinate of \p1, which is zero, so adding it should have no effect on the graph?

3

There is nothing too weird going on. \x1 has the value 0pt in your example. Let us try

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{calc}

\begin{tikzpicture}
  \draw[->] (-1,0) -- (1,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,5) node[above] {$y$};
  \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] 
    let 
        \p1 = (0,0)
    in 
        plot ({\f+0pt},{\f*\f});
\end{tikzpicture}
\end{document}

enter image description here

and compare it to

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{calc}

\begin{tikzpicture}
  \draw[->] (-1,0) -- (1,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,5) node[above] {$y$};
  \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] 
    let 
        \p1 = (0,0)
    in 
        plot ({\f*1cm+\x1},{\f*\f});
\end{tikzpicture}
\end{document}

enter image description here

which has the result which you may have expected. The issue is that, since you add a number to something that carries the units pt, TikZ tries to make sense of it by assuming that everything carries these units unless you say otherwise.

So if you have a nontrivial x component you may do

\documentclass[tikz]{standalone}
\begin{document}

\usetikzlibrary{calc}

\begin{tikzpicture}
  \draw[->] (-1,0) -- (1,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,5) node[above] {$y$};
  \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] 
    let 
        \p1 = (0.5,0),\n1={\x1/1pt}
    in 
        plot ({\f*1cm+\n1},{\f*\f});
\end{tikzpicture}
\end{document}

enter image description here

Let me finally remark that there are really weird things

\documentclass[tikz]{standalone}
\begin{document}

\usetikzlibrary{calc}

\begin{tikzpicture}
  \draw[->] (-1,0) -- (1,0) node[right] {$x$};
  \draw[->] (0,0) -- (0,5) node[above] {$y$};
  \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] 
  let \p1 = (0.1,0),\n1={(\x1/1pt)*1cm} 
  in \pgfextra{\typeout{\x1,\n1}} 
  plot ({\f+\n1},{\f*\f});
\end{tikzpicture}
\end{document}

enter image description here

Naively one might expect that this yields the same result as the one above, but it does not. However, TikZ still wants to take everything in pt, and the conversion multiplies the x coordinate by another factor as can be seen from the fact that the output from the \typeout is 2.84544pt,80.96068pt, i.e. there is another multiplication by 28.4544, the ratio between cm and pt.

Bottom-line: If you mix units, make sure to append the desired units everywhere if they do not happen to be pt, which is what TikZ uses internally.

4
  • Thanks for that explanation! – But according to that logic, shouldn't ({\f+\x1*1cm/1pt},{\f*\f}) also recover the original result? That somehow doesn't happen (and in my original example, the \x1 enters in the exponent, so I really need to get the `\x1' into the standard unit...)
    – mimuller
    Oct 12 '18 at 12:44
  • 1
    @ItsmeMimi On my machine {\f*1cm+\x1*1cm/1pt} yields the second output, i.e. the wide parabola. If you do ``{\f+\x1*1cm/1pt}, you get the narrow parabola. IMHO this is all consistent with my explanation. All you would have to do is to multiply in your expression \f` by 1cm. It is, however, true that you can get really weird effects if you overdo it with these conversions, try e.g. \draw[scale=0.5,domain=-3:3,smooth,variable=\f,blue] let \p1 = (0.1,0),\n1={(\x1/1pt)*1cm} in plot ({\f+\n1},{\f*\f});.
    – user121799
    Oct 12 '18 at 13:27
  • 1
    Shouldn't the \x1*1cm/1pt however carry the unit cm, and therefore TikZ should assume that everything is in the cm unit? – I will mark your answer as correct, because you definitely solved the original question. Unfortunately, my actual plot is more like your comment example, and I just can't get rid of the weird behavior there. :( Do you understand why it seems to shift the plot by 3 horizontally, rather than 0.1? The \f*1cm trick recovers the wide parabola, but the shift is still way too big... rounding errors??
    – mimuller
    Oct 12 '18 at 13:41
  • 1
    @ItsmeMimi I expanded the answer a bit. The upshot is that, yes, TikZ seems to overdo it a bit with adding pt wherever it is not sure. (Luckily you are careful enough to call the plot variable \f rather than \x because this is another source of confusion. In plots, I usually use the \n1=.... syntax to make use of coordinates that are read out from calc elements, but I agree that here this is not necessary.)
    – user121799
    Oct 12 '18 at 13:59

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.