ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Minimum example of problem getting curly braces printed in METAPOST
@ 2020-04-02 12:05 Gerben Wierda
  2020-04-03 15:36 ` Solved: " Gerben Wierda
  0 siblings, 1 reply; 3+ messages in thread
From: Gerben Wierda @ 2020-04-02 12:05 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 3642 bytes --]

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=yes]


\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 = {
      [1] = { "\"", "\"&ditto&\""   },
      -- DOESN'T WORK: [2] = { "\\", "\\\\" },
  }
  local tmpString = 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 = "doublefun" }
  context( "picture pic;")
  labelString = "1. Label Text OK"
  warnAndConTeXt( "pic := Foo( 0, 0, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "2. Label [Text]! OK"
  warnAndConTeXt( "pic := Foo( 0, -75, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "3. Label \"a\" [Text]! OK"
  warnAndConTeXt( "pic := Foo( 0, -150, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "4. {Label} [Text]! MISSING curly braces"
  warnAndConTeXt( "pic := Foo( 0, -225, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "5. {Label} \"a\" [Text]! MISSING curly braces"
  warnAndConTeXt( "pic := 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.graphics,metapost.terminal]

\starttext

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

\startMPinclusions[+]{doublefun}

\stopMPinclusions

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

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

\stopMPdefinitions

\ctxlua{moduledata.test("My ArchiMate Model Export BES.xml")}
\typefile[option=TEX]{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).




[-- Attachment #1.2.1: Type: text/html, Size: 14325 bytes --]

[-- Attachment #1.2.2: test11.tex --]
[-- Type: application/octet-stream, Size: 3108 bytes --]

\usemodule[scite]
\setupxml
 [entities=yes]


\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 = {
      [1] = { "\"", "\"&ditto&\""   },
      -- DOESN'T WORK: [2] = { "\\", "\\\\" },
  }
  local tmpString = 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 = "doublefun" }
  context( "picture pic;")
  labelString = "1. Label Text OK"
  warnAndConTeXt( "pic := Foo( 0, 0, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "2. Label [Text]! OK"
  warnAndConTeXt( "pic := Foo( 0, -75, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "3. Label \"a\" [Text]! OK"
  warnAndConTeXt( "pic := Foo( 0, -150, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "4. {Label} [Text]! MISSING curly braces"
  warnAndConTeXt( "pic := Foo( 0, -225, 150, 50, \"%s\");", mpLabelString( labelString))
  labelString = "5. {Label} \"a\" [Text]! MISSING curly braces"
  warnAndConTeXt( "pic := 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.graphics,metapost.terminal]

\starttext

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

\startMPinclusions[+]{doublefun}

\stopMPinclusions

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

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

\stopMPdefinitions

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

\stoptext

[-- Attachment #1.2.3: Type: text/html, Size: 279 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Solved: Minimum example of problem getting curly braces printed in METAPOST
  2020-04-02 12:05 Minimum example of problem getting curly braces printed in METAPOST Gerben Wierda
@ 2020-04-03 15:36 ` Gerben Wierda
  2020-04-05 10:08   ` Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Gerben Wierda @ 2020-04-03 15:36 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 5282 bytes --]

Thanks to Taco, the solution was to simply use:

function doubleQuotableEscapedConTeXtString( str)
  local rep = {
      [1] =  { '{', '{\\textbraceleft}' },
      [2] =  { '}', '{\\textbraceright}' },
      [3] =  { '#', '{\\texthash}' },
      [4] =  { '$', '{\\textdollar}' },
      [5] =  { '&', '{\\textampersand}' },
      [6] =  { '%', '{\\textpercent}' },
      [7] =  { '\\','{\\textbackslash}' },
      [8] =  { '|', '{\\textbar}' },
      [9] =  { '_', '{\\textunderscore}' },
      [10] = { '~', '{\\textasciitilde}' },
      [11] = { '^', '{\\textasciicircum}' },
      [12] = { '"', "\"&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=yes]
> 
> 
> \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 = {
>       [1] = { "\"", "\"&ditto&\""   },
>       -- DOESN'T WORK: [2] = { "\\", "\\\\" },
>   }
>   local tmpString = 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 = "doublefun" }
>   context( "picture pic;")
>   labelString = "1. Label Text OK"
>   warnAndConTeXt( "pic := Foo( 0, 0, 150, 50, \"%s\");", mpLabelString( labelString))
>   labelString = "2. Label [Text]! OK"
>   warnAndConTeXt( "pic := Foo( 0, -75, 150, 50, \"%s\");", mpLabelString( labelString))
>   labelString = "3. Label \"a\" [Text]! OK"
>   warnAndConTeXt( "pic := Foo( 0, -150, 150, 50, \"%s\");", mpLabelString( labelString))
>   labelString = "4. {Label} [Text]! MISSING curly braces"
>   warnAndConTeXt( "pic := Foo( 0, -225, 150, 50, \"%s\");", mpLabelString( labelString))
>   labelString = "5. {Label} \"a\" [Text]! MISSING curly braces"
>   warnAndConTeXt( "pic := 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.graphics,metapost.terminal]
> 
> \starttext
> 
> \definefontfamily[mainface][rm][Optima]
> \setupbodyfont[mainface,10pt]
> 
> \startMPinclusions[+]{doublefun}
> 
> \stopMPinclusions
> 
> \startMPdefinitions{doublefun}
> vardef makeTeXLabel( expr w, h, name) =
>   show "NAME makeTeXLabel:", name;
>   save p; picture p ;
>   save s; string s;
>   s := "\framed{" & name & "}"; % Curly braces will be missing. I need this to work.
>   % s := "\type-" & name & "-"; % Curly braces are displayed, but this must become a vbox in the end, so can't use it
>   show "SCAN:", s;
>   p := textext( s);
>   p
> enddef;
> 
> vardef Foo( expr xpos, ypos, width, height, str) =
>   show "NAME Foo:", str; % Backslashes are already gone here
>   save pic; picture pic;
>   pic := makeTeXLabel( width, height, str) shifted (xpos, ypos);
>   draw pic;
>   pic
> enddef;
> 
> \stopMPdefinitions
> 
> \ctxlua{moduledata.test("My ArchiMate Model Export BES.xml")}
> \typefile[option=TEX]{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>
> 
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 19070 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Solved: Minimum example of problem getting curly braces printed in METAPOST
  2020-04-03 15:36 ` Solved: " Gerben Wierda
