ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Nicola <nvitacolonna@gmail.com>
To: ntg-context@ntg.nl
Subject: Re: metapost question
Date: Fri, 21 Oct 2011 20:02:52 +0200	[thread overview]
Message-ID: <nvitacolonna-7792E9.20025121102011@news.gmane.org> (raw)
In-Reply-To: <0E2E620E-1148-41AD-9966-54A594FCCE94@xs4all.nl>

In article <0E2E620E-1148-41AD-9966-54A594FCCE94@xs4all.nl>,
 Hans van der Meer <havdmeer@xs4all.nl> wrote:

> I cannot find the answer to the following question in Knuth's Metafont book.
> The following definition with a trailing text argument:
> 
> def mydef (expr a, b, c) text modifier =
> 	if <modifier = empty>: 
> 	else:
> 	fi
> enddef;
> 
> How can I do the switch on an empty or nonempty modifier argument? It is not 
> a string because calling its length fails with an error.

A text argument is a (more or less) arbitrary sequence of tokens—that is, 
loosely speaking, any “chunk” of MetaPost code.

> I must do the switch, because "fill modifier" with an empty "modifier" gives 
> horrible results.
> 
> Usage would be calls like:
>   mydef(1,2,3) withcolor green; % with modifier text
>   mydef(1,2,3); % without modifier text

This is the best I could think of:

vardef emptytext?@# text t =
   (str @# = "empty.sandwich")
enddef;

def mydef(expr a,b,c) text t =
   if begingroup emptytext? empty t sandwich endgroup:
      % t is empty
      draw unitsquare scaled 1cm;
   else:
      % t is not empty
      fill unitsquare scaled 1cm t ;
   fi;
enddef;

The key is the 'emptytext? empty t sandwich;' line: if t is empty, @# matches 
'empty.sandwich', otherwise it matches 'empty' possibly followed by something 
else (depending on what t actually is). The undelimited text parameter of 
emptytext? is needed to “swallow” whatever tokens are left (possibly none) after 
matching the implicit suffix parameter. Note that begingroup and endgroup are 
necessary in the if clause, otherwise emptytext?'s undelimited text parameter 
will match beyond the colon.

In practice, you should rename 'sandwich' to some token that you are pretty sure 
will never occur as the beginning of the text argument when you invoke mydef.

I'd be glad to hear about other solutions: this question has bugged me for some 
time…

Regards,
Nicola

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

  parent reply	other threads:[~2011-10-21 18:02 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-21 13:05 Hans van der Meer
2011-10-21 15:13 ` Peter Rolf
2011-10-21 15:34   ` Meer, H. van der
2011-10-21 18:13   ` Meer, H. van der
2011-10-21 18:02 ` Nicola [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-12-06 20:54 MetaPost Question Context NTG
2015-12-06 21:49 ` Alan BRASLAU
2015-12-07 15:58 ` Context NTG
2011-12-01 22:25 metapost question Hans van der Meer
2011-12-01 23:30 ` Peter Rolf
2009-05-11 15:14 Metapost question Jean Magnan de Bornier
2009-05-11 15:34 ` Peter Rolf
2009-05-11 16:27   ` Jean Magnan de Bornier
2009-05-11 15:42 ` Mojca Miklavec
2009-05-11 15:58   ` Mojca Miklavec
2009-05-11 16:31     ` Jean Magnan de Bornier
2009-05-11 16:52       ` Mojca Miklavec
2009-05-11 17:33         ` Jean Magnan de Bornier
2009-05-11 18:02           ` Mojca Miklavec
2009-05-12  4:33             ` Jean Magnan de Bornier
2009-05-12  7:42               ` Taco Hoekwater
2009-05-12 14:15                 ` Jean Magnan de Bornier
2009-05-12 14:32                   ` Aditya Mahajan
2009-05-12 15:11                     ` Jean Magnan de Bornier
2009-05-12 15:53                       ` Taco Hoekwater
2009-05-12 16:37                         ` Jean Magnan de Bornier
2009-05-13  6:45                           ` Taco Hoekwater
2009-05-13  8:19                             ` Jean Magnan de Bornier
2009-05-11 18:16           ` Jean Magnan de Bornier
2009-05-11 17:01   ` Hans Hagen
2009-05-11 18:08     ` Mojca Miklavec
2009-05-11 15:47 ` Aditya Mahajan
2008-06-18 11:14 metapost question Thomas A. Schmitz
2008-06-18 11:43 ` Wolfgang Schuster
2008-06-18 11:58 ` Peter Rolf
2008-06-18 12:11   ` Thomas A. Schmitz
2007-10-08 13:47 Thomas A. Schmitz
2007-10-08 16:49 ` Otared Kavian
2007-10-08 17:34   ` Thomas A. Schmitz
2007-10-08 18:10   ` Jesse Alama
2007-10-23 12:17     ` Thomas A. Schmitz
2007-10-23 12:26       ` Hans Hagen
2007-10-23 20:58         ` Thomas A. Schmitz
2007-06-28 17:27 MetaPost question Tobias Burnus
2007-06-28 17:48 ` Taco Hoekwater
2007-05-02 20:38 Tobias Burnus
2007-05-02 20:46 ` Aditya Mahajan
2007-05-04 21:18   ` Aditya Mahajan
2005-06-14  6:54 Metapost question Tobias Burnus
2005-06-14  7:42 ` Taco Hoekwater
2005-06-14 10:51   ` h h extern
2005-06-14  7:56 ` Albrecht Kauffmann
2001-11-17 19:54 MetaPost question Jose Luis Diaz
2001-11-18  6:18 ` Johannes H?sing
2001-11-18 11:57 ` Denis B. Roegel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=nvitacolonna-7792E9.20025121102011@news.gmane.org \
    --to=nvitacolonna@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).