Calling function with Scribble text as argument(s)

41 views
Skip to first unread message

Hendrik Boom

unread,
Jul 30, 2019, 1:02:02 PM7/30/19
to Racket Users
I've found this as an example for getting coloured text:

#lang scribble/base

@(require scribble/core)

@(define (colorize #:color c . content)
(elem #:style (style #f (list (color-property c)))
content))

@colorize[#:color "red"]{WARNING}


But what if you want to call colorize from Racket code.
For example, to make a function has the colour red built in,
I tried

@(define (redtext text) (colorize #:color "red" text))

which did not work.

Presumably the (summary text) has to accept text as a bunch of
arguments, not just one.
and the last argument to colorize needs to be
understood accordingly.

By analogy to the definition of colorize I probably have to start with
somethng like
@(define (summary . text) ....
but then how to pass text to colorize as a bunch of arguments, not
just one?


-- hendrik


Joel Dueck

unread,
Jul 30, 2019, 2:01:21 PM7/30/19
to Racket Users
On Tuesday, July 30, 2019 at 12:02:02 PM UTC-5, Hendrik Boom wrote:
For example, to make a function has the colour red built in,
I tried

    @(define (redtext text) (colorize #:color "red" text))

which did not work.

When you say "which did not work" — what didn't work exactly? If I concatenate this `@define` with your top example and add

    @redtext{WARNING AGAIN}

…I get the expected output in DrRacket, no errors.

Jens Axel Søgaard

unread,
Jul 30, 2019, 2:08:53 PM7/30/19
to Racket Users
Den tir. 30. jul. 2019 kl. 19.02 skrev Hendrik Boom <hen...@topoi.pooq.com>:
I've found this as an example for getting coloured text:

    #lang scribble/base

    @(require scribble/core)

    @(define (colorize #:color c . content)
        (elem #:style (style #f (list (color-property c)))
              content))

    @colorize[#:color "red"]{WARNING}


But what if you want to call colorize from Racket code.
For example, to make a function has the colour red built in,
I tried

    @(define (redtext text) (colorize #:color "red" text))

which did not work.

You are right that the contents isn't a single string, so change it to:

@(define (redtext . contents)
   (colorize #:color "red" contents))

@redtext{foo}
 

Hendrik Boom

unread,
Jul 30, 2019, 2:55:46 PM7/30/19
to Racket Users
This works.

It works if the call to redtext is in the same file as the definition.

Now for the next problem. If I @include-section, an occurrence of
redtext in the included section is recognised as an unbound identifier.
Evidently I need to say something to get included sections to inherit
bindings fro the main file.

-- hendrik

Ben Greenman

unread,
Jul 30, 2019, 4:04:42 PM7/30/19
to Racket Users
> Now for the next problem. If I @include-section, an occurrence of
> redtext in the included section is recognised as an unbound identifier.
> Evidently I need to say something to get included sections to inherit
> bindings fro the main file.

`include-section` is much closer to Racket's `require` than TeX's `include`

Hendrik Boom

unread,
Jul 30, 2019, 10:40:37 PM7/30/19
to Racket Users
I see.

So the included section has to do its own definition of redtext, or require
it from a common source.

-- hendrik

Hendrik Boom

unread,
Jul 31, 2019, 10:36:47 AM7/31/19
to Racket Users
IT WORKS NOW.

At the beginning of every inclided file (and there are more than fifty
of them) I have to place the lines

#lang scribble/base
@(require "pfx.scrbl")

where pfx.scrbl contains the definitions of my new @ commands.

I'm in the process of converting a huge multifile manuscript from
another adhoc and incomplete document compiler that also uses a
(partly incompatible) version of @-markup. Now that the performance
aspects of scribble have been dealt with, it's feasible. I'm looking
forward to using it.


-- hendrik

Benjamin Lerner

unread,
Jul 31, 2019, 11:31:54 AM7/31/19
to Racket Users

In cases like these, I’d just define a helper file my-commands.rkt, and @(require "my-commands.rkt") Those helper commands can use standard Racket syntax, since they’re likely to mostly be standard-looking Racket functions, and they can (require scribble/whatever) libraries if they need to call some of the scribble markup functions. You can also use standard provide forms in my-commands.rkt to provide just your helper functions.

Ben Greenman

unread,
Jul 31, 2019, 11:46:54 AM7/31/19
to Benjamin Lerner, Racket Users
>> At the beginning of every inclided file (and there are more than fifty
>> of them) I have to place the lines
>>
>> #lang scribble/base
>> @(require "pfx.scrbl")
>>
>> where pfx.scrbl contains the definitions of my new @ commands.

You could replace those lines with a custom #lang:

http://prl.ccs.neu.edu/blog/2019/02/17/writing-a-paper-with-scribble/

(Scroll down to "Creating a #lang for a paper")
Reply all
Reply to author
Forward
0 new messages