Tag Info

Hot answers tagged

32

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 ...


24

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 ...


20

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 ...


20

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 ...


18

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 ...


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}


17

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 ...


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 ...


15

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};} ...


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 ...


14

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

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] ...


14

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} { ...


14

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 \\}% }% ...


14

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} ...


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

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]{% ...


13

\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 ...


13

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. ...


12

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) ...


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

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}


12

I think all you need to to do is to add an extra {} around the expression as the comma is probably confusing the parser. \foreach \j [evaluate=\j as \jn using {mod(\j,4)}] in {5,6} However, I would recommend a slightly different approach and that is to use pagemathtruncatemacro (or \pgfmathsetmacro if you need real number values) instead: Code: ...


12

I agree with Torbjørn's answer but in your case, it was possible to write the next code. I use only one command \draw on one path. I think is important to understand what is a path to work correctly with TikZ. The end of the path is determined by ; \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \i in {0, 1, 2, ...


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

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 ...


11

Deep in the bowels of TikZ, the child creation code calls \foreach to create the children. However, by the time it has done so then the various parameters to the \foreach loop have passed through several macros. Each time, there is the potentiality to strip off an outer pair of braces. Rather than try to keep track of this, TikZ gets round it by ensuring ...


11

Here's a way without cleveref, but it needs a recent version of expl3: \documentclass{article} \usepackage{xparse,amsmath} \ExplSyntaxOn \NewDocumentCommand{\erefs}{sm} { \IfBooleanTF{#1} { \jlperla_erefs:Nn \ref { #2 } } { \jlperla_erefs:Nn \eqref { #2 } } } \seq_new:N \l_jlperla_input_seq \seq_new:N \l_jlperla_output_seq \cs_new_protected:Npn ...



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