Add default-style parameter to math.equation

This commit is contained in:
fGolke 2025-04-05 21:23:01 +02:00
parent ea336a6ac7
commit 1df33f72b4
5 changed files with 46 additions and 10 deletions

View File

@ -4,7 +4,7 @@ use ecow::EcoString;
use typst_library::diag::SourceResult; use typst_library::diag::SourceResult;
use typst_library::foundations::{Packed, StyleChain, SymbolElem}; use typst_library::foundations::{Packed, StyleChain, SymbolElem};
use typst_library::layout::{Abs, Size}; use typst_library::layout::{Abs, Size};
use typst_library::math::{EquationElem, MathSize, MathVariant}; use typst_library::math::{EquationElem, MathSize, MathStyle, MathVariant};
use typst_library::text::{ use typst_library::text::{
BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric, BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric,
}; };
@ -187,14 +187,21 @@ fn styled_char(styles: StyleChain, c: char, auto_italic: bool) -> char {
let variant = EquationElem::variant_in(styles); let variant = EquationElem::variant_in(styles);
let bold = EquationElem::bold_in(styles); let bold = EquationElem::bold_in(styles);
let default_style = EquationElem::default_style_in(styles);
let default_italic = match c {
'a'..='z' | 'ħ' | 'ı' | 'ȷ' |
'α'..='ω' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ' =>
matches!(default_style, MathStyle::Iso | MathStyle::Tex | MathStyle::French),
'A'..='Z' =>
matches!(default_style, MathStyle::Iso | MathStyle::Tex),
'Α'..='Ω' =>
matches!(default_style, MathStyle::Iso),
'∂' =>
matches!(default_style, MathStyle::Tex | MathStyle::French),
_ => false
};
let italic = EquationElem::italic_in(styles).unwrap_or( let italic = EquationElem::italic_in(styles).unwrap_or(
auto_italic auto_italic && default_italic && matches!(variant, Sans | Serif),
&& matches!(
c,
'a'..='z' | 'ħ' | 'ı' | 'ȷ' | 'A'..='Z' |
'α'..='ω' | '∂' | 'ϵ' | 'ϑ' | 'ϰ' | 'ϕ' | 'ϱ' | 'ϖ'
)
&& matches!(variant, Sans | Serif),
); );
if let Some(c) = basic_exception(c) { if let Some(c) = basic_exception(c) {

View File

@ -14,7 +14,7 @@ use crate::layout::{
AlignElem, Alignment, BlockElem, InlineElem, OuterHAlignment, SpecificAlignment, AlignElem, Alignment, BlockElem, InlineElem, OuterHAlignment, SpecificAlignment,
VAlignment, VAlignment,
}; };
use crate::math::{MathSize, MathVariant}; use crate::math::{MathSize, MathStyle, MathVariant};
use crate::model::{Numbering, Outlinable, ParLine, Refable, Supplement}; use crate::model::{Numbering, Outlinable, ParLine, Refable, Supplement};
use crate::text::{FontFamily, FontList, FontWeight, LocalName, TextElem}; use crate::text::{FontFamily, FontList, FontWeight, LocalName, TextElem};
@ -52,6 +52,10 @@ pub struct EquationElem {
#[default(false)] #[default(false)]
pub block: bool, pub block: bool,
/// The rule determining which letters and symbols are italic by default.
#[default(MathStyle::Tex)]
pub default_style: MathStyle,
/// How to [number]($numbering) block-level equations. /// How to [number]($numbering) block-level equations.
/// ///
/// ```example /// ```example

View File

@ -252,3 +252,19 @@ pub enum MathVariant {
Mono, Mono,
Bb, Bb,
} }
/// A rule describing which letters and symbols are italic by default.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Cast, Hash)]
// #[derive(Serialize, Deserialize, Cast)]
// #[serde(rename_all = "kebab-case")]
pub enum MathStyle {
// Every latin or greek letter italic, [partial] upright.
Iso,
// Everything but capital greek letters italic.
Tex,
// Lowercase letters and [partial] italic.
French,
// Everything upright.
Upright
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 466 B

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,7 +1,16 @@
// Test text styling in math. // Test text styling in math.
--- math-style-italic-default --- --- math-style-italic-default ---
// Test italic defaults. // Test italic defaults for all `default-style`s.
$a, A, delta, ϵ, partial, Delta, ϴ$
#set math.equation(default-style: "iso")
$a, A, delta, ϵ, partial, Delta, ϴ$
#set math.equation(default-style: "french")
$a, A, delta, ϵ, partial, Delta, ϴ$
#set math.equation(default-style: "upright")
$a, A, delta, ϵ, partial, Delta, ϴ$ $a, A, delta, ϵ, partial, Delta, ϴ$
--- math-style --- --- math-style ---