Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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. Thanks, \real!

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

share|improve this question
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. –  Yiannis Lazarides Nov 12 '10 at 19:30
1  
See wiki.contextgarden.net/… –  Aditya Nov 12 '10 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 '10 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'. –  Yiannis Lazarides Nov 12 '10 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 '10 at 0:30
add comment

5 Answers

up vote 11 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
    
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 –  Andrew Starks Nov 12 '10 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. –  Khaled Hosny Nov 12 '10 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 –  Andrew Starks Nov 13 '10 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 '10 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! –  Andrew Starks Nov 13 '10 at 7:12
show 2 more comments

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
share|improve this answer
    
I like Taco's solution, why one would want a separate macro for each math operation when one can use intuitive operators. –  Khaled Hosny Nov 13 '10 at 11:00
    
Agreed. But similar functions can be defined for pgfmath, which will greatly speed up things. –  Aditya Nov 13 '10 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! –  Andrew Starks Nov 14 '10 at 6:25
add comment

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.

share|improve this answer
    
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. –  Andrew Starks Nov 13 '10 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) –  Joel Berger Nov 13 '10 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!) –  Joel Berger Nov 13 '10 at 4:48
add comment

Guys for calculations with TeX/LaTeX I used a CalcTeX package http://sg.bzip.pl/CalcTeX/en.html this package understated elements of TeX/LaTeX language and due this fact is possible make any calculations for example:

$ a := 1.2 $  text \\
$ b := 1/b $
\[
c:=a^b\cdot \frac{a}{b}
“\;\;\;”
c
\]

is possible to use this package for calculation with units too. Personally, CalcTeX works for me as an simply MathCAD for linux/windows and cell phone.

share|improve this answer
1  
I have to admit that I can't follow what you're saying here. –  Hendrik Vogt Dec 31 '10 at 11:16
    
@Hendrik: I suspect that CalcTeX is a python program to pre-parse a LaTeX document. –  Bruno Le Floch Apr 2 '11 at 23:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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