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 have the following code, where the size of the cube marks is set to \x inside a foreach loop:

\begin{tikzpicture}
 \begin{axis}[axis lines=none]
  \newlength{\mylen}
  \foreach \x in {1,2,...,10} {
  \setlength{\mylen}{\x pt}
    \addplot3[only marks,scatter,mark=cube*,mark size=\mylen] coordinates {(\x*5,10*\x,3*\x)};
    };
\end{axis}
\end{tikzpicture}

This results in an empty plot - i.e. it doesn't work.

However, if we set the length for mylen (to a constant) before the foreach-loop, it works just fine. That might be a clue. Any ideas?

Also, how can can I manipulate the length inside the \setlength command to for instance \setlength{\mylen}{2*\x+10 pt} ?

share|improve this question
2  
The assignment works for me inside the loop. Please post a minimal example which demonstrates the problem. Also you have in it not ... which is an unicode character. Are you doing anything special? (special package, Xelatex, ...). –  Martin Scharrer Mar 24 '11 at 1:24
    
@Martin: I have posted a new example, that hopefully clarifies my problem. I tried it in a MWE and I didn't get it to work. –  0sh Mar 24 '11 at 15:35
add comment

2 Answers

up vote 3 down vote accepted

You should use the \pgfplotsinvokeforeach{list}{command} command for this, instead of the usual \foreach. That makes sure the parameter is passed to the \addplot options expanded, so when the plots are actually drawn (which does not happen immediately, I believe), they include the correct parameter:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[axis lines=none]
  \pgfplotsinvokeforeach{1,2,...,10} {
    \addplot3[only marks,scatter,mark=cube*,mark size=#1 pt] coordinates {(#1*5,10*#1,3*#1)};
    };
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Ahh, ok. I wonder why the \addplot command isn't defined in a way that expands it options and stores the expanded values instead. –  Martin Scharrer Mar 24 '11 at 16:01
    
@Martin,Jake: This works just fine, but there is a problem. Setting mark size=#1+10 works while setting cube/size x=#1+10pt does not (it does however without the +10pt). Do you have a solution? –  0sh Mar 24 '11 at 17:24
    
@Milosz: You must add the unit (pt) to both numbers or not have any units. You can't mix this two styles in PGF/TikZ. So either use mark size=#1pt+10pt or mark size=#1+10 (then the default unit is used, which is normally 1cm and way to large). –  Martin Scharrer Mar 24 '11 at 17:27
    
@Martin: Nope, that doesn't do the trick. It does for mark size but not for cube/size x=#1pt+10pt. It seems as the size expands to only the first argument, so for instance cube/size x=#1pt+10pt expands to cube/size x=#1pt while cube/size x=10pt+#1pt expands to cube/size x=10pt. –  0sh Mar 24 '11 at 17:37
1  
@Milosz: It depends how the values are parsed by pgfplots, i.e. if \pgfmathparse is used or not. Try to write it as \the\dimexpr #1pt+10pt\relax. This handles the addition in eTeX and is expandable. –  Martin Scharrer Mar 24 '11 at 17:55
show 2 more comments

The problem seems to be that the \addplot command doesn't interpret its options when it is encountered but only afterwards when the plot is drawn. This means that the current value of \x or \mylen inside the loop is not used but the latest value before or after it. The loop content is executed inside a group, so that changes don't survive afterwards.

In your case all plots where drawn using mark size=\mylen with \mylen having the default value of 0pt. The solution is to expand the value manually beforehand and call \addplot with the expanded value. You actually don't need the length for this. If you want to use it exchange \x with \the\mylen in the \edef part:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis lines=none]
  \foreach \x in {1,2,...,10} {
        \edef\PLOT{\noexpand\addplot3[only marks,scatter,mark=cube*,mark size=\x pt]}
        \PLOT coordinates {(\x*5,10*\x,3*\x)};
    };
 \end{axis}
\end{tikzpicture}
\end{document}

Next time PLEASE provide a full compilable, but minimal example. I never used pgfplots before and had to guess the preamble. Here it turned out to be pretty simple but it can be quite complicated with PGF/TikZ when many libraries are required.

share|improve this answer
add comment

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.