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