Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

It is claimed by Marc van Dongen at Use TikZ foreach variable in node that \x is a "TiKZ primitive". Does TikZ have primitives? I can't understand why on earth any package would make \x a primitive, and I assume that outside TeX engines/platforms only LuaTeX can make a package/user command equivalent to a primitive. Please can anyone tell me the meaning of the \x primitive? I can't see it defined outside or within tikzpicture!

EDIT

I have now seen what is going on. Although TikZ has no primitives, Marc van Dongen has a point. From cjorssen's example,

\draw plot[samples=10,domain=-5:5] (\y,{(\y)^2})

fails, while

\draw plot[variable=\y,samples=10,domain=-5:5] (\y,{(\y)^2})

works. In my humble opinion, PGF shouldn't have used a generic macro like \x as a default scratch macro, even in a local group or with a restored stack. It may be convenient for users, but, as we've seen, it is not the best choice of a scratch variable.

share|improve this question
    
As far as I know it's used simply as a scratch macro like \next. –  egreg Jul 20 '12 at 12:37
    
"Primitive" just isn't the word that should be used in this context (as you explained yourself). –  Hendrik Vogt Jul 20 '12 at 12:40
    
You can do \draw[domain=-1:1] plot (\x, {exp(\x)}); in tikz. –  canaaerus Jul 20 '12 at 12:40
1  
@canaaerus This doesn't mean that \x has a constant definition outside that context. If you add \show\x;, the answer will be \x=undefined. –  egreg Jul 20 '12 at 12:46
    
I was not careful when I wrote redefining \x. In my more recent comment I clarify my comment why it is not clever to rely on a globally defined \x in tikz/pgfplots. The reason is that they may redefine \x as part of a let operation. I also give an example. Hope this clarifies my comment. I still believe using \x in your example is flawed. –  Marc van Dongen Jul 22 '12 at 15:51

1 Answer 1

up vote 5 down vote accepted

\x is not a tikz "primitive" as far as I understand. It can be used in the plot operation but it is behind the scenes used by \foreach. See in the following example: the two \draw commands do more or less the same (see lines 388- in pgfmoduleplot.code.tex and lines 2899- in tikz.code.tex).

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw plot[samples=10,domain=-5:5] (\x,{(\x)^2});
  \draw[red] (-5,{(-5)^2}) \foreach \x in {-4,...,5} {-- (\x,{(\x)^2})};
\end{tikzpicture}
\end{document}

The \foreach variable (here \x) is group protected so the fact that \x would be defined elsewhere is not a problem here.

I'd say that even if the implementation of the plot operation change in the future, tikz/pgf developers would take care of protecting the definition of \x.

share|improve this answer
1  
I'd say that in the \draw plot example \x is simply part of the syntax and has no real meaning by itself. In pgfmoduleplot.code.tex there's no \x other than in the example on line 384, where \x can be changed into \foo, if one wants. In tikz.code.tex there's \def\tikz@plot@var{\x} which doesn't imply that \x has a definition. –  egreg Jul 20 '12 at 13:07
    
@egreg Yes \x can be undefined or not, it does not matter for tikz/pgf. But if it is defined, tikz will redefined it internally and then restore its original definition. It is a dummy "variable" (that can be changed with variable option BTW). –  cjorssen Jul 20 '12 at 13:25
2  
@AhmedMusa The meaning of \x is changed only locally anyway in \draw plot. I don't see why it shouldn't be used: if you are in the middle of a \foreach\x you know you are and will use variable=\y. –  egreg Jul 20 '12 at 18:07
    
@egreg: My point is that if they did that with \x, it becomes difficult to predict what else they did with \x in the behemoth called PGF. I hope they will one day use something specific to PGF, like \pgftemp. –  Ahmed Musa Jul 20 '12 at 18:32
    
@AhmedMusa ack '\\x[^a-z]' /usr/local/texlive/2012/texmf-dist/tex/generic/pgf/ –  egreg Jul 20 '12 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.