Changes

Jump to navigation Jump to search
4,100 bytes added ,  16:48, 8 June 2020
m
no edit summary
ConTeXt implements extensions from eTeX including the evaluation of expressions
(quoted from the [[http://www.ntg.nl/maps/20/38.pdf| eTeX manual]]):
== Expressions ==
as first value after the <code>\dimexpr</code> command and then the numeric values.
Notice that the following expression is also valid:
<texcode>
\placesidebyside {\externalfigure[cow][width=.4\textwidth]}
{\framed[width=.4\textwidth,height=\dimexpr.4\textwidth*200/275\relax]
{\vbox{this is a box}}}
</texcode>
<contextsource="yes">
\setupexternalfigures[location={local,default}]
\placesidebyside{\externalfigure[cow][width=.4\textwidth]}
</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\dimen2=20pt\number\dimexpr\dimen0/\dimen2\relax</context> ''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> The important point to keep in mind is that TeX performs '''integer''' arithmetic in units of scaled points.Therefore, one must be careful, especially when using divisions that may lead to integer rounding. == LuaTeX == ''Alternatively, we can use luaTeX to reliably get the correct result. The <code>tex.dimen</code> command allows us to access the <code>\dimen</code> register from TeX in Lua.'' <texcode>\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</texcode> <context>\dimen0=15pt\dimen2=20pt\dimen4=15pt\dimen6=30pt \ifdim\dimexpr100\dimen0/\dimen2\relax<\dimexpr100\dimen4/\dimen6\relax The second fraction is greater.\else The first fraction is greater.\fi</context> ''and'' <texcode>\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</texcode> <context>\dimen0=15pt\dimen2=20pt\dimen4=15pt\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> ''The output is now correct in both cases without making use of any tricks.'' [[Category:Programming and Databases]][[Category:Tools]]

Navigation menu