Hot answers tagged

47

According to pages 470-471 of the pgfplots documentation: (Note: in pgfplots documentation v1.17, the page range has changed to 544-545.) 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 ...


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


29

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


25

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


22

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


21

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


17

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


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

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


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


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

There are many ways for numerical calculations. The following example uses e-TeX's \numexpr: \the\numexpr 135 + 5 * \x \relax Full example: \documentclass[letterpaper,12pt]{book} \usepackage{tikz} \begin{document} \begin{tikzpicture}[line width=1pt,line cap=round,x=.5cm,y=.5cm] \clip (0,-0.8) rectangle (10,1); % Inequality Line \draw [<->,...


13

I defined a new command \hexmult that takes 2 arguments. The first is a positive integer giving the number of rows of hexagons. The second is a comma-separated list, where each entry is of the form i;j/k. The j is the row (starting from the bottom), the i is the hexagon number (from the left), and the k is the cell contents, parsed in math mode (so the minus ...


13

Like this? \documentclass[border=2pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach\x/\y/\z in {4/2/A,5/3/B,6/4/C,2/.5/D,1/2/E,6/3/F,3/1.5/G,1/4/H} \draw [fill = black] (\x,\y)circle (1 mm) node[left] {\z}; \end{tikzpicture} \end{document}


12

Replace all the \addplot by \foreach \a in {-2.4,-1.8,...,2.4}{ \addplot [domain=-5:5, samples=100, color=cyan]{\a*x^2}; } \foreach \a in {-2.1,-1.5,...,2.4}{ \addplot [domain=-5:5, samples=100, color=red,dashed]{\a*x^2}; } Code \begin{tikzpicture} \begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$]; \foreach \a in {-2.4,-...


12

\documentclass{article} \usepackage{tikz} \newcommand{\dotexp}[1]{% \edef\DEnum{\the#1}% \tikz{% \foreach \x in {1,...,6}{% \ifnum\x>\DEnum \edef\DEfill{lightgray}% \else \edef\DEfill{cyan}% \fi \draw[\DEfill,fill=\DEfill] (\x/2.5,0) circle (.5ex);% }% }% } \begin{document} \newcount\x \x=0 \loop \...


11

Both savedanchor and anchor are aliases for internal macros. \anchor globally defines an anchor when the shape is declared (so expansion occurs in the anchor name), but only expands the code for the anchor when the anchor is called in a coordinate, for example, (A.pin 1). \savedanchor doesn't expand either of its arguments when the shape is declared but does ...


11

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


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


11

Presumably you are doing this because you want the rows of the table to depend on the counter in the loop. In this case you will need a way to add expanded objects to the list of tokens. Here I add an \eaddtabtoks macro in addition to egreg's corrections to the global assignments. \documentclass[10pt,a4paper]{article} \usepackage{tabularx,etoolbox} \...


11

Please always post complete code so that people do not have to guess which packages and class are required to reproduce the problem or answer the question. The following gives one way of incrementing the depth of colouring according to \j: \documentclass[tikz,border=5pt]{standalone} \begin{document} \definecolor{myblue}{cmyk}{10,0,0,0} \begin{...


11

After the definition via \pgfmathsetmacro, the macro \dest contains a real number (1.0, 2.0, ...), but very likely you need an integer. This can be achieved by using \pgfmathtruncatemacro instead of \pgfmathsetmacro to cut the decimal fraction part: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \path \foreach \src in {...


11

An alternative approach would be the standard \ifnum construct combined with \pgfmathparse. Note that since 1.6 is a float, you must provide a tolerance. A simple \pgfmathparse{\y == 1.6 ? int(1) : int(0)} would not work. Here is the complete solution: \documentclass[border=0.2cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach ...


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