Tag Info

Hot answers tagged

27

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


20

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


18

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


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

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

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


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


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


14

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


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

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


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

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

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

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

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


11

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


11

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


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

There are several reasons, why this does not work: \lx1 is not a command name, it consists of the command \lx, followed by digit 1. Either replace 1 by a letter or the usage is a little more complex via \csname. \x and \y contain the x and y values, the definition of \x defines \x, not the macro inside and \y whill change with every loop. \expandafter ...


11

You can produce it as follows: \documentclass[letterpaper]{article} \usepackage{amsmath, tikz} \usetikzlibrary{calc} \newcommand{\fracgraph}[3][2]{% % #1 = optional height \begin{tikzpicture} \pgfmathsetmacro{\Yheight}{0.5*#1}% \pgfmathsetmacro{\Xincrement}{#2/#3}% \draw (0,0) rectangle (#2,#1); \node at ($(0.5*#2,0.75*#1)$) {1}; ...


11

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


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


10

Why not using a single variable and evaluate? \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \y [evaluate=\y as \yy using int(2*\y)] in {0,...,7} \node at (0,\y) {\y -- \yy}; \end{tikzpicture} \end{document}


10

Here's a possible solution, adapted from Jake's answer here. The code is well-commented, but the basic idea is to modify the styling of a comb-type plot, which has the correct general form. We need to change the mark, and shift and rotate it so the mark is aligned to the correct portion of the curve. The sampling may be adjusted using the each nth ...


10

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



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