Hi all, again, I'm playing a bit with processing my xml in lua. I want a simple interface for processing tables (I don't need all the power and complexity of cals tables and want to learn something in the process). And I thought that collecting the setups in lua might be the easiest way, but so far, I haven't been successful. Here's an example: \startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer \startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups \xmlregistersetup{xml:testsetups} \startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:nattable \startluacode framestate = lxml.att("#1", "frame") context(framestate) context(true) context.placefigure( { "here" }, "none" , function() context.setupTABLE( { "frame=on" } ) context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups \startxmlsetups xml:tr \startluacode context.bTR() context.xmlflush("#1") context.eTR() \stopluacode \stopxmlsetups \startxmlsetups xml:td \startluacode context.bTD() context.xmlflush("#1") context.eTD() \stopluacode \stopxmlsetups \starttext \xmlprocessbuffer{main}{test}{} \stoptext question: I have the value "on" in the lua variable "framestate." But how can I pass this on to the line context.setupTABLE( { "frame=on" } )? I tried something like context.setupTABLE( { "frame=" .. framestate } ) but that doesn't work. Any suggestions? Thomas
Hi Thomas, Am 22.10.2011 18:46, schrieb Thomas A. Schmitz:
Hi all,
again, I'm playing a bit with processing my xml in lua. I want a simple interface for processing tables (I don't need all the power and complexity of cals tables and want to learn something in the process). And I thought that collecting the setups in lua might be the easiest way, but so far, I haven't been successful. Here's an example:
\startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer
\startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups
\xmlregistersetup{xml:testsetups}
\startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:nattable \startluacode framestate = lxml.att("#1", "frame") context(framestate) context(true) context.placefigure( { "here" }, "none" , function() context.setupTABLE( { "frame=on" } ) context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups
\startxmlsetups xml:tr \startluacode context.bTR() context.xmlflush("#1") context.eTR() \stopluacode \stopxmlsetups
\startxmlsetups xml:td \startluacode context.bTD() context.xmlflush("#1") context.eTD() \stopluacode \stopxmlsetups
\starttext \xmlprocessbuffer{main}{test}{} \stoptext
question: I have the value "on" in the lua variable "framestate." But how can I pass this on to the line context.setupTABLE( { "frame=on" } )? I tried something like context.setupTABLE( { "frame=" .. framestate } )
"frame=" .. tostring(framestate) seems to work. Anyhow, if you are planning to do some more complex stuff, you should separate the lua from the tex code. The advantages are - no catcode limitations Then you can use nice stuff like string.format("frame=%s",framestate) - in case of lua errors you get the real line number - lua syntax check is possible Load the lua code at the beginning of your tex file with \registerctxluafile{filename_without_suffix}{version number or empty} One last but important tip: if you get lua errors like 'undefined whatever', check your lua file with 'luatexc -p foo.lua' (lua compiler). A single syntax error in the lua file and all of its content is ignored! Can be quite frustrating to find the error cause, if all of your lua code is undefined right from the beginning. Best wishes, Peter PS: lua programming is most efficient when done in direct moon light, while drinking a glass of fresh goat milk! Mh, can't find the link to this interesting statistic right now... so you just have to believe me and give it a try.. :-)
Hi Peter, thanks for your reply, and I'll keep the bit about goat milk in mind :-).
"frame=" .. tostring(framestate)
It has the advantage of making the example compile. It has the drawback of not doing anything :-)
seems to work. Anyhow, if you are planning to do some more complex stuff, you should separate the lua from the tex code.
The advantages are - no catcode limitations Then you can use nice stuff like string.format("frame=%s",framestate) - in case of lua errors you get the real line number - lua syntax check is possible
Load the lua code at the beginning of your tex file with
\registerctxluafile{filename_without_suffix}{version number or empty}
One last but important tip: if you get lua errors like 'undefined whatever', check your lua file with 'luatexc -p foo.lua' (lua compiler). A single syntax error in the lua file and all of its content is ignored! Can be quite frustrating to find the error cause, if all of your lua code is undefined right from the beginning.
OK, that sounds like very good advice - I'll see if I can manage to apply this for xml style sheets. Thanks a lot, and best wishes Thomas
Am 23.10.2011 16:37, schrieb Thomas A. Schmitz:
Hi Peter,
thanks for your reply, and I'll keep the bit about goat milk in mind :-).
"frame=" .. tostring(framestate)
It has the advantage of making the example compile. It has the drawback of not doing anything :-)
(sadly) true :-) I will look into it. Looks like I need a big glass of goat milk before (can't wait for the moon)...
seems to work. Anyhow, if you are planning to do some more complex stuff, you should separate the lua from the tex code.
The advantages are - no catcode limitations Then you can use nice stuff like string.format("frame=%s",framestate) - in case of lua errors you get the real line number - lua syntax check is possible
Load the lua code at the beginning of your tex file with
\registerctxluafile{filename_without_suffix}{version number or empty}
One last but important tip: if you get lua errors like 'undefined whatever', check your lua file with 'luatexc -p foo.lua' (lua compiler). A single syntax error in the lua file and all of its content is ignored! Can be quite frustrating to find the error cause, if all of your lua code is undefined right from the beginning.
OK, that sounds like very good advice - I'll see if I can manage to apply this for xml style sheets.
Thanks a lot, and best wishes
Thomas
Am 22.10.2011 18:46, schrieb Thomas A. Schmitz:
Hi all,
again, I'm playing a bit with processing my xml in lua. I want a simple interface for processing tables (I don't need all the power and complexity of cals tables and want to learn something in the process). And I thought that collecting the setups in lua might be the easiest way, but so far, I haven't been successful. Here's an example:
\startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer
\startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups
\xmlregistersetup{xml:testsetups}
\startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:nattable \startluacode framestate = lxml.att("#1", "frame")
context(type(framestate)) Here is the problem. The function lxml.att() only prints the attribute, but returns nothing (framestate = nil). I looked into an old example of mine where I played with attributes, but it's totally outdated (uses xml.filters, now mkii only). Sorry, no solution (goat milk was out). Peter [lxml-tex.lua] function lxml.attribute(id,pattern,a,default) local collected = xmlapplylpath(getid(id),pattern) if collected then attribute(collected,a,default) end end local function attribute(collected,a,default) if collected and #collected > 0 then local at = collected[1].at local str = (at and at[a]) or default if str and str ~= "" then contextsprint(notcatcodes,str) end elseif default then contextsprint(notcatcodes,default) end end
context(framestate)
does nothing
context(true) context.placefigure( { "here" }, "none" , function() context.setupTABLE( { "frame=on" } ) context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups
\startxmlsetups xml:tr \startluacode context.bTR() context.xmlflush("#1") context.eTR() \stopluacode \stopxmlsetups
\startxmlsetups xml:td \startluacode context.bTD() context.xmlflush("#1") context.eTD() \stopluacode \stopxmlsetups
\starttext \xmlprocessbuffer{main}{test}{} \stoptext
question: I have the value "on" in the lua variable "framestate." But how can I pass this on to the line context.setupTABLE( { "frame=on" } )? I tried something like context.setupTABLE( { "frame=" .. framestate } ) but that doesn't work. Any suggestions?
Thomas ___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
On 22-10-2011 18:46, Thomas A. Schmitz wrote:
Hi all,
again, I'm playing a bit with processing my xml in lua. I want a simple interface for processing tables (I don't need all the power and complexity of cals tables and want to learn something in the process). And I thought that collecting the setups in lua might be the easiest way, but so far, I haven't been successful. Here's an example:
\startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer
\startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups
\xmlregistersetup{xml:testsetups}
\startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:nattable \startluacode framestate = lxml.att("#1", "frame") context(framestate) context(true) context.placefigure( { "here" }, "none" , function() context.setupTABLE( { "frame=on" } ) context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups
\startxmlsetups xml:tr \startluacode context.bTR() context.xmlflush("#1") context.eTR() \stopluacode \stopxmlsetups
\startxmlsetups xml:td \startluacode context.bTD() context.xmlflush("#1") context.eTD() \stopluacode \stopxmlsetups
\starttext \xmlprocessbuffer{main}{test}{} \stoptext
question: I have the value "on" in the lua variable "framestate." But how can I pass this on to the line context.setupTABLE( { "frame=on" } )? I tried something like context.setupTABLE( { "frame=" .. framestate } ) but that doesn't work. Any suggestions?
why so complex \startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> <nattable frame="off"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> <nattable frame="no"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer \startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups \xmlregistersetup{xml:testsetups} \startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups \xmlmapvalue {nattable:frame} {on} {on} \xmlmapvalue {nattable:frame} {yes} {on} \xmlmapvalue {nattable:frame} {off} {off} \xmlmapvalue {nattable:frame} {no} {off} \startxmlsetups xml:nattable \placefigure [here,none] {} { \setupTABLE[frame=\xmlval{nattable:frame}{\xmlatt{#1}{frame}}{on}] \bTABLE \xmlflush{#1} \eTABLE } \stopxmlsetups \startxmlsetups xml:tr \bTR \xmlflush{#1} \eTR \stopxmlsetups \startxmlsetups xml:td \bTD \xmlflush{#1} \eTD \stopxmlsetups \starttext \xmlprocessbuffer{main}{test}{} \stoptext In x-cals.lua / mkiv you can see an example of pure lua solution. It boils down to applying a function the the whole table element. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/24/2011 12:17 PM, Hans Hagen wrote:
why so complex
Hi Hans, yes, I'm aware that this procedure is quite silly for this example, this was just for myself, for educational purposes. As soon as the setup becomes more complex, I thought it might be easier to collect and process the options in lua than in tex (as I have done before), but I'm running against a wall here (see my exchange with Peter about goat milk yesterday). So the real question is: how can I collect data such as an attribute value to process it with lua, say in the form if lxml.att(t, "frame") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end I don't really understand what kind of object a call such as lxml.att(t, "frame") produces. All best Thomas
On 24-10-2011 14:18, Thomas A. Schmitz wrote:
On 10/24/2011 12:17 PM, Hans Hagen wrote:
why so complex
Hi Hans,
yes, I'm aware that this procedure is quite silly for this example, this was just for myself, for educational purposes. As soon as the setup becomes more complex, I thought it might be easier to collect and process the options in lua than in tex (as I have done before), but I'm running against a wall here (see my exchange with Peter about goat milk yesterday). So the real question is: how can I collect data such as an attribute value to process it with lua, say in the form
if lxml.att(t, "frame") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end
I don't really understand what kind of object a call such as lxml.att(t, "frame") produces.
all lxml.* calls print something to tex, so when you're at the lua end and want to stay there you should use the xml.* methods; also, when you have an element, say e then there is an e.at table that has all attributes ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/24/2011 02:26 PM, Hans Hagen wrote:
if lxml.att(t, "frame") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end
I don't really understand what kind of object a call such as lxml.att(t, "frame") produces.
all lxml.* calls print something to tex, so when you're at the lua end and want to stay there you should use the xml.* methods; also, when you have an element, say e then there is an e.at table that has all attributes
OK, excuse me for being particularly thick today: there doesn't seem to be xml.att, only xml.attribute, so it should be something like: function tableinit(t) if xml.attribute(t, "/", "frame", "off") == "on" then context("Yes, the framestate is really on!") else context("Nope, sorry, it's still off.") end end (if I understand the definition of xml.attribute correctly: id,pattern,a,default)? Thomas
On 24-10-2011 19:00, Thomas A. Schmitz wrote:
On 10/24/2011 02:26 PM, Hans Hagen wrote:
if lxml.att(t, "frame") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end
I don't really understand what kind of object a call such as lxml.att(t, "frame") produces.
all lxml.* calls print something to tex, so when you're at the lua end and want to stay there you should use the xml.* methods; also, when you have an element, say e then there is an e.at table that has all attributes
OK, excuse me for being particularly thick today: there doesn't seem to be xml.att, only xml.attribute, so it should be something like:
function tableinit(t) if xml.attribute(t, "/", "frame", "off") == "on" then context("Yes, the framestate is really on!") else context("Nope, sorry, it's still off.") end end
(if I understand the definition of xml.attribute correctly: id,pattern,a,default)?
sure, oryou can play with if xml.filter(t,".[@frame=on]) then the x-*.lua show some tricks Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/24/11 8:48 PM, Hans Hagen wrote:
sure, oryou can play with
if xml.filter(t,".[@frame=on]) then
the x-*.lua show some tricks
OK, I'm in my "stubborn" mode then: why doesn't this work: \startbuffer[test] <a> <nattable frame="on"> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> </nattable> </a> \stopbuffer \startxmlsetups xml:testsetups \xmlsetsetup{main}{a|nattable|tr|td|}{xml:*} \stopxmlsetups \xmlregistersetup{xml:testsetups} \startxmlsetups xml:a \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:nattable \startluacode if xml.attribute(t, "/", "frame", "off") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end context(true) context.placefigure( { "here" }, "none" , function() context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups \startxmlsetups xml:tr \startluacode context.bTR() context.xmlflush("#1") context.eTR() \stopluacode \stopxmlsetups \startxmlsetups xml:td \startluacode context.bTD() context.xmlflush("#1") context.eTD() \stopluacode \stopxmlsetups \starttext \xmlprocessbuffer{main}{test}{} \stoptext I get a lua error here. Thomas
Am 25.10.2011 um 11:43 schrieb Thomas A. Schmitz:
On 10/24/11 8:48 PM, Hans Hagen wrote:
sure, oryou can play with
if xml.filter(t,".[@frame=on]) then
the x-*.lua show some tricks
OK, I'm in my "stubborn" mode then: why doesn't this work:
[…]
\startxmlsetups xml:nattable \startluacode if xml.attribute(t, "/", "frame", "off") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end
if xml.attribute(lxml.id("#1"), "/", "frame", "off") == "on" then % context("Yes, the frame is really on!") else % context("Nope, sorry, it's still off.") end % Wolfgang
On 25-10-2011 11:43, Thomas A. Schmitz wrote:
\startxmlsetups xml:nattable \startluacode if xml.attribute(t, "/", "frame", "off") == "on" then context("Yes, the frame is really on!") else context("Nope, sorry, it's still off.") end context(true) context.placefigure( { "here" }, "none" , function() context.bTABLE() context.xmlflush("#1") context.eTABLE() end) \stopluacode \stopxmlsetups
see Wolfgangs answer anyway, best move the lua code and wrap it in a function "document.MyWhatever" ... now its get defined each time Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/25/11 12:17 PM, Hans Hagen wrote:
see Wolfgangs answer
anyway, best move the lua code and wrap it in a function "document.MyWhatever" ... now its get defined each time
Hans
Wolfgang, Hans, thanks a lot, this works now. Will now try and move the code to a ctxluafile and see if I can make it work again. If not, I'll be back; no good deed goes unpunished... Thomas
On 10/25/2011 12:27 PM, Thomas A. Schmitz wrote:
Wolfgang, Hans,
thanks a lot, this works now. Will now try and move the code to a ctxluafile and see if I can make it work again. If not, I'll be back; no good deed goes unpunished...
Thomas
OK, I'm slowly making progress processing xml in lua. One naive question now: how can I have access to and manipulate the textwidth dimension? In other words, given 0.4\textwidth what would be the equivalent in lua code? I tried 0.4 * tex.dimen.textwidth or 0.4 * tex.dimen["textwidth"] but couldn't make that work. Also tried grepping the tex/context/base/*.lua files for width, but couldn't find anything immediately obvious. Thanks! Thomas
Am 27.10.2011 um 11:23 schrieb Thomas A. Schmitz:
On 10/25/2011 12:27 PM, Thomas A. Schmitz wrote:
Wolfgang, Hans,
thanks a lot, this works now. Will now try and move the code to a ctxluafile and see if I can make it work again. If not, I'll be back; no good deed goes unpunished...
Thomas
OK, I'm slowly making progress processing xml in lua. One naive question now: how can I have access to and manipulate the textwidth dimension? In other words, given
0.4\textwidth
what would be the equivalent in lua code? I tried
0.4 * tex.dimen.textwidth
or
0.4 * tex.dimen["textwidth"]
but couldn't make that work. Also tried grepping the tex/context/base/*.lua files for width, but couldn't find anything immediately obvious.
With “tex.dimen[…]” you get the value in scaled points but util-dim.lua provides some functions to convert the value in points, centimeter etc. \starttext \startluacode context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) } \stopluacode \stoptext Wolfgang
On 10/27/2011 11:53 AM, Wolfgang Schuster wrote:
With “tex.dimen[…]” you get the value in scaled points but util-dim.lua provides some functions to convert the value in points, centimeter etc.
\starttext \startluacode context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) } \stopluacode \stoptext
Thank you Wolfgang, that's exactly what I was looking for! All best Thomas
On 27-10-2011 12:32, Thomas A. Schmitz wrote:
On 10/27/2011 11:53 AM, Wolfgang Schuster wrote:
With “tex.dimen[…]” you get the value in scaled points but util-dim.lua provides some functions to convert the value in points, centimeter etc.
\starttext \startluacode context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) } \stopluacode \stoptext
Thank you Wolfgang, that's exactly what I was looking for!
or just tex.dimen["textwidth"]/2 .. "sp" ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/27/11 13:05, Hans Hagen wrote:
With “tex.dimen[…]” you get the value in scaled points but util-dim.lua provides some functions to convert the value in points, centimeter etc.
\starttext \startluacode context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) } \stopluacode \stoptext
Thank you Wolfgang, that's exactly what I was looking for!
or just tex.dimen["textwidth"]/2 .. "sp"
Wait, just so I understand: your solution would imply that tex.dimen["textwidth"] holds a number, not a dimension, right? (Because you simply concatenate it with a dimension unit). Which makes sense when I think of it because lua has no concept of dimensions, only of strings, functions, tables, numbers... Whereas the Wolfgang implies that the result is already in sp. So who's right? Thomas
On Fri, Oct 28, 2011 at 8:23 AM, Thomas A. Schmitz
On 10/27/11 13:05, Hans Hagen wrote:
With “tex.dimen[…]” you get the value in scaled points but util-dim.lua provides some functions to convert the value in points, centimeter etc.
\starttext \startluacode context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) } \stopluacode \stoptext
Thank you Wolfgang, that's exactly what I was looking for!
or just tex.dimen["textwidth"]/2 .. "sp"
Wait, just so I understand: your solution would imply that tex.dimen["textwidth"] holds a number, not a dimension, right? (Because you simply concatenate it with a dimension unit). Which makes sense when I think of it because lua has no concept of dimensions, only of strings, functions, tables, numbers... Whereas the Wolfgang implies that the result is already in sp. So who's right? both -- where is the problem ? tex.dimen["textwidth"]/2 .. "sp" gives "xyzsp"
number.topoints(tex.dimen["textwidth"]/2) gives "XYZ.ABSpt" TeX reads "xyzsp" and drops sp or TeX reads "XYZ.ABC.pt" and convert to "xyzsp" and drops sp The problem is that tex.dimen["textwidth"]/2 gives a number, while TeX wants a dimension there PS both numbers are signed ( and maybe with spaces here and there, I don't remember) -- luigi
On Fri, Oct 28, 2011 at 8:37 AM, luigi scarso
number.topoints(tex.dimen["textwidth"]/2) gives "XYZ.ABSpt" number.topoints(tex.dimen["textwidth"]/2) gives "XYZ.ABCpt"
TeX reads "XYZ.ABC.pt" and convert to "xyzsp" and drops sp TeX reads "XYZ.ABCpt" and convert to "xyzsp" and drops sp
-- luigi
context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) }
or just tex.dimen["textwidth"]/2 .. "sp"
Wait, just so I understand: your solution would imply that tex.dimen["textwidth"] holds a number, not a dimension, right? (Because you simply concatenate it with a dimension unit). Which makes sense when I think of it because lua has no concept of dimensions, only of strings, functions, tables, numbers... Whereas the Wolfgang implies that the result is already in sp. So who's right?
both :) tex.dimen holds a number, that is the size in points, multiplied by 65536/1pt, so for example 3pt becomes 3pt * 65536 ----------- = 3 * 65536 = 196608 1pt This number is also known "sp". So if you store the number 3*65536 in tex.dimen[...], you can say: my width is \directlua{ tex.dimen[...] / 2}sp (results to "my width is 196608sp", which you can use as an argument to whatever needs a length) or my width is \directlua{ number_to_points(tex.dimen[...]) } where number_to_points is something like number_to_points = function (amount_in_sp) in_pt = tostring(amount_in_sp / 65536) return in_pt .. "pt" end which gives "3pt". Patrick
On 10/28/11 08:44, Patrick Gundlach wrote:
context.blackrule{ width = number.topoints(tex.dimen["textwidth"]/2) }
or just tex.dimen["textwidth"]/2 .. "sp"
Wait, just so I understand: your solution would imply that tex.dimen["textwidth"] holds a number, not a dimension, right? (Because you simply concatenate it with a dimension unit). Which makes sense when I think of it because lua has no concept of dimensions, only of strings, functions, tables, numbers... Whereas the Wolfgang implies that the result is already in sp. So who's right?
both :)
tex.dimen holds a number, that is the size in points, multiplied by 65536/1pt, so for example 3pt becomes
3pt * 65536 ----------- = 3 * 65536 = 196608 1pt
This number is also known "sp". So if you store the number 3*65536 in tex.dimen[...], you can say:
my width is \directlua{ tex.dimen[...] / 2}sp (results to "my width is 196608sp", which you can use as an argument to whatever needs a length)
or
my width is \directlua{ number_to_points(tex.dimen[...]) } where number_to_points is something like
number_to_points = function (amount_in_sp) in_pt = tostring(amount_in_sp / 65536) return in_pt .. "pt" end
which gives "3pt".
Patrick
Luigi, Patrick, thanks for your explanations! The point of my question was: can I feed the content of tex.dimen["textwidth"] directly back to TeX, and the answer to this appears to be "no"; you need to add some unit to it (otherwise, you get an error message). Which was a bit confusing to me at first, because the name tex.dimen implies that it holds a "real" dimension, like \newdim does. Thomas
Am 28.10.2011 um 08:55 schrieb Thomas A. Schmitz:
Luigi, Patrick,
thanks for your explanations! The point of my question was: can I feed the content of tex.dimen["textwidth"] directly back to TeX, and the answer to this appears to be "no"; you need to add some unit to it (otherwise, you get an error message). Which was a bit confusing to me at first, because the name tex.dimen implies that it holds a "real" dimension, like \newdim does.
You can get the same value in TeX when you print the value of a dimension with \number: \starttext \scratchdimen=3pt \number\scratchdimen \ctxlua{context(tex.dimen["scratchdimen"])} \stoptext Wolfgang
On 28-10-2011 10:35, Wolfgang Schuster wrote:
Am 28.10.2011 um 08:55 schrieb Thomas A. Schmitz:
Luigi, Patrick,
thanks for your explanations! The point of my question was: can I feed the content of tex.dimen["textwidth"] directly back to TeX, and the answer to this appears to be "no"; you need to add some unit to it (otherwise, you get an error message). Which was a bit confusing to me at first, because the name tex.dimen implies that it holds a "real" dimension, like \newdim does.
You can get the same value in TeX when you print the value of a dimension with \number:
\starttext
\scratchdimen=3pt
\number\scratchdimen
\ctxlua{context(tex.dimen["scratchdimen"])}
\stoptext
which also means that you can say \scratchcounter=\scratchdimen and test dimens as if they are numbers ... this is why we can have \let\ifzeropt\ifcase which probably no one ever noticed in the source. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Hi Thomas,
thanks for your explanations! The point of my question was: can I feed the content of tex.dimen["textwidth"] directly back to TeX, and the answer to this appears to be "no"; you need to add some unit to it (otherwise, you get an error message). Which was a bit confusing to me at first, because the name tex.dimen implies that it holds a "real" dimension, like \newdim does.
Just switch to philosopher mode for a while and ask yourself what implications that would have in the rather fuzzy world of printing. What is a 'real' dimension? What we call points (pt) is in other application also called points but happens to be basepoints in our universe (bp). Also, imagine that in good american tradition the dimension would have been inches while we all moved on to meters ... So, Knuth foresaw this (and also wanted predictable calculations and wanted to avoid unportable floating points) so he came up with his own unit: scaled points. So, a \dimen is just a \count but consider it tagged to show you pt for convenience when printed (\the) and the parser permits you to enter these numbers as pt/bp/dd/cc/cm/mm etc. At the lua end all are just integers (with some limited size but that might change as Taco and I want to play a bit with adding a couple of bytes and see to what extent that will break things). In metapost the internal unit is bp (because it targets at postscript) and there cm, mm etc are just variables that one multiplies with so there you can change the universe by just saying "in := cm". Skips are another story (not to speak of boxes as we do have a dimendef but not a boxdef of inserts which are yet another class of animals). Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
On 10/28/11 10:56, Hans Hagen wrote:
Just switch to philosopher mode for a while and ask yourself what implications that would have in the rather fuzzy world of printing.
What is a 'real' dimension? What we call points (pt) is in other application also called points but happens to be basepoints in our universe (bp). Also, imagine that in good american tradition the dimension would have been inches while we all moved on to meters ...
So, Knuth foresaw this (and also wanted predictable calculations and wanted to avoid unportable floating points) so he came up with his own unit: scaled points. So, a \dimen is just a \count but consider it tagged to show you pt for convenience when printed (\the) and the parser permits you to enter these numbers as pt/bp/dd/cc/cm/mm etc.
At the lua end all are just integers (with some limited size but that might change as Taco and I want to play a bit with adding a couple of bytes and see to what extent that will break things).
In metapost the internal unit is bp (because it targets at postscript) and there cm, mm etc are just variables that one multiplies with so there you can change the universe by just saying "in := cm".
Skips are another story (not to speak of boxes as we do have a dimendef but not a boxdef of inserts which are yet another class of animals).
Thanks for this philosophical explanation - I guess I'll have to reread these passages in the TeX book. I'm looking forward to your experiments :-) Thomas
On Fri, Oct 28, 2011 at 10:56 AM, Hans Hagen
Hi Thomas,
thanks for your explanations! The point of my question was: can I feed the content of tex.dimen["textwidth"] directly back to TeX, and the answer to this appears to be "no"; you need to add some unit to it (otherwise, you get an error message). Which was a bit confusing to me at first, because the name tex.dimen implies that it holds a "real" dimension, like \newdim does.
Just switch to philosopher mode for a while and ask yourself what implications that would have in the rather fuzzy world of printing.
What is a 'real' dimension? What we call points (pt) is in other application also called points but happens to be basepoints in our universe (bp). Also, imagine that in good american tradition the dimension would have been inches while we all moved on to meters ...
So, Knuth foresaw this (and also wanted predictable calculations and wanted to avoid unportable floating points) so he came up with his own unit: scaled points. just to see the floating point in action: \starttext \startluacode context(collectgarbage('count')," KB are ",collectgarbage('count')*1024," byte") \stopluacode \stoptext
collectgarbage('count') returns the total memory in use by Lua (in Kbytes).
At the lua end all are just integers (with some limited size but that might change as Taco and I want to play a bit with adding a couple of bytes and see to what extent that will break things).
hm, I'm bit loss here. In CWEB I see define max_dimen 0x3FFFFFFF so I suppose that it's a kind of int But the type of tex.dimen is number as is the type of math.sqrt(2) as we can see in context(type(tex.dimen['textwidth'])) context(type(math.sqrt(2))) i.e it's a Lua number -- a floating point. So a kind of conversion can happen between a floating point and a sp number (which has a limited range) Of course Lua has its routines, but probably they differs from TeX If I recall correctly, both use 32 bits, but if you extend to lets say 48 bit then the TeX routines are not good anymore. Well, I'm not sure it's right, of course. -- luigi
i.e it's a Lua number -- a floating point. So a kind of conversion can happen between a floating point and a sp number (which has a limited range) Of course Lua has its routines, but probably they differs from TeX If I recall correctly, both use 32 bits, but if you extend to lets say 48 bit then the TeX routines are not good anymore.
Well, I'm not sure it's right, of course.
The lua numbers have enough precision to cast on the tex numbers (with rounding) but in practice I don't care too much. As we have standardized floats the behaviour is predictable. We could probably go completely float at the tex end but too many datastructures expect an integer so it's not worth the trouble. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (6)
-
Hans Hagen
-
luigi scarso
-
Patrick Gundlach
-
Peter Rolf
-
Thomas A. Schmitz
-
Wolfgang Schuster