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

Add alignment parameter to matrices and vectors #454

Open
Enivex opened this issue Mar 30, 2023 · 13 comments
Open

Add alignment parameter to matrices and vectors #454

Enivex opened this issue Mar 30, 2023 · 13 comments
Labels
feature request New feature or request good contribution Good, isolated issue for someone that wants to contribute math Related to math syntax, layout, etc.

Comments

@Enivex
Copy link
Collaborator

Enivex commented Mar 30, 2023

If I write the matrix

$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1) $

this is displayed as

image

To "correctly" display this, I currently have to add & to every single cell in the matrix, when all I really want is for cells to be aligned to be flush right

$ mat(-1&, 1&, 1&; 1&, -1&, 1&; 1&, 1&, -1&) $

image

While allowing arbitrary alignment is nice, it would be nicer in this case if I could write something like

$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: right) $

to achieve the same effect. (Also for vectors)

In LaTeX I can do this with

\[ \begin{pmatrix*}[r] -1 & 1 & 1\\ 1 & -1 & 1\\ 1 & 1 & -1 \end{pmatrix*} \]

$$ \begin{pmatrix*}[r] -1 & 1 & 1\\ 1 & -1 & 1\\ 1 & 1 & -1 \end{pmatrix*} $$

@user202729
Copy link
Contributor

user202729 commented Mar 30, 2023

Proof of concept:

#let matright(..aa) = math.mat(..aa.pos().map(row => row.map(y => {y; [$&$]})))

$matright(1, 22, 3; 4, 5, 6)$

Does what you expect.

(side note, it may be better to make the repr of arguments type different from
that of array type, otherwise it's a bit confusing)

Although manually inserting & at arbitrary place definitely feel like a hack.

@SUPERCILEX
Copy link
Contributor

I think this is also solved by tabular alignment because you can just align the first row and the others will follow.

@SUPERCILEX
Copy link
Contributor

We discovered that's not true for left alignment.


I won't have the time to implement this, but here are some thoughts:

  • With the parameter as a single element, we can support left, right, up, down alignment and their combinations like left top. This can be implemented by setting the starting alignment of into_aligned_frame and changing the if points.is_empty() to something that handles plain left/right alignment.
  • If we allow a list, it gets more complicated. We could support a flat list for matrices, but it's not clear to me why that should mean column alignment instead of rows. So it seems like matrices need an mxn list.
  • The list arg could support strides: if the matrix is 5 wide and you pass in two elements for the columns (say left, right), then that pattern will be repeated for the 3-4, and 5 columns.

@Enivex
Copy link
Collaborator Author

Enivex commented Apr 24, 2023

I think you're making things more complicated than they need to be. There is no real reason to touch vertical alignment, and the vast majority of use cases would be covered by simply allowing the user to set left, center (default) or right alignment for the entire matrix.

@SUPERCILEX
Copy link
Contributor

Hence "If we allow a list" :)

@Enivex
Copy link
Collaborator Author

Enivex commented Apr 24, 2023

Hence "If we allow a list" :)

If you allowed a list it would specify alignment for each column. Though I can't imagine spacing would look very good in most cases.

My vote would be for removing & from matrices entirely, allowing the user to specify left/center/right alignment, and rather leave more arbitrary alignment to some future package. Though obviously @laurmaedje may feel otherwise.

@gabrielalexandrelopes
Copy link

I tried to use the same trick to match the rows of matrices using:

#let matright(..aa) = math.mat(..aa.pos().map(row => row.map(y => {y; v(8mm)})))

to go from this:
Screenshot 2024-06-21 at 17 35 04
to this:
Screenshot 2024-06-21 at 17 35 31

but it is still not perfect. I also tried the natrix package but that does not work for this example. Do you have other suggestions?

@laurmaedje laurmaedje added the good contribution Good, isolated issue for someone that wants to contribute label Jul 16, 2024
@GuyKawaii
Copy link

GuyKawaii commented Sep 10, 2024

are there anyone on this? seems like a really nice feature to have. I just gave typst a go and was surprised by the default alignment being center for each element of a matrix with no simple way of selecting right or left for the whole matrix instead of manual &'s

image

@Enivex
Copy link
Collaborator Author

Enivex commented Sep 10, 2024

I doubt anyone's working on it unless they've mentioned it here. The actual implementation wouldn't be that hard I would think, but there's some design decisions to be discussed and made.

@GuyKawaii
Copy link

GuyKawaii commented Sep 10, 2024

Thanks a lot @Enivex for the quick response and appreciate the work you guys do on Typst and makes sense😄. I'll do it manual for now and see if it takes to much effort as i have to write a lot of matrices and if its better to try latex at present for some docs types

@Enivex
Copy link
Collaborator Author

Enivex commented Sep 10, 2024

you guys

To be clear, I'm not affiliated with typst

@laurmaedje
Copy link
Member

To be clear, I'm not affiliated with typst

Affiliated or not, you're doing a lot for the project and we're very thankful! :)

@mkorje
Copy link
Contributor

mkorje commented Sep 11, 2024

@GuyKawaii I plan on working on the matrix issues/feature requests after I finish with another PR I'm doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request good contribution Good, isolated issue for someone that wants to contribute math Related to math syntax, layout, etc.
Projects
None yet
Development

No branches or pull requests

7 participants