From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/106734 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Gerben Wierda Newsgroups: gmane.comp.tex.context Subject: Solved: Minimum example of problem getting curly braces printed in METAPOST Date: Fri, 3 Apr 2020 17:36:03 +0200 Message-ID: <63D08101-93C1-4DBA-8A80-D7A500DB777A@rna.nl> References: <68992ADC-44A8-410D-90D6-CFCD31EE77F6@rna.nl> Reply-To: mailing list for ConTeXt users Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.14\)) Content-Type: multipart/mixed; boundary="===============7004925488332632084==" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="85956"; mail-complaints-to="usenet@ciao.gmane.io" To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Fri Apr 03 17:36:44 2020 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKON9-000MDW-Ty for gctc-ntg-context-518@m.gmane-mx.org; Fri, 03 Apr 2020 17:36:43 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 53B4E18300E; Fri, 3 Apr 2020 17:36:10 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LZ3bIztbjSCz; Fri, 3 Apr 2020 17:36:08 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 66987183011; Fri, 3 Apr 2020 17:36:08 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id E8768183000 for ; Fri, 3 Apr 2020 17:36:06 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id s18CZs2FRe4C for ; Fri, 3 Apr 2020 17:36:05 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=213.125.118.53; helo=mail.rna.nl; envelope-from=gerben.wierda@rna.nl; receiver= Original-Received: from mail.rna.nl (mail.rna.nl [213.125.118.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 74B56182C24 for ; Fri, 3 Apr 2020 17:36:05 +0200 (CEST) Original-Received: from hermione.rna.nl (hermione.rna.nl [192.168.2.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.rna.nl (Postfix) with ESMTPSA id 397A236A6D82 for ; Fri, 3 Apr 2020 17:36:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rna.nl; s=dkim_rsa2048; t=1585928164; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YAxgnid1xi4OUcMThrxknBqHI9NeA3lUAM7g16rdX6w=; b=aZFcjCgoHw5U0+MUMb8cd6Azkp9s8cKqjdJuoRlErODBL6nyo3ZrBwX9y+xxIdSQTIlrfR 7hZwQCjuj6Ut6V+KHu7IhZJREroPCNa+Ybg7FwMxORFsqpARExyEBYHaFzGwhXd3x71ifZ xHQtt2tukaye5RcDnXYJkxcZpwgyr9lW+kFdb5QCRwJr7WeU0pzElIzU2MxvY9q7wGfKW5 zzFHlq84KRbyQL29+oNV9SctCL/ToMr3Rcp8voPs57g5UPRYzlhangu8HNIXiIDCW4Azt6 8aF2zUkybA5CY9KtYRHTmheaM0tSyAeKYtPl2/Z4V/+sZ1uxvGdGpaLykfxmYA== In-Reply-To: <68992ADC-44A8-410D-90D6-CFCD31EE77F6@rna.nl> X-Mailer: Apple Mail (2.3445.104.14) X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:106734 Archived-At: --===============7004925488332632084== Content-Type: multipart/alternative; boundary="Apple-Mail=_4DED376E-C8ED-4BBD-949D-1F8D8CEE498E" --Apple-Mail=_4DED376E-C8ED-4BBD-949D-1F8D8CEE498E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Thanks to Taco, the solution was to simply use: function doubleQuotableEscapedConTeXtString( str) local rep =3D { [1] =3D { '{', '{\\textbraceleft}' }, [2] =3D { '}', '{\\textbraceright}' }, [3] =3D { '#', '{\\texthash}' }, [4] =3D { '$', '{\\textdollar}' }, [5] =3D { '&', '{\\textampersand}' }, [6] =3D { '%', '{\\textpercent}' }, [7] =3D { '\\','{\\textbackslash}' }, [8] =3D { '|', '{\\textbar}' }, [9] =3D { '_', '{\\textunderscore}' }, [10] =3D { '~', '{\\textasciitilde}' }, [11] =3D { '^', '{\\textasciicircum}' }, [12] =3D { '"', "\"&ditto&\"" }, } return lpeg.replacer(rep):match(str) end And the string becomes something that can safely be given toi METAPOST = and safely handled by TeX when called from METAPOST via textext() G > On 2 Apr 2020, at 14:05, Gerben Wierda wrote: >=20 > Here is a minimum example of a problem that I have in getting curly = braces printed in METAPOST in code that is generated by lua. >=20 > Any help is welcome. >=20 > \usemodule[scite] > \setupxml > [entities=3Dyes] >=20 >=20 > \startluacode >=20 > function warn( ... ) > texio.write_nl("-----> " .. string.format(...)) > end >=20 > local function mpLabelString( xmlLabelString) > -- Returns a string where each " is replaced by a METAPOST = compatible result, except for outer double quotes" > rep =3D { > [1] =3D { "\"", "\"&ditto&\"" }, > -- DOESN'T WORK: [2] =3D { "\\", "\\\\" }, > } > local tmpString =3D string.formatters( "%!tex!", xmlLabelString) > warn( "STRING.FORMAT XML \"%s\"", xmlLabelString) > warn( "STRING.FORMAT TeX-ed \"%s\"", tmpString) > warn( "STRING.FORMAT Replaced \"%s\"", = lpeg.replacer(rep):match(tmpString)) > return lpeg.replacer(rep):match(tmpString) > end >=20 > function warnAndConTeXt( ...) > warn( ...) > context( ...) > end >=20 > function moduledata.test( filename) > local labelString > context( "The string to typeset is:\\par\\type-{Label} \"a\" = [Text]!-") > context( "\\par The attempts are:") > context( "\\par1. \\type-Label Text-") > context( "\\par2. \\type-Label [Text]!-") > context( "\\par3. \\type-Label \"a\" [Text]!-") > context( "\\par4. \\type-{Label} [Text]!-") > context( "\\par5. \\type-{Label} \"a\" [Text]!-") > context.startMPpage { instance =3D "doublefun" } > context( "picture pic;") > labelString =3D "1. Label Text OK" > warnAndConTeXt( "pic :=3D Foo( 0, 0, 150, 50, \"%s\");", = mpLabelString( labelString)) > labelString =3D "2. Label [Text]! OK" > warnAndConTeXt( "pic :=3D Foo( 0, -75, 150, 50, \"%s\");", = mpLabelString( labelString)) > labelString =3D "3. Label \"a\" [Text]! OK" > warnAndConTeXt( "pic :=3D Foo( 0, -150, 150, 50, \"%s\");", = mpLabelString( labelString)) > labelString =3D "4. {Label} [Text]! MISSING curly braces" > warnAndConTeXt( "pic :=3D Foo( 0, -225, 150, 50, \"%s\");", = mpLabelString( labelString)) > labelString =3D "5. {Label} \"a\" [Text]! MISSING curly braces" > warnAndConTeXt( "pic :=3D Foo( 0, -300, 150, 50, \"%s\");", = mpLabelString( labelString)) > context( "drawdot (0,0) withpen pencircle scaled 4 withcolor red;") > context.stopMPpage() > end > \stopluacode >=20 > \usemodule[article-basic] > = %\enabletrackers[metapost.tracingall,metapost.lua,metapost.runs,metapost.t= extexts,metapost.scrintersectionPoints,metapost.runs,metapost.graphics,met= apost.terminal] >=20 > \starttext >=20 > \definefontfamily[mainface][rm][Optima] > \setupbodyfont[mainface,10pt] >=20 > \startMPinclusions[+]{doublefun} >=20 > \stopMPinclusions >=20 > \startMPdefinitions{doublefun} > vardef makeTeXLabel( expr w, h, name) =3D > show "NAME makeTeXLabel:", name; > save p; picture p ; > save s; string s; > s :=3D "\framed{" & name & "}"; % Curly braces will be missing. I = need this to work. > % s :=3D "\type-" & name & "-"; % Curly braces are displayed, but = this must become a vbox in the end, so can't use it > show "SCAN:", s; > p :=3D textext( s); > p > enddef; >=20 > vardef Foo( expr xpos, ypos, width, height, str) =3D > show "NAME Foo:", str; % Backslashes are already gone here > save pic; picture pic; > pic :=3D makeTeXLabel( width, height, str) shifted (xpos, ypos); > draw pic; > pic > enddef; >=20 > \stopMPdefinitions >=20 > \ctxlua{moduledata.test("My ArchiMate Model Export BES.xml")} > \typefile[option=3DTEX]{test11.tex} >=20 > \stoptext >=20 > The question is: how can I get this to work? The strings that have to = be printed inside the METAPOST picture come from an XML and can contain = about anything. But in the end that string will have to be vertically = typeset as a paragraph, hence I cannot use \type (which works). >=20 > >=20 > = __________________________________________________________________________= _________ > If your question is of interest to others as well, please add an entry = to the Wiki! >=20 > maillist : ntg-context@ntg.nl / = http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > wiki : http://contextgarden.net > = __________________________________________________________________________= _________ --Apple-Mail=_4DED376E-C8ED-4BBD-949D-1F8D8CEE498E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Thanks to Taco, the solution was to simply use:

function doubleQuotableEscapedConTeXtString( = str)
  local rep =3D {
      [1] =3D  { '{', = '{\\textbraceleft}' },
      [2] =3D  { '}', = '{\\textbraceright}' },
      [3] =3D  { '#', '{\\texthash}' = },
    =   [4] =3D  { '$', '{\\textdollar}' },
      [5] =3D  { '&', = '{\\textampersand}' },
      [6] =3D  { '%', '{\\textpercent}' = },
    =   [7] =3D  { '\\','{\\textbackslash}' = },
    =   [8] =3D  { '|', '{\\textbar}' },
      [9] =3D  { '_', = '{\\textunderscore}' },
      [10] =3D { '~', '{\\textasciitilde}' = },
    =   [11] =3D { '^', '{\\textasciicircum}' },
      [12] =3D { '"', = "\"&ditto&\"" },
  }
  return = lpeg.replacer(rep):match(str)
end

And the string becomes something that = can safely be given toi METAPOST and safely handled by TeX when called = from METAPOST via textext()

G


On 2 Apr = 2020, at 14:05, Gerben Wierda <gerben.wierda@rna.nl> wrote:

Here is a minimum = example of a problem that I have in getting curly braces printed in = METAPOST in code that is generated by lua.

Any help is welcome.

\usemodule[scite]
\setupxml
 [entities=3Dyes]


\startluacode

function warn( ... )
  texio.write_nl("-----> " .. = string.format(...))
end

local function = mpLabelString( xmlLabelString)
  -- Returns a string where each " is replaced by a = METAPOST compatible result, except for outer double = quotes"
  rep =3D = {
    =   [1] =3D { "\"", "\"&ditto&\""   = },
    =   -- DOESN'T WORK: [2] =3D { "\\", "\\\\" = },
  = }
  local = tmpString =3D string.formatters( "%!tex!", = xmlLabelString)
  warn( = "STRING.FORMAT XML \"%s\"", xmlLabelString)
  warn( "STRING.FORMAT TeX-ed \"%s\"", = tmpString)
  warn( = "STRING.FORMAT Replaced \"%s\"", = lpeg.replacer(rep):match(tmpString))
  return = lpeg.replacer(rep):match(tmpString)
end

function warnAndConTeXt( ...)
  warn( ...)
  context( ...)
end

function moduledata.test( filename)
  local labelString
  context( "The string to typeset = is:\\par\\type-{Label} \"a\" [Text]!-")
  context( "\\par The attempts = are:")
  = context( "\\par1. \\type-Label Text-")
  context( "\\par2. \\type-Label = [Text]!-")
  = context( "\\par3. \\type-Label \"a\" [Text]!-")
  context( "\\par4. \\type-{Label} = [Text]!-")
  = context( "\\par5. \\type-{Label} \"a\" = [Text]!-")
  = context.startMPpage { instance =3D "doublefun" }
  context( "picture = pic;")
  = labelString =3D "1. Label Text OK"
  warnAndConTeXt( "pic :=3D Foo( 0, 0, 150, 50, = \"%s\");", mpLabelString( labelString))
  labelString =3D "2. Label [Text]! = OK"
  = warnAndConTeXt( "pic :=3D Foo( 0, -75, 150, 50, \"%s\");", = mpLabelString( labelString))
  labelString =3D "3. Label \"a\" [Text]! = OK"
  = warnAndConTeXt( "pic :=3D Foo( 0, -150, 150, 50, \"%s\");", = mpLabelString( labelString))
  labelString =3D "4. {Label} [Text]! MISSING curly = braces"
  = warnAndConTeXt( "pic :=3D Foo( 0, -225, 150, 50, \"%s\");", = mpLabelString( labelString))
  labelString =3D "5. {Label} \"a\" [Text]! MISSING = curly braces"
  = warnAndConTeXt( "pic :=3D Foo( 0, -300, 150, 50, \"%s\");", = mpLabelString( labelString))
  context( "drawdot (0,0) withpen pencircle scaled 4 = withcolor red;")
  = context.stopMPpage()
end
\stopluacode

\usemodule[article-basic]
%\enabletrackers[metapost.tracingall,metapost.lua,metapost.runs= ,metapost.textexts,metapost.scrintersectionPoints,metapost.runs,metapost.g= raphics,metapost.terminal]

\starttext

\definefontfamily[mainface][rm][Optima]
\setupbodyfont[mainface,10pt]

\startMPinclusions[+]{doublefun}

\stopMPinclusions

\startMPdefinitions{doublefun}
vardef makeTeXLabel( expr w, h, name) = =3D
  show = "NAME makeTeXLabel:", name;
  save p; picture p ;
  save s; string s;
  s :=3D "\framed{" & name & "}"; % = Curly braces will be missing. I need this to = work.
  % s :=3D = "\type-" & name & "-"; % Curly braces are displayed, but this = must become a vbox in the end, so can't use it
  show "SCAN:", s;
  p :=3D textext( s);
  p
enddef;

vardef Foo( expr xpos, ypos, width, height, str) = =3D
  show = "NAME Foo:", str; % Backslashes are already gone = here
  save = pic; picture pic;
  pic :=3D makeTeXLabel( width, height, str) shifted = (xpos, ypos);
  draw = pic;
  = pic
enddef;

\stopMPdefinitions

\ctxlua{moduledata.test("My ArchiMate Model Export = BES.xml")}
\typefile[option=3DTEX]{test11.tex}

\stoptext
The question is: how can I get this to = work? The strings that have to be printed inside the METAPOST picture = come from an XML and can contain about anything. But in the end that = string will have to be vertically typeset as a paragraph, hence I cannot = use \type (which works).

<test11.tex><= /span>

_____________________________________________= ______________________________________
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://context.aanhet.net
archive  : = https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
_______________________________________________________________= ____________________

= --Apple-Mail=_4DED376E-C8ED-4BBD-949D-1F8D8CEE498E-- --===============7004925488332632084== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --===============7004925488332632084==--