Allow non-string contents for math operators (#2663)

This commit is contained in:
frozolotl 2023-11-13 12:26:58 +01:00 committed by GitHub
parent 8219ec2595
commit 67422e664b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 9 deletions

View File

@ -21,7 +21,7 @@ use super::*;
pub struct OpElem { pub struct OpElem {
/// The operator's text. /// The operator's text.
#[required] #[required]
pub text: EcoString, pub text: Content,
/// Whether the operator should show attachments as limits in display mode. /// Whether the operator should show attachments as limits in display mode.
#[default(false)] #[default(false)]
@ -31,8 +31,7 @@ pub struct OpElem {
impl LayoutMath for OpElem { impl LayoutMath for OpElem {
#[tracing::instrument(skip(ctx))] #[tracing::instrument(skip(ctx))]
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
let fragment = let fragment = ctx.layout_fragment(self.text())?;
ctx.layout_text(&TextElem::new(self.text().clone()).spanned(self.span()))?;
ctx.push( ctx.push(
FrameFragment::new(ctx, fragment.into_frame()) FrameFragment::new(ctx, fragment.into_frame())
.with_class(MathClass::Large) .with_class(MathClass::Large)
@ -49,12 +48,15 @@ impl LayoutMath for OpElem {
macro_rules! ops { macro_rules! ops {
($($name:ident $(: $value:literal)? $(($tts:tt))?),* $(,)?) => { ($($name:ident $(: $value:literal)? $(($tts:tt))?),* $(,)?) => {
pub(super) fn define(math: &mut Scope) { pub(super) fn define(math: &mut Scope) {
$(math.define( $({
stringify!($name), let operator = EcoString::from(ops!(@name $name $(: $value)?));
OpElem::new(ops!(@name $name $(: $value)?).into()) math.define(
.with_limits(ops!(@limit $($tts)*)) stringify!($name),
.pack() OpElem::new(TextElem::new(operator).into())
);)* .with_limits(ops!(@limit $($tts)*))
.pack()
);
})*
let dif = |d| { let dif = |d| {
HElem::new(THIN.into()).with_weak(true).pack() HElem::new(THIN.into()).with_weak(true).pack()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

@ -24,3 +24,7 @@ $ op("myop", limits: #false)_(x:=1) x \
--- ---
// Test styled operator. // Test styled operator.
$ bold(op("bold", limits: #true))_x y $ $ bold(op("bold", limits: #true))_x y $
---
// With non-text content
$ op(#underline[ul]) a $