Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro feature similar to LaTeX & *roff #93

Closed
ollelogdahl opened this issue Mar 21, 2023 · 6 comments
Closed

Macro feature similar to LaTeX & *roff #93

ollelogdahl opened this issue Mar 21, 2023 · 6 comments

Comments

@ollelogdahl
Copy link
Contributor

ollelogdahl commented Mar 21, 2023

One of my favourite features of LaTeX and *roff is extendability. For example, Tikz is quite powerful LaTeX, but the macro's pic (for writing pictures), chem (for writing chemical-compounds), grap (for graphs) for *.roff is really where programmable typesetters shine.

Will there be some plugin/preprocessor support? Or will it all be implemented using functions in the 'standard library'. Would be amazing to be able to write similar preprocessors in Typst. Is this a planned feature?

@CGMossa
Copy link
Sponsor Contributor

CGMossa commented Mar 21, 2023

In many ways, this goes against the design philosophy of typst. Incremental compilation, that enables the expedient compilation we experience in the app, is due to the fact that there ain't any macros.

@laurmaedje
Copy link
Member

I think functions pretty much fill this role.

@ollelogdahl
Copy link
Contributor Author

In many ways, this goes against the design philosophy of typst. Incremental compilation, that enables the expedient compilation we experience in the app, is due to the fact that there ain't any macros.

I see, thank you for responding. I really hope we get graphs and diagrams some day :^)

@owiecc
Copy link
Sponsor Contributor

owiecc commented Mar 24, 2023

See https://gitlab.com/giacomogallina/typst-cd for a commutative diagrams example.

@cnaak
Copy link

cnaak commented Aug 21, 2024

I think functions pretty much fill this role.

Functions suffer from scope limitations, while macro expansions occur in the caller's scope.

I'm somewhat frustrated by not being able to nicely encapsulate set page(...), or set heading(...) inside a function, thus separating code from formatting, as it simply won't work; however, "polluting" an user's source file with show and set rules is the only way (at least I know of, and I've been typst'ing a lot lately) to make those instructions to being into effect.

The following does not work:

#let apply-intended-formattings() = {
  // my code-oriented including `set` and `show` rules,
  // which can be placed in another, package-side source file, but shown here for conciseness...
}

// user source:
#apply-intended-formattings()
// Bummer! the changes likely fell out-of-scope and do not seem to work at all...

But the following, that mixes code with document contents, does:

//user source interspersed with lots of `set` and `show` rule codes.
= Headings
And text concerning the writer, all mixed up.

Another (related) form to make this work is by having the ability to global set, and global show within functions, as discussed on #2153.

@laurmaedje
Copy link
Member

@cnaak What you want to do in this case is use a bare show rule to pass the rest of your document into the function. Rather than pulling out the styling and making it global somehow, you push in the content, making it local. It would look like this:

#let apply-intended-formattings(body) = {
  set document(title: "My Title")
  set page(paper: "a5")
  set text(size: 12pt)
  body
}

#show: apply-intended-formattings
This is affected!

Effectively show: f; The rest is equivalent to #f[The rest].

See also https://typst.app/docs/tutorial/making-a-template/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants