···4646 Ok(typst)
4747}
48484949+pub fn tex2typst_with_macros(tex: &str, custom_macros: &HashMap<String, String>) -> Result<String, String> {
5050+ let tex_tree = tex_parser::parse_tex(tex, custom_macros)?;
5151+ let typst_tree = converter::convert_tree(&tex_tree)?;
5252+ let mut writer = typst_writer::TypstWriter::new();
5353+ writer.serialize(&typst_tree)?;
5454+ let typst = writer.finalize()?;
5555+ Ok(typst)
5656+}
5757+4958/// Converts a given input string containing TeX math expressions to Typst format.
5059///
5160/// This function searches for inline and display math expressions within the input string,
+19-1
src/tests.rs
···33#[cfg(test)]
44mod tests {
55 use std::collections::HashMap;
66- use crate::{converter, tex2typst, tex_parser, text_and_tex2typst, typst_writer};
66+ use std::error::Error;
77+ use std::result;
88+ use crate::{converter, tex2typst, tex2typst_with_macros, tex_parser, text_and_tex2typst, typst_writer};
79810 #[test]
911 fn simple_test() {
···4143 let typst = text_and_tex2typst(tex).unwrap_or_else(|e| format!("Error: {}", e));
4244 println!("{}", &typst);
4345 Ok(())
4646+ }
4747+4848+ #[test]
4949+ fn test_macros() {
5050+ let tex = r"\d^2";
5151+ let mut custom_macros = HashMap::new();
5252+ custom_macros.insert(r"\d".to_string(), r"\partial".to_string());
5353+ let result = tex2typst_with_macros(tex, &custom_macros).unwrap_or_else(|e| format!("Error: {}", e));
5454+ println!("{}", result);
5555+ }
5656+5757+ #[test]
5858+ fn test_floor() {
5959+ let tex = r"\left\lfloor \frac{a}{b} \right\rfloor \floor{\frac{a}{b}}";
6060+ let result = tex2typst(tex).unwrap();
6161+ println!("{}", result);
4462 }
45634664 #[test]