miles-1
December 1, 2024, 11:53pm
1
How can you use arguments with hyphens in math mode? E.g.,
$mat(1, 2, 3; 4, 5, 6; delim:"[")$ // works
$mat(1, 2, 3; 4, 5, 6; column-gap: #2pt)$ // does not work
The issue here is of course the hyphen being interpreted as a minus sign. I realize that you can set arguments like column-gap
beforehand, but this is somewhat tedious if you want to change the value of this parameter for just a single use of mat
amongst many.
For variables with hyphens or underscores, you can use #
in math mode:
#let this-variable = 5
$ (#this-variable - a) / b $
…but this approach does not work for arguments:
$mat(1, 2, 3; 4, 5, 6; #column-gap: #2pt)$ // does not work
2 Likes
sCarton
December 4, 2024, 12:54pm
2
I used the hashtag to invoke the function explicitly:
$#math.mat(column-gap: 2pt, (1, 2, 3), (4, 5, 6))$
This workaround does lose some flexibility afforded to the maths mode.
1 Like
Your answer seems to be the closest thing to a solution, so I will mark it as such, though I’m bummed there’s no way to use it directly without needing to use #
.
Indeed. Given the following is an example from the official documentation, what you’ve seen seems like a bug.
$ vec(1, 2, delim: "[") $
1 Like
ian
December 7, 2024, 8:38pm
5
This is actually in a PR right now by mkorje!
typst:main
← mkorje:better-math-argument-parsing
opened 05:07PM - 23 Sep 24 UTC
This PR brings how math function call arguments are parsed more in line with how… they are parsed in code mode.
New things:
- Closes #2052: Named arguments in math function calls can now be any identifier. This means it can contain hyphens and start with an underscore (as long as it isn't just an underscore), just like in code mode. For example, the below now works.
```typ
#let func(my-body: none) = my-body
$ func(my-body: a) $
```
- Closes #938: Argument spreading now works in math function calls. Prefixing two dots `..` to a variable does the same as in code mode. You can escape the dots or add a space after them to get `..` verbatim. See the example below.
<table>
<tr>
<td> Code </td> <td> Image </td>
</tr>
<tr>
<td>
```typ
#set page(width: 5cm, height: auto)
$
// Spreading the argument, so works.
mat(..#range(1, 5).chunks(2))
// Passed verbatim, so reprs the list.
mat(#(..range(2).map(_ => range(2))))
$
#let nums = ((1,) * 5).intersperse(0).chunks(3)
$ mat(..nums, delim: "[") $
```
</td>
<td>
![math](https://github.com/user-attachments/assets/07362233-da6a-4aa8-a971-b21e892161b6)
</td>
</tr>
</table>
- Duplicate named arguments error: The below now gives an error, instead of silently overriding the previous instance of the named argument given.
```typ
#let func(my: none) = my
// Error: 15-17 duplicate argument: my
$ func(my: a, my: b) $
```
These changes don't conflict with #4364 majorly. Some things, like the new lex modes, might end up a bit cleaner with the changes from #4364 I think. Also, these changes are somewhat isolated; only a little (ish) has spread outside of math argument parsing to the rest of the math parsing code. Nothing else has changed with regards to math parsing (at least I hope so, all of the tests have passed which is promising). Only one test was updated to test `column-gap` and `row-gap` being used directly in matrices.
1 Like