Hot answers tagged

33

Short answer The problem illustrated by your example is due to round-off error. See the TikZ/PGF documentation (section 56 in v2.10, p.505; or section 83 in v3.0, p.910): [...] for fractional steps that are not multiples of 2^{-n} for some small n, rounding errors can occur pretty easily. Thus, in \foreach \x in {0,0.1,...,0.5} {\x, }, 0.5 should ...


28

There is exactly one reason why there are pgfplots equivalents to the \foreach command of pgf/tikz: scoping. Occasionally, one wants to aggregrate things, i.e. to compute a value which exists outside of the loop's body -- but which is not global either. If you do not care about scoping and if you are not aggregrating things, the use of \foreach might be the ...


26

Section Options to customize the foreach-statement. (pages 507-508): /pgf/foreach/count= <macro>from<value> (no default) This key allows <macro> to hold the position in the list of the current item. The optional from<value> statement allows the counting to begin from <value>. So, for example, in \foreach \x ...


23

The \mymacro isn't expanded by the \foreach loop, but only afterwards. You need to remove the braces { } around the macro to make it work: \documentclass{scrartcl} \usepackage[utf8]{inputenc} \usepackage{tikz} \begin{document} Picture one: \begin{tikzpicture} \foreach \x/\y in {1.0/2.0, 3.0/4.0} \node[draw] at (\x,\y) {\x--\y}; \end{tikzpicture} Picture ...


22

It's possible to use a minimum code \documentclass{minimal} \usepackage{tikz} \begin{document} \begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!40,minimum size=20}] \foreach \x in {0,...,4} \foreach \y in {0,...,4} {\pgfmathtruncatemacro{\label}{\x - 5 * \y +21} \node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {\label};} ...


20

Yes, foreach is a TikZ/PGF statement. It is described in the very detailed pgfmanual. You can also use it independently of TikZ/PGF by issuing \usepackage{pgffor} in your preamble. This is not necessary if you're using TikZ, as it will be loaded automatically. Here's an example of how to achieve what you described in your question. ...


19

