Hot answers tagged

52

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


37

According to pages 470-471 of the pgfplots documentation: Keep in mind that inside of an axis environment, all loop constructions (including custom loops, \foreach and \pgfplotsforeachungrouped) need to be handled with care: loop arguments can only be used in places where they are immediately evaluated; but pgfplots postpones the evaluation of many macros....


34

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


26

No pgffor package is required for this; just write your table preamble as: \begin{tabular}{l *{6}{n{2}{3}}} The general syntax is: *{n}{column(s) pattern} where n is the number of repetitions, and the pattern can be any number of column specifiers, @{some code}, !{some code}, >{…}, <{…}. Some part of this syntax depends on the array package – in ...


22

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


22

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


21

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


21

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


20

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


20

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


19

Two solutions using only foreach own computing tools \foreach \x [count=\i] in {3.14,6.28,...,21.98} Difference between second and first items in list is calculate and added to second and successive values until it reaches the last one. On the same time \i counts list items. \foreach \i [evaluate=\i as \x using \i*3.14] in {1,2,...,7} \i advances ...


17

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


17

In my opinion, this is a bug in TikZ. As is well-known on this site (!) the PGF \foreach command executes its innards inside a group (actually, two groups). This means that when something is meant to be remembered from one iteration to the next, there has to be an explicit mechanism for remembering it. When the \foreach construction is used to build a ...


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


16

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

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


15

You could make TikZ 'remember' the previous point, from which to continue drawing: \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}[scale=.5] \foreach [count=\x] \pt in {(0,5), (0,2), (2,2), (2,0), (6,0), (6,2), (8, 2), (8,6), (6,6), (6,8), (3,8)} \node[black, circle, draw] (p\x) at \pt {}; \draw \foreach \...


15

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


15

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


15

Here's an alternative way to draw your circle + vectors using Metapost and luamplib. Compile with lualatex (assuming you have the TeX Gyre maths fonts available). \documentclass[border=5mm]{standalone} \usepackage{luamplib} \usepackage{unicode-math} \setmathfont{TeX Gyre Termes Math} \begin{document} \mplibtextextlabel{enable} \begin{mplibcode} beginfig(...


15

How can I resist to this golf challenge ! \documentclass[tikz,border=7pt]{standalone} \begin{document} \begin{tikzpicture} \foreach~in{0,...,24} \fill[shift={({mod(~,5)+.5},{div(~,5)})},gray,rotate=45]rectangle(45:1); \draw rectangle(5,5); \end{tikzpicture} \end{document} EDIT: A second code that is 2 characters longer but use a single ...


14

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}


14

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}


14

\foreach strips spaces before each list item, and then collects everything up to the next comma (or the list terminating token) before assigning to the relevant variables. The \foreach problem arises in this case because of the extra space given before the closing brace in the list. Consider the following: \foreach \p/\q in {a/{1,2,3}, b/{4,5,6} } \...


14

You're missing the fact that \foreach executes each cycle in a group, so when finishing it the change to \@tabtoks is undone. Add \global in the relevant places (and also \arraybackslash in the specification for the last column, but this is another problem). \documentclass[10pt,a4paper]{article} \usepackage{tabularx} \usepackage{tikz} \newcommand{\nRows}{5}...


14

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

The code illustrates the use of polar coordinates which make it easy to construct symmetrical graphs. \documentclass[border=2mm]{standalone} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture}% [vertex/.style={circle,draw,fill=black,minimum width=1.5mm,inner sep=0mm}] \newcommand\coce{0.5}% distance of triangle corners from ...


13

You shouldn't expand the \\ that you add to your matrix content. If you use \gappto\mymatrixcontent{\\}% (instead of \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\\}%), your example works: \documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \usepackage{etoolbox} \begin{document} \begin{tikzpicture}[>=latex] \tikzstyle{...


13

With your loop: \foreach \x in {1,...,4} \foreach \y in {0,...,4} { \pgfmathtruncatemacro{\cur}{\x + 5* \y} \pgfmathtruncatemacro{\next}{\cur + 1} \draw (\cur) -- (\next); } you are actually drawing just horizontal lines. Thus, if I understood correctly the question, you need to draw vertical lines. For simplicity I used another loop:...


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