Skip to main content

ratex_parser/functions/
styling.rs

1use std::collections::HashMap;
2
3use crate::error::ParseResult;
4use crate::functions::{define_function_full, FunctionContext, FunctionSpec};
5use crate::parse_node::{ParseNode, StyleStr};
6
7pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
8    define_function_full(
9        map,
10        &[
11            "\\displaystyle",
12            "\\textstyle",
13            "\\scriptstyle",
14            "\\scriptscriptstyle",
15        ],
16        "styling",
17        0, 0, None,
18        true,  // allowed_in_argument
19        true,
20        true,
21        false, false,
22        handle_styling,
23    );
24}
25
26fn handle_styling(
27    ctx: &mut FunctionContext,
28    _args: Vec<ParseNode>,
29    _opt_args: Vec<Option<ParseNode>>,
30) -> ParseResult<ParseNode> {
31    let break_on = ctx.break_on_token_text.clone();
32    let body = ctx.parser.parse_expression(true, break_on.as_deref())?;
33
34    let style = match ctx.func_name.as_str() {
35        "\\displaystyle" => StyleStr::Display,
36        "\\textstyle" => StyleStr::Text,
37        "\\scriptstyle" => StyleStr::Script,
38        "\\scriptscriptstyle" => StyleStr::Scriptscript,
39        _ => StyleStr::Text,
40    };
41
42    Ok(ParseNode::Styling {
43        mode: ctx.parser.mode,
44        style,
45        body,
46        loc: None,
47    })
48}