rust_texas/
commands.rs

1// commands, macros and stuff.
2
3use crate::prelude::*;
4
5/// Latex macros.
6/// Rudimentary so far, have to embed latex.
7/// 
8/// - name: Name of the new latex macro
9/// - nargs: Number of Args
10/// - def: definition of the command.
11/// 
12/// Compiles to \newcommand{\<name>}[<nargs>]{<def>}
13#[derive(Debug, Clone)]
14pub struct Command {
15    pub name: String,
16    pub nargs: usize,
17    pub def: String, // actual latex, cannot help
18}
19
20impl Command {
21    /// You use this to make a new command (duh), but you need the `doc.new_command()` call to actually be able to access it within the document.
22    pub fn new(name: &str, nargs: usize, def: &str) -> Self {
23        Self {
24            name: name.to_string(),
25            nargs,
26            def: def.to_string(),
27        }
28    }
29
30    /// I'd really prefer you try and use the `command!` macro.
31    pub fn call(&self, args: Vec<&str>) -> TexResult<String> {
32        if args.len() != self.nargs {
33            return Err(TexError::ArgLen.into());
34        }
35        let temp = format!(
36            "\\{}{}",
37            self.name,
38            args.iter().map(|x| format!("{{{x}}}")).collect::<String>()
39        );
40        Ok(temp)
41    }
42
43    /// \\newcommand
44    pub fn declare(&self) -> String {
45        format!(
46            "\\newcommand{{\\{}}}[{}]{{{}}} ",
47            self.name, self.nargs, self.def
48        )
49    }
50}