Tag Info

Hot answers tagged

15

Here is a solution using TeX integer arithmetic. I am reusing counters defined by PGF in order to avoid having to declare new ones. \documentclass{article} \usepackage{tikz} \makeatletter \newcommand\binomialCoefficient[2]{% % Store values \c@pgf@counta=#1% n \c@pgf@countb=#2% k % % Take advantage of symmetry if k > n - k ...


15

This happens because you're calling min with an argument it's not made for: The first element of your list is empty, which sets \pgfmathresult to the largest allowable number, due to the way the algorithm is implemented. To get the correct behaviour, you should make sure the first component is not empty (the last one may be, though). Also, \foreach ...


14

Well, here is a solution that uses pgf only for the random integers. Everything else is done with TeX's own conditionals and, in one instance, a loop. Code \documentclass{article} \usepackage{pgf} \pgfmathsetseed{\pdfuniformdeviate 10000000} \newcommand*\MakeFirstTerm[2]{ \loop\pgfmathrandominteger{\a}{#1}{#2} \ifnum\a<0\relax ...


13

Lots of if :) \documentclass{article} \usepackage{tikz} \pgfmathsetseed{\pdfuniformdeviate 10000000} \newcommand{\rndcoeff}[1][1]{ \pgfmathrandominteger{\a}{\ifnum#1>1 1\else0\fi}{6} \ifnum#1>1 \pgfmathparse{rand>0?:"-"}\pgfmathresult\ifnum\a=1\else\a\fi x^2 \else \ifnum#1<1\relax \ifnum\a>0\relax ...


12

In the cvs version of pgf/tikz or in the version available for texlive at tlcontrib there is an experimental undocumented dim function in pgfmath defined as \makeatletter % dim function: return dimension of an array % dim({1,2,3}) return 3 % dim({{1,2,3},{4,5,6}}) return 2 \pgfmathdeclarefunction{dim}{1}{% \begingroup \pgfmath@count=0\relax ...


12

\pgfmath is not expandable, and so has to be used with a 'known' output macro to provide the result (for more on expandable code, see for example Tricks to make macros expandable and Why isn't everything expandable?). The LaTeX3 FPU is expandable, and so you can do \documentclass{article} \usepackage{expl3} \ExplSyntaxOn ...


11

Use \pgfmathsetmacro\mymacro{...} instead of \pgfmathparse{...}. From the v2.10 pgfmanual, section 62.1 Commands for Parsing Expressions, page 527: \pgfmathsetmacro{<macro>}{<expression>} Defines <macro> as the value of <expression>. The result is a decimal without units. ...


11

The problem is the inaccurate division step after the rounding operation, which is necessary because you can't specify the number of decimal digits for the rounding operation. A solution would be to use \pgfmathprintnumber[precision=1]{\pgfmathresult} to round and output the number. If you want to use the rounded number elsewhere, you can use ...


11

If the function is used inside a coordinate pair, then you get two closing parentheses: (0.2, f(0.2)) This apparently confuses the parser. These parentheses are not matched like curly braces in \TeX, when an argument is read. A set of curly braces protects the inner parentheses: (0.2, {f(0.2)}) Then the inner delimiters are hidden for the parser that ...


10

You can use the optional arguments of \pgfmathprintnumber to either cut off the decimal part or to round the number to an integer. Alternatively, you can use the \num macro from the siunitx package to round the number: \documentclass{article} \usepackage{tikz} \usepackage{siunitx} \begin{document} \pgfmathsetmacro\testnumber{25.7} ...


10

It is also relatively easy to solve using just TikZ: \documentclass{article} \usepackage{tikz} \newcounter{arraycard} \def\arrayLength#1{% \setcounter{arraycard}{0}% \foreach \x in #1{% \stepcounter{arraycard}% }% \the\value{arraycard}% } \begin{document} \noindent The length of $\{1,2,3\}$ is \arrayLength{{1,2,3}}.\\ And the length of ...


10

Here's a pair of lualatex solutions. Both provide \genrand which takes an optional integer argument that specifies the maximum (absolute) value of the coefficients, the default is 10. The first uses pattern matching and the second is a somewhat odd variation of the standard approach. Pattern Matching: \documentclass{article} \usepackage{luacode} ...


9

You can set the random seed each time you draw the ellipse, like so: \documentclass[presentation]{beamer} \usepackage{tikz, pgf} \usetikzlibrary{decorations.pathmorphing} \begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \uncover<1-3>{ \pgfmathsetseed{1234} % Choose a four-digit number here \fill [decorate, decoration={random ...


9

\pgfmathparse always saves its result with a decimal part. You can either use \pgfmathprintnumber{\pgfmathresult} in the node text to output the number, which will remove the .0 for integers, or do the calculation using \pgfmathtruncatemacro\mymacro{round(\i*100/3)}, which will save the result of the calculation without the decimal part, and then use ...


9

There seems to be an error in pgfmathparser.code.tex. Line 712 of the latest version (1.47) reads \pgfmathdeclareoperator{!=}{notequalto}{2}{infix} {250} but it should be \pgfmathdeclareoperator{!=}{notequal}{2}{infix} {250} since the function is called notequal (not notequalto). Changing this line fixes the problem. I'll file a bug report.


9

The pgfmanual states (section 62.1 Commands for Parsing Expressions, page 526 with v2.10): An integer with a zero-prefix (excluding, of course zero itself), is interpreted as an octal number and is automatically converted to base 10. So 021 or 0021 is actually 2x8+2x1 = 17, not 21. You would need to avoid or remove the leading 0s, e.g. using a ...


9

You can do complete expansion beforehand: \documentclass{article} \usepackage{pgf}% Easy way to get pgfmath \def\epgfmathsetmacro#1#2{\begingroup \edef\x{\endgroup\noexpand\pgfmathsetmacro\noexpand#1{#2}}\x} \def\MaxValueOfTok{8}% \newtoks\SomeTokDefinedDirectly \newtoks\SomeTokDefinedViaDef \SomeTokDefinedDirectly={7} ...


9

One way to do this is to use \addplot coordinates. Or, as Jake suggested one could let the second coordinate be the variable (3,x) Code: \documentclass{article} \usepackage[paperwidth=21.0cm]{geometry}% for image capture \usepackage{pgfplots} \begin{document}\noindent \begin{tikzpicture} \begin{axis}[xlabel=$x$, ylabel=$y$] ...


9

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


9

No, that doesn’t work because TikZ only accepts fully expandable input. By the way, the coordinate input is thrown into PGF math anyway, so \draw[thick] (0.255in*3/2,2) … works just as well (case 1). You can also just store 0.255in*3/2 in \leftBoundary and use that macro (case 2; again, using that anything will be parsed through the mathematical ...


8

Your code works for me with just some little changes: instead of gterm and denom use \gterm and \denom. use { and } to protect r51(a,b,c) in \drawline definition. If I've understood your definition of r51, it's easier to obtain a simple form. I've called it r52. They look the same. \documentclass{standalone} \usepackage{tikz} ...


8

This is a problem with the fpu library that is used by pgfplots: The ifthenelse command is not implemented in fpu, so it falls back to the normal pgfmath routine, which then stumbles over the floating point format of the arguments, because numbers are handled in the form 1Y1.0e0]. To circumvent this, the fpu library can be disabled in the newly defined math ...


8

From the pgf manual 2.10csv page 694: It should be noted that all calculations must not exceed ±16383.99999 at any point, because the underlying computations rely on TeX dimensions. This means that many of the underlying computations are necessarily approximate and that in addition, are not very fast. TeX is, after all, a typesetting language and ...


8

I don't think that these PGF arrays are suitable for your task. You can use an implementation of mine which might be better. But although that implementation proved to be useful, it has quite high demands on TeX skills and has no real support. In the following, I will elaborate on that implementation. You can read it, and then you should go back and ...


8

\put needs something that expands to a number: \documentclass{beamer} \usepackage{tikz} \begin{document} \def\yoff{5} \newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult} \begin{frame} \frametitle{Testing} \begin{itemize} \item Test picture \begin{picture}(0, 0) \put(0,\numexpr8+\yoff\relax){ \tikz \draw[red,thick] (0,0) ellipse ...


8

You have two to four choices (as \pgfmathresult is already fully expanded there is no different in all four). The key handler /.expanded is like an \edef (recommended if you use it inside \pgfkeys{…}): \pgfkeys{/junk/.expanded=\pgfmathresult} The key handlers /.expand once and /.expand twice do just enough \expandafters to expand the given value once or ...


8

Based on this answer. \documentclass{standalone} \usepackage{tikz} \makeatletter \pgfmathdeclarefunction{erf}{1}{% \begingroup \pgfmathparse{#1 > 0 ? 1 : -1} \edef\sign{\pgfmathresult}% \pgfmathparse{abs(#1)} \edef\x{\pgfmathresult}% \pgfmathparse{1/(1+0.3275911*\x)} \edef\t{\pgfmathresult}% \pgfmathparse{% 1 - ...


7

\ifnum compares integers. The results stored by \pgfmathsetmacro always contain a decimal part, even for integer numbers, so you'll have to use \pgfmathtruncatemacro in this case: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \pgfmathtruncatemacro\one{1} \pgfmathtruncatemacro\two{2} \ifnum\one<\two \draw ...


7

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


7

Macros in a node name are merely expanded, but not parsed by a math parser. You need to do this yourself, either by using an explicit command like Marco suggested in his answer, or by using the [evaluate = <variable> as <new macro> using <expression>] option of the \foreach statement. Note that you want to use the int(...) function to make ...



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