6

I am trying to use the fp package, and the results are deeply confusing to me:

\documentclass[a4paper]{article}
\usepackage{fp}
\begin{document}
\begin{tabular}{r|l}
\FPeval\testing{2 + 2}
a & \testing
\end{tabular}
\end{document}

This will not compile:

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018/Arch Linux) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./mvce.tex
LaTeX2e <2018-04-01> patch level 5
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/fp/fp.sty
`Fixed Point Package', Version 0.8, April 2, 1995 (C) Michael Mehlich
(/usr/share/texmf-dist/tex/latex/fp/defpattern.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-basic.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-addons.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-snap.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-exp.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-trigo.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-pas.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-random.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-eqn.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-upn.sty)
(/usr/share/texmf-dist/tex/latex/fp/fp-eval.sty))
No file mvce.aux.
( FP-EVAL ( FP-UPN ( FP-ADD ) ) )
! Undefined control sequence.
l.6 a & \testing

However, removing the & will give the intended result. Why does that happen, and what can I do about it? Should I use a different package for arithmetic?

This is just a simplified example. I actually want to use the result multiple times across a & and \\ boundary, which is why I can't just move the FPeval where the result is used.

1 Answer 1

7

\testing is a local variable, lost in the local scope (corresponding to each cell of the tabular). You need to set it global to use it in a different cell, or outside the tabular.

EDIT: Since egreg strongly recommends avoiding the syntax \global\let\testing\testing, I instead introduce \localtesting and \testing as the local and global versions of the same data.

\documentclass[a4paper]{article}
\usepackage{fp}
\begin{document}
\begin{tabular}{r|l}
\FPeval\localtesting{2 + 2}\global\let\testing\localtesting
a & \testing
\end{tabular}

Look, I am even available outside the tabular: \testing
\end{document}

enter image description here

7
  • Isn't it more the cells than the tabular env? Each cell in a table is a local group.
    – daleif
    Oct 17, 2018 at 17:52
  • I believe that's what I need, thanks. Are there any other scopes than \global? For example, is it possible to make the variable's scope be the interior of a \newcommand definition? Could I wrap this construction into a macro, to avoid typing it out every time? Something like \myeval\global\testing{2 + 2}
    – Maya
    Oct 17, 2018 at 17:54
  • 1
    @daleif That is what I meant, but I see how it can be confused...I will edit to clarify. Oct 17, 2018 at 18:03
  • @NieDzejkob There are only local and global scopes. With extra work, you could save a global variable, change it as you wish, and then reset it back to the earlier value, but why spend the effort? That said, you could certainly define a \gFPeval that accomplishes both the \FPeval and the globalization. Oct 17, 2018 at 18:07
  • 2
    To wrap this monstrosity into a neat macro: \def\geval#1#2{\FPeval#1{#2}\global\let#1#1}
    – Maya
    Oct 17, 2018 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .