[Question also asked on the ConTeXt mailing list but no answer yet so it's probably complicated enough to get an answer, hence repeated here]

I would like to use the result of transparent() straight in my macros, so I can pass the colors with transparency using a single parameter. That should be possible if I understand the MetaFun manual (where I’ve copy-pasted this example from):


vardef SampleText (expr t, c) =
  save p ; picture p ;
  p := image (draw t infont "\truefontname{Regular}") ;
  draw (p shifted (- xpart center p,0)) scaled 5 withcolor c;
enddef ;
SampleText ("Much Of This"   , transparent(1, .5, red  )) ;
SampleText ("Functionality"  , transparent(1, .5, green)) ;
SampleText ("Was Written"    , transparent(1, .5, blue )) ;
SampleText ("While Listening", transparent(1, .5, cmyk(1,0,0,0))) ;
SampleText ("To the CD's Of" , transparent(1, .5, cmyk(0,1,0,0))) ;
SampleText ("Tori Amos"      , transparent(1, .5, cmyk(0,0,1,0))) ;


But it results in:

system          > files > jobname 'testcolor', input './testcolor', result 'testcolor'
fonts           > latin modern fonts are not preloaded
languages       > language 'en' is active
open source     > level 2, order 3, name './testcolor.tex'
fonts           > preloading latin modern fonts (second stage)
fonts           > 'fallback modern-designsize rm 12pt' is loaded
metapost        > initializing instance 'doublefun:1' using format 'metafun' and method 'double'
metapost        > loading 'metafun' as '/usr/local/context-osx-64/tex/texmf-context/metapost/context/base/mpiv/metafun.mpxl' using method 'double'
metapost        > initializing number mode 'double'
metapost log    > 
metapost log    > loading metafun, including plain.mp version 1.004 for metafun iv and xl
metapost log    > 
metapost log    > ! Missing ')' has been inserted.
metapost log    > <to be read again> 
metapost log    >                    withprescript
metapost log    > transparent->(1)withprescript
metapost log    >                              "tr_alternative="&decimal.transparency_alternat...
metapost log    > <*> ... ("Much Of This" , transparent(1, .5, red )
metapost log    >                                                   ) ; SampleText ("Functiona...
metapost log    > 
backend         > xmp > using file '/usr/local/context-osx-64/tex/texmf-context/tex/context/base/mkiv/lpdf-pdx.xml'
pages           > flushing realpage 1, userpage 1, subpage 1
close source    > level 2, order 3, name './testcolor.tex'
close source    > level 1, order 3, name 'cont-yes.mkiv'

For me, it is not about passing colour on to ConTeXt to typeset text, these are colors that normally would be drawn with withcolor ca withtransparency (method,factor) but that requires the passing of many more parameters and a lot more work in coding.

Does transparent() maybe produce a string that looks like ca withtransparency (method,factor) so something only usable in draw/fill/etc statements? But in that case how does that work without scantokens to parse the string?

Is there a way I can do this without having twice the parameters (and I have to pass multiple colours) in my macros everywhere?



1 Answer 1


You can pass the transparent() argument as a text type. In that case the rest of the line until ; is copied into the argument and it can be used directly in a draw command. I'm not really sure if I completely understand your requirements but this seems to come close to the original example.


vardef SampleText (expr t) text c =
    save p ; picture p ;
    p := image (draw t infont "\truefontname{Regular}") ;
    draw (p shifted (- xpart center p,0)) scaled 5 withcolor c ;
enddef ;

SampleText ("Much Of This")    transparent(1, .5, red  ) ;
SampleText ("Functionality")   transparent(1, .5, green) ;
SampleText ("Was Written")     transparent(1, .5, blue ) ;
SampleText ("While Listening") transparent(1, .5, cmyk(1,0,0,0)) ;
SampleText ("To the CD's Of")  transparent(1, .5, cmyk(0,1,0,0)) ;
SampleText ("Tori Amos")       transparent(1, .5, cmyk(0,0,1,0)) ;



enter image description here

  • So what if I have to pass one multiple colors (with transparency)? If it copies everything until the end, does that work? E.g. SampleText( expr e)(text colorone)(text colortwo) = and a call that says SampleText( "Foo", transparent(1, 0.5,red), transparent( 1, 0.8, green))
    – gctwnl
    Apr 9, 2020 at 8:28
  • Answering myself: this is not an option. So, I must find another way or resign myself with code with many more parameters/arguments.
    – gctwnl
    Apr 9, 2020 at 9:38
  • I didn't find a way yet to pass two text arguments. It should be possible to pass two things as a single argument and then parse that into the individual elements, however I'm not sure how. The whole thing is rather convoluted :)
    – Marijn
    Apr 9, 2020 at 10:02
  • Yes, it is not the right way to go. One could pass arguments as strings and then use scantokens() but that is more trouble than it's worth. It is just a bit counterintuitive if you are used to functions that the linguistic setup is inline expanding macros. It's a different paradigm that also works well but has some complexities given how parsers work. Anyway, I moved to passing primitive arguments.
    – gctwnl
    Apr 10, 2020 at 12:26
  • 1
    @Marijn You are wasting your time here. When this question was asked it was crossposted without notification to the ConTeXt mailing list, where it has since been answered in detail. mailman.ntg.nl/pipermail/ntg-context/2020/097528.html Apr 15, 2020 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.