
Hi, I would like to print in a text the values of a array computed in a \startluacode …. \stopluacode combination. How can I do it properly? Please have a look at the example below and its output, which is not satisfactory because the right parenthesis is separated from the number by a space. How can I suppress this unwanted space? For instance I get (1, 103 ) instead of (1, 103). Thanks in advance for any help, Best regards: OK %%% begin ctxlua-print.tex \starttext \startluacode n = 6 ; vecteurX = {} ; for i = 1,n do vecteurX[i] = i ; end vecteurY = {} ; for i = 1,n do vecteurY[i] = 3*vecteurX[i] + 100 ; end for i = 1,n do context("(") tex.print(vecteurX[i]) context(", ") tex.print(vecteurY[i]) tex.print(")") context.par() end \stopluacode \stoptext %%% begin ctxlua-print.tex

Hi, I think you can use tex.write() instead of tex.print() (pretty much the same as in lua io functions). Hope this helps Joseph De : Otared Kavian Envoyé le :dimanche 19 mars 2017 16:31 À : mailing list for ConTeXt users Objet :[NTG-context] How to use tex.print in ctxlua Hi, I would like to print in a text the values of a array computed in a \startluacode …. \stopluacode combination. How can I do it properly? Please have a look at the example below and its output, which is not satisfactory because the right parenthesis is separated from the number by a space. How can I suppress this unwanted space? For instance I get (1, 103 ) instead of (1, 103). Thanks in advance for any help, Best regards: OK %%% begin ctxlua-print.tex \starttext \startluacode n = 6 ; vecteurX = {} ; for i = 1,n do vecteurX[i] = i ; end vecteurY = {} ; for i = 1,n do vecteurY[i] = 3*vecteurX[i] + 100 ; end for i = 1,n do context("(") tex.print(vecteurX[i]) context(", ") tex.print(vecteurY[i]) tex.print(")") context.par() end \stopluacode \stoptext %%% begin ctxlua-print.tex

On 19.03.2017 16:30, Otared Kavian wrote:
Otared, is there a reason why you don't use context(vecteurY[i]) and context(vecteurX[i])? This gets rid of the spurious space for me (and makes your code more consistent). But I assume you must have tried it because you use the "context" command in other places of your lua code. Thomas

On 3/19/2017 4:51 PM, Thomas A. Schmitz wrote:
always try to use the context command because it deals with catcodes and is more robust than a tex.(s)print Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------

On 03/19/2017 04:30 PM, Otared Kavian wrote:
Hi Otared, is there any reason not to use the following instead of your way below? for i = 1,n do tex.print("(" .. vecteurX[i] .. ", " .. vecteurY[i] .. ")\\par") end
I think this is shorter and clearer (to me, at least). Just in case it helps, Pablo -- http://www.ousia.tk

On 19.03.2017 16:56, Pablo Rodriguez wrote:
I don't think that this is a good approach. Refer to chapter 11.6 of "Programming in Lua" to see why string concatenation is computationally expensive. (It wouldn't matter in this simple case, but it's not a good habit to develop.) All best Thomas

Hi Aditya, Thanks, as Thomas, Pablo and you mentioned the right way is to use context() instead of tex.print() Actually I just saw that one can also concatenate with context() as in: context("(" .. vecteurX[i] .. ", " .. vecteurY[i] .. ")\\par") which is equivalent to tex.print("(" .. vecteurX[i] .. ", " .. vecteurY[i] .. ")\\par") Best regards: OK

Hi Wolfgang, Thanks for letting me know the command string.formatters[….]. It allows to avoid the concatenation, which is to avoid as Thomas points out. Now that I have solved the issue with the spurious space, I have one more question: how could one print the values (vecteuX[i],vecteurY[i]) in a tabulated environment so that when for instance one has 15 such values, one gets 3 lines with 5 values on each line? Best regards: OK

On 3/19/2017 5:56 PM, Wolfgang Schuster wrote:
\starttext \startluacode local string_a = "12" local string_b = "23" context("(%s,%s)",string_a,string_b) context.formatted.bold("(%s,%s)",string_a,string_b) \stopluacode \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (7)
-
Aditya Mahajan
-
Hans Hagen
-
josephcanedo@gmail.com
-
Otared Kavian
-
Pablo Rodriguez
-
Thomas A. Schmitz
-
Wolfgang Schuster