Hi, Can I divide dimensions to get a number in pdftex? I have four dimensions \!!dimena, \!!dimenb, \!!dimenc and \!!dimend, and I want if (dimena/dimenb) > (dimenc/dimend) do something else do otherthing Any hints on how to do this in ConTeXt. Thanks, Aditya
Am 21.03.2009 um 23:26 schrieb Arthur Reutenauer:
Can I divide dimensions to get a number in pdftex?
LaTeX has a package to do exactly that, fp. You could look into its code. Or you could use LuaTeX :-)
It's easy to do with etex but I would use lua if possible. \starttext \unprotect \!!dimena=1cm \!!dimenb=2cm \!!dimenc=1cm \!!dimend=3cm \ifdim\dimexpr\!!dimena*100/\!!dimenb\relax>\dimexpr\!!dimenc*100/\!! dimend\relax this \else that \fi \!!dimend=1cm \ifdim\dimexpr\!!dimena*100/\!!dimenb\relax>\dimexpr\!!dimenc*100/\!! dimend\relax this \else that \fi \protect \stoptext Wolfgang
On Sat, 21 Mar 2009, Wolfgang Schuster wrote:
Am 21.03.2009 um 23:26 schrieb Arthur Reutenauer:
Can I divide dimensions to get a number in pdftex?
LaTeX has a package to do exactly that, fp. You could look into its code. Or you could use LuaTeX :-)
It's easy to do with etex but I would use lua if possible.
\starttext \unprotect
\!!dimena=1cm \!!dimenb=2cm \!!dimenc=1cm \!!dimend=3cm
\ifdim\dimexpr\!!dimena*100/\!!dimenb\relax>\dimexpr\!!dimenc*100/\!!dimend\relax this \else that \fi
Ah. I was using \dimexpr\!!dimena/\!!dimenb with crazy results. So the trick is to mulitply by a number in between. Thanks Wolfgang. Aditya
Am 21.03.2009 um 23:49 schrieb Aditya Mahajan:
\ifdim\dimexpr\!!dimena*100/\!!dimenb\relax>\dimexpr\!! dimenc*100/\!!dimend\relax this \else that \fi
Ah. I was using \dimexpr\!!dimena/\!!dimenb with crazy results. So the trick is to mulitply by a number in between. Thanks Wolfgang.
Yes because you're working with integers and dividing a number by a bigger number results in zero (that's why you could also use \numexpr above). Wolfgang
Can someone explain this? The solutions posted in lua seem more complicated than that using \dimexpr I will add further examples to http://wiki.contextgarden.net/Expressions (or someone else can do this directly, of course) once I understand the advantages and limitations. Alan On Saturday 21 March 2009 23:41:19 Wolfgang Schuster wrote:
Can I divide dimensions to get a number in pdftex?
LaTeX has a package to do exactly that, fp. You could look into its code. Or you could use LuaTeX :-)
It's easy to do with etex but I would use lua if possible
Can someone explain this?
tex.dimen is a Lua table containing the dimension registers, in scaled points. Hence, if \!!dimena is 1pt, tex.dimen["!!dimena"] will be 65536, for instance. The rest is algebra and Lua control structures.
The solutions posted in lua seem more complicated than that using \dimexpr
If you mean from the number of characters, maybe. Then again, it's only about comparing a / b to c / d in both cases. It would be interesting to compare their efficiency. Arthur
Am 23.03.2009 um 08:15 schrieb Alan BRASLAU:
Can someone explain this? The solutions posted in lua seem more complicated than that using \dimexpr
I will add further examples to http://wiki.contextgarden.net/Expressions (or someone else can do this directly, of course) once I understand the advantages and limitations.
Here is a example document. Wolfgang \starttext \setupwhitespace[line] Our todays question is how to divide two dimension and compare their result with the result of another division, to do this we assign values two four dimensions. \startbuffer \dimen0=15pt \dimen2=20pt \dimen4=15pt \dimen6=30pt \stopbuffer \typebuffer \getbuffer Because we can use \ETEX\ functions with \CONTEXT\ let's try to use them, the following example should work: \startbuffer \ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax Second number is bigger. \else First number is bigger. \fi \stopbuffer \typebuffer \getbuffer This seems to be working but did it also work when the first result of the second division is bigger. \startbuffer \dimen6=15pt \ifdim\dimexpr\dimen0/\dimen2\relax<\dimexpr\dimen4/\dimen6\relax Second number is bigger. \else First number is bigger. \fi \stopbuffer \typebuffer \getbuffer This seems to be weird because we still get as result that the first number is bigger but why did this happen? Let's take a closer look at the real values of the dimensions and their results after the divison. Dimen0 has a value of \the\dimen0{} which is \number\dimen0{} in scaled points. Only the second value in scaled points is important for our calculation, we wan't to also know what the value of the second dimensions is: \number \dimen2. The last we want to know is the result of the division from the two number, this is \number\dimexpr\dimen0/\dimen2\relax. Before I want to say if this number is or good let's see what the value of the second number is before we changed the value for dimension 6 and after we changed it: \startlines Before: \dimen6=30pt \number\dimexpr\dimen4/\dimen6\relax After: \dimen6=15pt \number\dimexpr\dimen4/\dimen6\relax \stoplines The result is always 1 and not very helpful for us, you can see, if you to divide two number which are nearly equal the result in \TEX\ is not ver helpful, to compensate this we can multiply the first number in the division to get a better result. We try this first with the pure number before we include this in the \type{\ifdim} text we wanted. \startlines Result 1: \number\dimexpr\dimen0*100/\dimen2\relax Result 2: \number\dimexpr\dimen4*100/\dimen6\relax \stoplines This gives us now to higher number we can compare with reliable results. We will now try to test both this with our number above again. \startbuffer \dimen6=30pt \ifdim\dimexpr\dimen0*100/\dimen2\relax<\dimexpr \dimen4*100/\dimen6\relax Second number is bigger. \else First number is bigger. \fi \stopbuffer \typebuffer \getbuffer \startbuffer \dimen6=15pt \ifdim\dimexpr\dimen0*100/\dimen2\relax<\dimexpr \dimen4*100/\dimen6\relax Second number is bigger. \else First number is bigger. \fi \stopbuffer \typebuffer \getbuffer You can see both tests give us a correct result after we multiplied the first number in each division by 100 but should we rely on such limitations in \TEX\ when we can also use \LUATEX\ to get a correct result. The \type{tex.dimen} function allows us to access the dimen register from \TEX\ in Lua. \startbuffer \dimen6=30pt \startluacode if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then tex.sprint("Second number is bigger.") else tex.sprint("First number is bigger.") end \stopluacode \stopbuffer \typebuffer \getbuffer \startbuffer \dimen6=15pt \startluacode if tex.dimen[0] / tex.dimen[2] < tex.dimen[4] / tex.dimen[6] then tex.sprint("Second number is bigger.") else tex.sprint("First number is bigger.") end \stopluacode \stopbuffer \typebuffer \getbuffer The output is now correct in both values without any tricks. \stoptext
Hello Wolfgang, Thanks for the detailed explanation.
The \type{tex.dimen} function allows us to access the dimen register from \TEX\ in Lua.
A small correction: it's not a function, it really behaves like a table in Lua. You can use both the register number, or the symbolic name (as defined by \dimendef, if applicable), as a key to the table. Arthur
Great! I added a section to the wiki http://wiki.contextgarden.net/Expressions taking liberties to modify the presentation slightly. A few points are broken, though, notably that the online ConTeXt doesn't appear to do luacode... Alan On Monday 23 March 2009 13:12:54 Wolfgang Schuster wrote:
Here is a example document.
On Tue, Mar 24, 2009 at 00:27, Alan BRASLAU wrote:
Great! I added a section to the wiki http://wiki.contextgarden.net/Expressions taking liberties to modify the presentation slightly.
A few points are broken, though, notably that the online ConTeXt doesn't appear to do luacode...
True. We don't want users to experiment with os.execute. Mojca
Mojca Miklavec wrote:
On Tue, Mar 24, 2009 at 00:27, Alan BRASLAU wrote:
Great! I added a section to the wiki http://wiki.contextgarden.net/Expressions taking liberties to modify the presentation slightly.
A few points are broken, though, notably that the online ConTeXt doesn't appear to do luacode...
True. We don't want users to experiment with os.execute.
actually, the code for a more secure handling has been present from the start ... one can overload the normal executer --~ executer.register('.*') --~ executer.register('*') --~ executer.register('dir','ls') --~ executer.register('dir') --~ executer.finalize() --~ executer.execute('dir',"*.tex") --~ executer.execute("dir *.tex") --~ executer.execute("ls *.tex") --~ os.execute('ls') reminds me to add the exec and spawn variants when i have time i will add some commandline argument to control it Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Am 21.03.2009 um 23:26 schrieb Arthur Reutenauer:
Can I divide dimensions to get a number in pdftex?
LaTeX has a package to do exactly that, fp. You could look into its code. Or you could use LuaTeX :-)
\startluacode if tex.dimen["!!dimena"] / tex.dimen["!!dimenb"] > tex.dimen["!! dimenc"] / tex.dimen["!!dimend"] then tex.sprint("this") else tex.sprint("that") end \stopluacode Wolfgang
Dnia Sat, Mar 21, 2009 at 06:08:41PM -0400, Aditya Mahajan napisał(a):
Hi,
Can I divide dimensions to get a number in pdftex?
I have four dimensions \!!dimena, \!!dimenb, \!!dimenc and \!!dimend, and I want
if (dimena/dimenb) > (dimenc/dimend) do something else do otherthing
What about if (dimena*dimend) > (dimenc*dimenb) ... ? If dimenb and dimend are positive, this should be equivalent (assuming you don't run into overflow).
Any hints on how to do this in ConTeXt.
Thanks, Aditya
Regards -- Marcin Borkowski (http://mbork.pl) Ja nie chcę czytać tych głupich gazet I wymyślonych oglądać historii. (Chili My)
On Sun, 22 Mar 2009, Marcin Borkowski wrote:
Dnia Sat, Mar 21, 2009 at 06:08:41PM -0400, Aditya Mahajan napisał(a):
Hi,
Can I divide dimensions to get a number in pdftex?
I have four dimensions \!!dimena, \!!dimenb, \!!dimenc and \!!dimend, and I want
if (dimena/dimenb) > (dimenc/dimend) do something else do otherthing
What about
if (dimena*dimend) > (dimenc*dimenb) ... ?
If dimenb and dimend are positive, this should be equivalent (assuming you don't run into overflow).
I tried that, but that leads to overflow. Aditya
Aditya Mahajan wrote:
Hi,
Can I divide dimensions to get a number in pdftex?
I have four dimensions \!!dimena, \!!dimenb, \!!dimenc and \!!dimend, and I want
depends on what you want to achive as there are many possibilities \number\dimexp 123pt\relax \number\dimexp 123pt/65536\relax there's also \withoutpt anyhow, you code ... \dimen0=10pt \dimen2=20pt \dimen4=30pt \dimen6=40pt \ifdim \dimexpr\dimen0/\dimen2\relax > \dimexpr\dimen4/\dimen6\relax do something \else do otherthing \fi Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (7)
-
Aditya Mahajan
-
Alan BRASLAU
-
Arthur Reutenauer
-
Hans Hagen
-
Marcin Borkowski
-
Mojca Miklavec
-
Wolfgang Schuster