Changes

Jump to navigation Jump to search
3,644 bytes added ,  23:37, 23 March 2009
Following discussion on the mailing list, added further information.
</context>
{{todo|Add some more illustrative examples.}} == More on evaluating expressions in ConTeXt == Whereas the eTeX <code>\dimexpr</code> is quite useful,one must be aware of its limitations arising from how integer arithmetic is performed within TeX.Following a discussion on the ConTeXt mailing list,http://archive.contextgarden.net/thread/20090323.122228.af43852e.en.htmlthis will be illustrated through a pedagogical example (including, as a bonus, a few handy tricks): ----''Our question today is how to divide two dimension and compare their result with the result of another division. To do this, we first assign values to four dimensions. Then, we will try using eTeX functions with ConTeXt; the following example should work:'' <context source="yes">\dimen0=15pt\dimen2=20pt\dimen4=15pt\dimen6=30pt \ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax The second fraction is greater.\else The first fraction is greater.\fi</context> ''This appears to work, but does it really? What will be the result if the second fraction were to be greater:'' <context source="yes">\dimen0=15pt\dimen2=20pt\dimen4=15pt%\dimen6=30pt\dimen6=15pt \ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax The second fraction is now greater.\else The first fraction is still greater.\fi</context> ''What is going on here? The result is weird and unexpected. Why did this happen? Is TeX broken? Let's take a closer look at the real values of the dimensions and their results after division:''<context source="yes">\dimen0=15pt\the\dimen0{}</context>''which has a real internal value of''<context source="yes">\dimen0=15pt\number\dimen0{}</context>''in scaled points. Only the second integer value (in scaled points) is important for our calculation.''''We also need to know the internal value of the second dimension:''<context source="yes">\dimen2=20pt\number\dimen2</context>''Finally, we would like to know the result of the division of these two numbers:''<context source="yes">\dimen0=15pt\dimen1=20pt\number\dimexpr\dimen0/\dimen2\relax</context>{{todo|Fix me!}} ''The result of the integer division is 1 (rather than the decimal 0.75); This illustrates that the result of dividing two numbers that are nearly equal within TeX is not very useful. We can get around this by multiplying the numerators by some large factor, assuring that the evaluated fractions remain integer:''<context source="yes">\dimen0=15pt\dimen2=20pt\dimen4=15pt%\dimen6=30pt\dimen6=15pt \ifdim\dimexpr100\dimen0/\dimen2\relax<\dimexpr100\dimen4/\dimen6\relax The second fraction is now greater.\else The first fraction is still greater.\fi</context> == LuaTeX == ''Alternatively, we can use luaTeX to reliably get the correct result. The <code>\type{tex.dimen}</code> command allows us to access the <code>\dimen</code> register from TeX in Lua.'' <context source="yes">\dimen0=15pt\dimen2=20pt\dimen4=15pt\dimen6=30pt \startluacodeif tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then tex.sprint("The second fraction is greater.")else tex.sprint("The first fraction is greater.")end\stopluacode</context>{{todo|I guess that the online conTeXt doesn't do luacode...}} ''and'' <context source="yes">\dimen0=15pt\dimen2=20pt\dimen4=15pt%\dimen6=30pt\dimen6=15pt \startluacodeif tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then tex.sprint("The second fraction is now greater.")else tex.sprint("The first fraction is still greater.")end\stopluacode</context> ''The output is now correct in both cases without making use of any tricks.''
148

edits

Navigation menu