4

Let us assume that we want to divide two numbers u=-60 and v=3 using the fp package without calling tikz picture. It works fine as below:

\documentclass{article}
\usepackage{fp}
\begin{document}
\edef\u{-60}
\edef\v{3}
\FPeval \wd{u/v}
$u/v=\wd$
\end{document}

The correct answer will be seen at the end:

enter image description here Now let us call tikz library to put the answer in a node feature as below:

\documentclass{article}

\usepackage{fp}
\usepackage{tikz}
\begin{document}
\edef\u{-60}
\edef\v{3}
\FPeval \wd{u/v}
$u/v=\wd$
\begin{tikzpicture}
\node at ( 0,0) {$u/v=\wd$};
\end{tikzpicture}

\end{document}

Unfortunately we get an error in this case:

! You can't use `the character -' after \the. \wd ->- 20.000000000000000000 l.11 \node at ( 0,0) {$u/v=\wd$};

  • Do you know how to use node in tikz with div (\) of \FPeval at the same time?
  • 4
    You're overwriting the \wd primitive. This calls for trouble. Name your macro \mywd or so instead. – Henri Menke Aug 13 at 8:12
  • 2
    Same goes for the \u and \v single letter macros. There are use for diacritics. In general, thou shalt not redefine single letter macros. – Henri Menke Aug 13 at 8:13
  • Thanks @Henri, When I use \w instead of \wd for the variable name it works with tikz. – Aria Aug 13 at 8:15
  • 1
    Yes, that is correct. To be on the safe side just always use a unique prefix, e.g. \ariau and \ariav. These are highly unlikely to be defined by any other package and should therefore always be safe to use. – Henri Menke Aug 13 at 8:23
  • 1
    you can find a description in the pgfmanual. And a comparision of the three calculation methods is here tug.org/TUGboat/tb40-1/tb124wright-fpkg.pdf (a shorter version is here texdev.net/2018/12/09/floating-point-calculations-in-latex) – Ulrike Fischer Aug 13 at 8:32
7

It's quite unfortunate that fp doesn't offer an option to warn the user when the first argument to an \FP... macro is already defined.

It's also (unfortunate) common practice to use short names for variables to use in TikZ. This may have no side effect if used inside a tikzpicture, because (non global) redefinitions are nullified when the tikzpicture ends.

Not always safe: in a very recent question, a user got into troubles because of using \color as the name for a variable in a \foreach loop, but the loop code used \textcolor.

In your case, you're redefining three existing control sequences:

  • \u is for the “breve accent”, whence \u{a} yields ă;
  • \v is for the “háček” accent, whence \v{c} yields č;
  • \wd is a primitive TeX command that's used in several places, as it returns the width of some built box.

While the first two redefinitions are harmless in your example, because it doesn't sport accented letters, the last one is fatal. At some point, TeX is instructed to do some calculations involving the box containing the text to be typeset at the node and it needs the width of that box, which it inquires using \wd; but \wd now means something completely different.

Here is a different method. The names are longer, but the input is much clearer. With \fpeval you can do computations on the spot (see the second node).

\documentclass{article}
\usepackage{xfp} % not fp
\usepackage{tikz}

\ExplSyntaxOn
\NewDocumentCommand{\setfpvar}{mm}
 {
  \fp_zero_new:c { l_aria_fpvar_#1_fp }
  \fp_set:cn { l_aria_fpvar_#1_fp } { #2 }
 }
\NewExpandableDocumentCommand{\fpvar}{m}
 {
  \fp_use:c { l_aria_fpvar_#1_fp }
 }
\ExplSyntaxOff


\begin{document}
\setfpvar{u}{-60}
\setfpvar{v}{3}
\setfpvar{wd}{\fpvar{u}/\fpvar{v}}

\begin{tikzpicture}
\node at (0,0) {$u/v=\fpvar{wd}$};
\node at (0,-1) {$u/v=\fpeval{\fpvar{u}/\fpvar{v}}$};
\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |
  • 1
    We should remind Bruno about the ideas of having named variables in expressions: it's in l3trial at the moment – Joseph Wright Aug 13 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.