@ 2020-04-05 10:08   ` Hans Hagen
  0 siblings, 0 replies; 3+ messages in thread
From: Hans Hagen @ 2020-04-05 10:08 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Gerben Wierda

On 4/3/2020 5:36 PM, Gerben Wierda wrote:
> Thanks to Taco, the solution was to simply use:
> 
> function doubleQuotableEscapedConTeXtString( str)
>    local rep = {
>        [1] =  { '{', '{\\textbraceleft}' },
>        [2] =  { '}', '{\\textbraceright}' },
>        [3] =  { '#', '{\\texthash}' },
>        [4] =  { '$', '{\\textdollar}' },
>        [5] =  { '&', '{\\textampersand}' },
>        [6] =  { '%', '{\\textpercent}' },
>        [7] =  { '\\','{\\textbackslash}' },
>        [8] =  { '|', '{\\textbar}' },
>        [9] =  { '_', '{\\textunderscore}' },
>        [10] = { '~', '{\\textasciitilde}' },
>        [11] = { '^', '{\\textasciicircum}' },
>        [12] = { '"', "\"&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()
more efficient

local rep = lpeg.replacer {
   { '{', '{\\textbraceleft}' },
   { '}', '{\\textbraceright}' },
   { '#', '{\\texthash}' },
   { '$', '{\\textdollar}' },
   { '&', '{\\textampersand}' },
   { '%', '{\\textpercent}' },
   { '\\','{\\textbackslash}' },
   { '|', '{\\textbar}' },
   { '_', '{\\textunderscore}' },
   { '~', '{\\textasciitilde}' },
   { '^', '{\\textasciicircum}' },
   { '"', "\"&ditto&\"" },
}

function doubleQuotableEscapedConTeXtString( str)
     return rep:match(str)
end

this is probably also ok:

local rep = {
     ["\""] = "\\char34 ",
     ["#"]  = "\\char35 ",
     ["$"]  = "\\char36 ",
     ["%"]  = "\\char37 ",
     ["&"]  = "\\char38 ",
     ["\\"] = "\\char92 ",
     ["^"]  = "\\char94 ",
     ["_"]  = "\\char95 ",
     ["{"]  = "\\char123 ",
     ["|"]  = "\\char124 ",
     ["}"]  = "\\char125 ",
     ["~"]  = "\\char126 ",
}

function doubleQuotableEscapedConTeXtString(str)
     return (string.gsub(str,".",rep))
end


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-04-05 10:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-02 12:05 Minimum example of problem getting curly braces printed in METAPOST Gerben Wierda
2020-04-03 15:36 ` Solved: " Gerben Wierda
2020-04-05 10:08   ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).