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.

I was used to write 2\x to obtain the product 2*x but today I've found this is not correct anymore in TiKZ.

Next code

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}[x=2cm,y=0.5cm]
\foreach \i in {0,...,3} 
    \foreach \j in {0,...,3} {
        \draw[black] (\i,\j) circle[radius=2pt];
        \draw[blue] (2\i,2\j) circle[radius=2pt];
        \draw[red] (2*\i,2*\j) circle[radius=1pt];
        }
\end{tikzpicture}
\end{document}

gives me

enter image description here

As you can see all blue circles with coordinates computed through (2\i, 2\j) are wrongly placed while red ones computed with (2*\i,2*\j) are correct.

I don't remember to have read anything about * addition for products in TiKZ but, of course, I could be wrong. So, is it a known behaviour? Where is it documented?

share|improve this question
2  
I have always used * as otherwise it is just expanding to 2\i==20, so I think it is obvious to always put the *. Think what would happen if you accidentally put it in an expanded context. Then 2\i would always be wrong. –  zeroth Sep 1 at 11:34
5  
I think you were using 2\i if \i was a dimen. Then it is recognized but otherwise it should be as zeroth noted direct replacement of the number. –  percusse Sep 1 at 11:41
    
@percusse: thank you for the comment. Could you consider converting it into an answer? –  Ignasi Sep 1 at 12:56
    
Was it the problem? –  percusse Sep 1 at 12:57
    
@zeroth: After more than twenty years using LaTeX I'm still trying to understand expansion, so, thank you for the example. Please consider converting it into an answer? –  Ignasi Sep 1 at 12:59

1 Answer 1

A simple reason for not using this is that TikZ expands its arguments whenever it can (in certain context it might not expand the macros):

\foreach \i in {0,1} {
   \draw (2\i,0) circle (2pt);
}

will produce these commands

\draw (20,0) circle (2pt);
\draw (21,0) circle (2pt);

it would then correspond to coordinates multiplied by the current x and y unit vectors (1cm is the default value for both) which was clearly not the intent.

Providing an asterisk (*) will enable the TikZ to switch to pgfmath parsing which then calculates the result

\foreach \i in {0,1} {
   (2*\i,0)
}

and will produce:

\draw (0,0) circle (2pt);
\draw (2,0) circle (2pt);

Alternatively, if the variable is a dimension, then left implied multiplication is a valid TeX operation such as .5\textwidth,2\pageheight etc. Then Tikz reads the dimension calculation as a single argument and expands correctly.

share|improve this answer
    
I took the liberty to clarify some minor differences. Hope you don't mind. –  percusse Sep 1 at 14:59
    
@percusse, sure no problem :) –  zeroth Sep 1 at 15:42

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.