1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// commands, macros and stuff.
use crate::*;
/// Latex macros.
/// Rudimentary so far, have to embed latex.
/// Please refer to README for usage.
#[derive(Debug, Clone)]
pub struct Command {
pub name: String,
pub nargs: usize,
pub def: String, // actual latex, cannot help
}
impl Command {
/// 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.
pub fn new(name: &str, nargs: usize, def: &str) -> Self {
Self {
name: name.to_string(),
nargs,
def: def.to_string(),
}
}
/// I'd really prefer you try and use the `command!` macro.
pub fn call(&self, args: Vec<&str>) -> Res<String> {
if args.len() != self.nargs {
return Err(TexError::ArgLen.into());
}
let temp = format!(
"\\{}{}",
self.name,
args.iter().map(|x| format!("{{{x}}}")).collect::<String>()
);
Ok(temp)
}
/// \\newcommand
pub fn declare(&self) -> String {
format!(
"\\newcommand{{\\{}}}[{}]{{{}}} ",
self.name, self.nargs, self.def
)
}
}