Skip to main content

ratex_parser/functions/
enclose.rs

1use std::collections::HashMap;
2
3use crate::error::ParseResult;
4use crate::functions::{define_function_full, ArgType, FunctionContext, FunctionSpec};
5use crate::parse_node::ParseNode;
6
7pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
8    define_function_full(
9        map,
10        &["\\fbox"],
11        "enclose",
12        1,
13        0,
14        Some(vec![ArgType::HBox]),
15        false,
16        true,
17        true,
18        false,
19        false,
20        handle_fbox,
21    );
22
23    define_function_full(
24        map,
25        &["\\cancel", "\\bcancel", "\\xcancel", "\\phase"],
26        "enclose",
27        1,
28        0,
29        None,
30        false,
31        false,
32        true,
33        false,
34        false,
35        handle_cancel,
36    );
37
38    // \sout originates as a LaTeX text-mode command but KaTeX accepts it in
39    // both modes (allowedInText: true, no argType override → arg inherits
40    // surrounding mode). We mirror that so `\text{\sout{abc}}` parses while
41    // `\sout{x^2}` still keeps its argument in math mode.
42    define_function_full(
43        map,
44        &["\\sout"],
45        "enclose",
46        1,
47        0,
48        None,
49        false,
50        true,
51        true,
52        false,
53        false,
54        handle_cancel,
55    );
56
57    define_function_full(
58        map,
59        &["\\angl"],
60        "enclose",
61        1,
62        0,
63        Some(vec![ArgType::HBox]),
64        false,
65        false,
66        true,
67        false,
68        false,
69        handle_angl,
70    );
71}
72
73fn handle_fbox(
74    ctx: &mut FunctionContext,
75    args: Vec<ParseNode>,
76    _opt_args: Vec<Option<ParseNode>>,
77) -> ParseResult<ParseNode> {
78    Ok(ParseNode::Enclose {
79        mode: ctx.parser.mode,
80        label: "\\fbox".to_string(),
81        background_color: None,
82        border_color: None,
83        body: Box::new(args.into_iter().next().unwrap()),
84        loc: None,
85    })
86}
87
88fn handle_cancel(
89    ctx: &mut FunctionContext,
90    args: Vec<ParseNode>,
91    _opt_args: Vec<Option<ParseNode>>,
92) -> ParseResult<ParseNode> {
93    Ok(ParseNode::Enclose {
94        mode: ctx.parser.mode,
95        label: ctx.func_name.clone(),
96        background_color: None,
97        border_color: None,
98        body: Box::new(args.into_iter().next().unwrap()),
99        loc: None,
100    })
101}
102
103fn handle_angl(
104    ctx: &mut FunctionContext,
105    args: Vec<ParseNode>,
106    _opt_args: Vec<Option<ParseNode>>,
107) -> ParseResult<ParseNode> {
108    Ok(ParseNode::Enclose {
109        mode: ctx.parser.mode,
110        label: "\\angl".to_string(),
111        background_color: None,
112        border_color: None,
113        body: Box::new(args.into_iter().next().unwrap()),
114        loc: None,
115    })
116}