8

I've been using LaTeX for about 6 years and the calc package for a long time. Therefore, not being able to use real / floating point numbers was never really a problem.

I've been pouring over the documentation of ConTeXt for the last few days, but haven't seeing if they've decided to address this. It appears that Lua is probably the answer, but I'm just trying to fight my way through ConTeXt, at this point.

Is there an e-TeX or ConTeXt way to deal with this? I think the answer is no, but I thought I'd try. :)

5
  • 1
    The `fp' package for LaTeX, is one of the better packages for handling real numbers. I recently had a reason to go through its documentation and IMHO it shouldn't be too difficult to port. Nov 12, 2010 at 19:30
  • 1
    See wiki.contextgarden.net/…
    – Aditya
    Nov 12, 2010 at 21:26
  • 1
    @Yiannis Lazarides: I don't doubt it could be ported, but why should you use such a hack if you already have a much cleaner and more capable solution? LuaTeX has the potential to obsolete dozens of LaTeX packages, and the various calculation/floating point packages are definitely among them.
    – Philipp
    Nov 12, 2010 at 22:25
  • 2
    @Philipp You are right that it is `cleaner' and quicker to use LuaTeX, but personally I get such a sense of satisfaction after solving something in TeX/LaTeX alone. To paraphrase Knuth 'it is like teaching your dog to walk on his hind legs'. Nov 12, 2010 at 23:54
  • @Yiannis Lazarides: I don't object to TeX-based solutions (the huge majority of users still uses pdfLaTeX), but the OP specifically asked for e-TeX or ConTeXt solutions.
    – Philipp
    Nov 13, 2010 at 0:30

4 Answers 4

12

Current ConTeXt code uses \dimexpr and \numexpr from e-TeX a lot, because most of ConTeXt predates luatex.

If you want to do a quick calculation in lua, you can just do something trivial like this:

\def\evaluate#1{\directlua{tex.sprint(tostring(#1))}}

then you can write:

\evaluate{10.2+1e6}
\evaluate{10.2/0}

et cetera.

Best wishes, Taco

6

e-TeX can evaluate simple fixed-point expressions via \dimexpr, but as you already stated, for ConTeXt Mk IV programming in Lua is the way to go, and it is much nicer, simpler and cleaner than anything TeX could ever offer.

7
  • But it won't jump out of my computer and type it into my keyboard for me while I nap! :) I'm excited to learn it. It's just a lot to bite off right now. :P Nov 12, 2010 at 22:06
  • 1
    @Andrew: lua is one of the simplest and very intuitive languages that I've seen (I haven't seen much though, but any thing is god send when compared to TeX), it took me like 2 hours reading some lua tuturial to get the grasp of it. Nov 12, 2010 at 22:31
  • I can't wait to get into it! I'm going to hold off until pgf and MARK IV work together, however. I'm not willing to ditch everything, only to pick up METAPOST Nov 13, 2010 at 4:15
  • @Andrew: If PGF and Mark IV do not work together for you, please report it on the context mailing list. There was a discussion about this recently and the incompatibility between ConTeXt and PGF color mode should be fixed now.
    – Aditya
    Nov 13, 2010 at 6:52
  • @Aditya: It is. I just installed the daily in my local tds. It works nicely. Next, I'm going to see how I missed that discussion and then figure out why some PDF files obey transparency shadings and others do not... for another post. Thank you! Nov 13, 2010 at 7:12
4

Here is a simple port of the first four functions of fp-basic.sty

\unprotect
\def\FPadd#1#2#3% #1 = #2 + #3
  {\ctxlua{context.setvalue("\strippedcsname#1", #2+#3)}}

\def\FPsub#1#2#3% #1 = #2 - #3
  {\ctxlua{context.setvalue("\strippedcsname#1", #2-#3)}}

\def\FPmul#1#2#3% #1 = #2 * #3
  {\ctxlua{context.setvalue("\strippedcsname#1", #2*#3)}}

\def\FPdiv#1#2#3% #1 = #2 / #3
  {\ctxlua{context.setvalue("\strippedcsname#1", #2/#3)}}
\protect

Other functions can be defined in a similar manner, if needed. As a bonus, this works for any number format (1e6, etc) that is recognized by lua. There are no error checks, so division by zero gives inf. For example:

\starttext
\FPadd\ADD{10.2}{1e6}
\FPsub\SUB{10.2}{13.5}
\FPmul\MUL{10.2}{13.5}
\FPdiv\DIV{10.2}{0}

\startlines
\ADD
\SUB
\MUL
\DIV
\stoplines
\stoptext

gives

1000010.2
-3.3
137.7
inf
3
  • I like Taco's solution, why one would want a separate macro for each math operation when one can use intuitive operators. Nov 13, 2010 at 11:00
  • Agreed. But similar functions can be defined for pgfmath, which will greatly speed up things.
    – Aditya
    Nov 13, 2010 at 17:19
  • This is impressive stuff, however. Seeing that the choice between MK IV and PGF/TiKz was a false choice, I've decided to compile with MK IV, use PGF, and learn Lua. Thank you so much for all of your great answers! Nov 14, 2010 at 6:25
0

How about the pgfmath part of pgf? I know it can be used in ConTeXt, I just don't know if it will do full floating point for you. In trying to find out to what degree this would work for you I also found this link which has some other useful thoughts, especially in the comments.

3
  • I'm kind of stuck right now and it's funny that you mentioned PGF. I'm using PGF, which can't be used in ConTeXt MARK IV. So... I have to go to XeTeX, which buys me some easier font handling, but I loose Lua. I could go to METAPOST / MFPIC, but that seems like a short/medium term solution. I think that I'll stick to TiKZ / ConTeXt MARK II / XeTeX until they update TiKz/ ConTeXt to play nice. It'll give me reason to learn e-TeX internals more fully and I'll be able to stick with the graphics package I know and have macros already written for. Nov 13, 2010 at 4:13
  • How about doing the math in Perl first. You could use a PerlTeX command, though I'm not sure that that will work with ConTeXt. What WILL work (I think) is setting up a Template Toolkit template, evaluate what you need, substitute it into the template and then compile with a system call. I think that has to work (probably) Nov 13, 2010 at 4:42
  • The more I think of it, TT would help you put a number into a document, but I don't know if it will help you with something more involved than that. Sorry maybe you're just screwed. (Damn you dependency hell!) Nov 13, 2010 at 4:48

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.