Add alignment parameter to matrices and vectors (#4998)

This commit is contained in:
Max 2024-09-26 14:30:47 +00:00 committed by GitHub
parent cf35c2f44c
commit a40e068590
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 61 additions and 7 deletions

View File

@ -7,7 +7,8 @@ use crate::foundations::{
Smart, StyleChain, Value,
};
use crate::layout::{
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Length, Point, Ratio, Rel, Size,
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, HAlignment, Length, Point, Ratio,
Rel, Size,
};
use crate::math::{
alignments, scaled_font_size, stack, style_for_denominator, AlignmentResult,
@ -29,7 +30,8 @@ const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05);
/// A column vector.
///
/// Content in the vector's elements can be aligned with the `&` symbol.
/// Content in the vector's elements can be aligned with the
/// [`align`]($math.vec.align) parameter, or the `&` symbol.
///
/// # Example
/// ```example
@ -47,6 +49,16 @@ pub struct VecElem {
#[default(DelimiterPair::PAREN)]
pub delim: DelimiterPair,
/// The horizontal alignment that each element should have.
///
/// ```example
/// #set math.vec(align: right)
/// $ vec(-1, 1, -1) $
/// ```
#[resolve]
#[default(HAlignment::Center)]
pub align: HAlignment,
/// The gap between elements.
///
/// ```example
@ -70,7 +82,7 @@ impl LayoutMath for Packed<VecElem> {
ctx,
styles,
self.children(),
FixedAlignment::Center,
self.align(styles),
self.gap(styles),
LeftRightAlternator::Right,
)?;
@ -87,7 +99,9 @@ impl LayoutMath for Packed<VecElem> {
/// special syntax of math function calls to define custom functions that take
/// 2D data.
///
/// Content in cells that are in the same row can be aligned with the `&` symbol.
/// Content in cells can be aligned with the [`align`]($math.mat.align)
/// parameter, or content in cells that are in the same row can be aligned with
/// the `&` symbol.
///
/// # Example
/// ```example
@ -109,6 +123,16 @@ pub struct MatElem {
#[default(DelimiterPair::PAREN)]
pub delim: DelimiterPair,
/// The horizontal alignment that each cell should have.
///
/// ```example
/// #set math.mat(align: right)
/// $ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1) $
/// ```
#[resolve]
#[default(HAlignment::Center)]
pub align: HAlignment,
/// Draws augmentation lines in a matrix.
///
/// - `{none}`: No lines are drawn.
@ -249,6 +273,7 @@ impl LayoutMath for Packed<MatElem> {
ctx,
styles,
rows,
self.align(styles),
augment,
Axes::new(self.column_gap(styles), self.row_gap(styles)),
self.span(),
@ -454,6 +479,7 @@ fn layout_mat_body(
ctx: &mut MathContext,
styles: StyleChain,
rows: &[Vec<Content>],
align: FixedAlignment,
augment: Option<Augment<Abs>>,
gap: Axes<Rel<Abs>>,
span: Span,
@ -538,7 +564,11 @@ fn layout_mat_body(
for (cell, &(ascent, descent)) in col.into_iter().zip(&heights) {
let cell = cell.into_line_frame(&points, LeftRightAlternator::Right);
let pos = Point::new(
if points.is_empty() { x + (rcol - cell.width()) / 2.0 } else { x },
if points.is_empty() {
x + align.position(rcol - cell.width())
} else {
x
},
y + ascent - cell.ascent(),
);

View File

@ -476,7 +476,11 @@ pub(super) fn stack(
let mut y = Abs::zero();
for (i, row) in rows.into_iter().enumerate() {
let x = align.position(width - row.width());
let x = if points.is_empty() {
align.position(width - row.width())
} else {
Abs::zero()
};
let ascent_padded_part = minimum_ascent_descent
.map_or(Abs::zero(), |(a, _)| (a - row.ascent()))
.max(Abs::zero());

View File

Before

Width:  |  Height:  |  Size: 1009 B

After

Width:  |  Height:  |  Size: 1009 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -92,7 +92,12 @@ $ mat(1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 1) $
// Error: 3-37 cannot draw a vertical line after column 3 of a matrix with 3 columns
$ mat(1, 0, 0; 0, 1, 1; augment: #3) $,
--- math-mat-align-explicit--alternating ---
--- math-mat-align ---
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #left) $
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #center) $
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #right) $
--- math-mat-align-explicit-alternating ---
// Test alternating explicit alignment in a matrix.
$ mat(
"a" & "a a a" & "a a";
@ -124,6 +129,17 @@ $ mat(
"a a a"&, "a"&, "a a a"&;
) $
--- math-mat-align-explicit-mixed ---
// Test explicit alignment in some columns with align parameter in a matrix.
#let data = (
($&18&&.02$, $1$, $+1$),
($-&9&&.3$, $-1$, $-&21$),
($&&&.011$, $1$, $&0$)
)
$ #math.mat(align: left, ..data) $
$ #math.mat(align: center, ..data) $
$ #math.mat(align: right, ..data) $
--- math-mat-align-complex ---
// Test #460 equations.
#let stop = {

View File

@ -4,6 +4,10 @@
#set math.vec(gap: 1em)
$ vec(1, 2) $
--- math-vec-align ---
$ vec(-1, 1, -1, align: #left)
vec(-1, 1, -1, align: #center)
vec(-1, 1, -1, align: #right) $
--- math-vec-align-explicit-alternating ---
// Test alternating alignment in a vector.