Internally the definition of foreach will be saving the body of the loop in a macro so it is like (if looping over a,b,... ) \def\body{% \renewcommand*{\SomeCommand}[1]{\color{red}#1}% Using ## here eliminates the error. \par\SomeCommand{\x}% }% } \def\x{a}\body \def\x{b}\body ... That initial \def (or \newcommand if you prefer) will require a # in ...


19

Here's another version, not all that different from the previously supplied but combines them in a slightly different way. In effect, it is a bit like defining a dynamic style alias that expands to the given list of options. When a style like red drawing/.style={draw,red} is defined, then calling red drawing executes \tikzset{draw,red} (sort of, actually ...


17

To have more than one \draw (or similar) within a loop, you have to enclose them in braces ({}): \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \i in {0, 1, 2, 3} { \draw (\i, 0) rectangle +(0.5, 0.5); \draw (\i, 1) rectangle +(0.5, 0.5); } \end{tikzpicture} \end{document}


16

\foreach isn't expandable and the grouping is most likely causing some issues as well. I would recommend to use the loop outside the matrix and accumulate the rows in a macro which then only has to be expanded. The etoolbox package provides \gappto (global append to; \xappto would cause issues with fragile content) which can be used here. There is also ...


16

Both commands behave exactly the same. The rounding error is the reason that the terminal value is "missed" in the first case. This check \documentclass{report} \usepackage{tikz} \begin{document} \foreach \x in {1,1.1,...,2} {\number\x\ } \foreach \x in {1,1.2,...,2} {\number\x\ } \end{document} results in 1 1.1 1.20001 ...


16

The main problem is that \foreach works in a group, but it's not only that. The simplest thing to do is to first build the table in a macro and then expand it: \documentclass{article} \usepackage{tikz} \newcommand{\test}[1]{% \def\temp{}% \foreach \i in {1,...,#1} {% \expandafter\gdef\expandafter\temp\expandafter{\temp t1 & t2 \\}% }% ...


15

When using macros in node names, the macros have to be expandable in an \edef context. \pgfmathparse is not. So you need to do the computation beforehand and only use the result of it in the node name. One way is to use the evaluate key on the \foreach as in the following. \documentclass{article} %\url{http://tex.stackexchange.com/q/141259/86} ...


15

To iterate variables "simultaneously" TikZ has the following syntax: the list of variables must be separated by slashes /, and the list items can also be lists of values separated by slashes. \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw (0, 0) grid (4, 3); \foreach \x/\angle in {0.5/20, 1.5/40, 2.5/60, 3.5/80} { ...


15

You can use the additional facilities of foreach macro given in the manual by adding pgfmath package too. For some reason, (initially 4) option is not working if TikZ is not fully loaded so you can define it externally. \documentclass{article} \usepackage{pgffor,pgfmath} \begin{document} \def\lastx{4} \foreach \x[count=\xi from 2,remember=\x as \lastx] ...


15

To number objects from 0 up to value n-1, it is possible to use evaluate or count keys. Both are illustrated in the pgfmanual while explaining foreach operation. An example with the latter: \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \def \n {5} \def \radius {3cm} \def \margin {12} % margin in angles, depends on the ...


15

You can use PGFPlots for creating plots of functions (and of data files): The binomial function isn't defined in the math engine, but you can define it yourself using the key declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);} Then you can plot the function using \documentclass{article} \usepackage{pgfplots} \begin{document} ...


14

The explanation will need a bit of TeX theory. A group is a part of the document that confines most changes made inside it only to this group itself. That is, most settings changed inside a group will be reverted to their old values once the group ends. A group is delimited by { … } or \begingroup … \endgroup. For example, a command \itshape switches the ...


13

pgfplots can draw vector fields by means of its quiver plot handler. \documentclass[a4paper]{article} \usepackage{pgfplots} \begin{document} \thispagestyle{empty} \begin{tikzpicture} \begin{axis}[title=Quiver and plot table] \addplot[blue, quiver={u=\thisrow{u},v=\thisrow{v}}, -stealth] table { x y u v 0 0 1 0 1 1 ...


13

The simplest solution that respects the question of the OP (a triangle of dots with Tikz) seems to be : \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \y in {0,...,4} \foreach \x in {-\y,...,\y} \fill [blue] (\x,-\y) circle [radius=0.2]; \end{tikzpicture} \end{document}


13

Use indirect styles numbered (perhaps more verbose but flexible): \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture} \tikzset{ s0/.style={draw}, s1/.style={draw,red}, s2/.style={circle,draw=blue}, s3/.style={draw}, } \foreach \x/\content in {% 0/a, 1/b, 2/c, 3/d% } { \node[s\x] at (\x,0) ...


13

The \foreach command has its quirks; however, the behaviour shown in your MWE is consistent with section 56 of the PGF manual (2.10), which describes in detail what the ... does inside \foreach. Consider \foreach \xx in {x,y,...,z}. The difference d=y-x is used to "fill in" the elements implicitly specified by ... (see p.505): In this situation, the ...


13

You're right, it's a bit of a shame that the expressions aren't evaluated before the loop is started. Three approaches: Use the count=\xi expression to make the outer loop counter accessible. In this case, the outer loop only has to run from 2 to 4, while the counter will run from 1 to 3, which happens to be the starting point of the inner loop. This ...


13

The \let\OldRef=\the\Ref doesn't work because \OldRef is then simply a copy of \the. Also note that \Ref here stands for \toks<number> and coping it using \let would only let both macros point to the same token register. You can accumulate code using the \g@addto@macro macro: \def\Ref{} \makeatletter \newcommand{\AddRef}[1]{% ...


12

You can use the option [evaluate = <variable> as <macro> using <expression>] to calculate a new value based on a counter, which can then be used in an inner counter: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach [evaluate = \y as \n using \y*2-1] \y in {1,...,5} { \foreach \x in {1,...,\n} { ...


12

Some possibilities with foreach. In your case, you can try the first one \documentclass[11pt]{scrartcl} \usepackage{tikz} \begin{document} \def\I{2} \begin{tikzpicture} \foreach \i [evaluate=\i] in {\I+1,...+1,19+1} \node[anchor=center] at (\i,0) {$\i$}; \end{tikzpicture} \begin{tikzpicture} \foreach \i [evaluate=\i as \j using \i+1] in ...


12

\rule is a fragile command so you can not use it in a \write without \protect. \slogan*{1.1}{Arithmetic \protect\rule{1em}{2pt}}


12

The problem stems from the ... part in the argument of the \foreach macro; note that it disappears if you delete ..., from your code. Although you can of course recognise a pattern in 7/0, 8/10, ..., 10/30 the \foreach macro cannot. I refer you to section 56 of the tikz manual and to using computations with \foreach in tikz for more details about how ...


12

Use \space to get a literal space. The active character ~ does not only produce a space, it produces a non-breakable space, saved in the macro \nobreakspace that expands to \leavevmode\nobreak\␣ (LaTeX). The \xnum{} trick to end the macro does not work because TikZ performs at some point a \csname pgf@sh@ma@#1\endcsname (where #1 is our coordinate/node ...


12

Assignments in a \foreach are local, so you can use \xdef\lastx{\x}: Code: \documentclass{article} \usepackage{pgffor} \newcommand*{\lastx}{} \begin{document} \foreach \x/\index in {4/0,5/1,19/2} { \ifnum \index > 0 % use lastx \fi \xdef\lastx{\x} } Last x was \lastx \end{document}



Only top voted, non community-wiki answers of a minimum length are eligible