Skip to content

Commit

Permalink
feat: add support for \includegraphics
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeX4 committed Apr 18, 2024
1 parent 7db8519 commit 368fc8b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
77 changes: 77 additions & 0 deletions crates/mitex/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ impl Converter {
"label" => {
self.convert_command_label(f, &cmd)?;
}
"includegraphics" => {
self.convert_command_includegraphics(f, &cmd)?;
}
_ => {
self.convert_normal_command(f, elem, spec)?;
}
Expand Down Expand Up @@ -509,6 +512,80 @@ impl Converter {
Ok(())
}

/// Convert command `\includegraphics[width=0.5\textwidth]{example-image}`
fn convert_command_includegraphics(
&mut self,
f: &mut fmt::Formatter<'_>,
cmd: &CmdItem,
) -> Result<(), ConvertError> {
let opt_arg = cmd.arguments().find(|arg| {
matches!(
arg.first_child().unwrap().kind(),
LatexSyntaxKind::ItemBracket
)
});
let arg = cmd
.arguments()
.find(|arg| {
matches!(
arg.first_child().unwrap().kind(),
LatexSyntaxKind::ItemCurly
)
})
.expect("\\includegraphics command must have one argument");
// remove { and } then trim
let body = arg.text().to_string();
let body = &body[1..(body.len() - 1)];
let body = body.trim();
f.write_str("#image(")?;
// optional arguments
if let Some(opt_arg) = opt_arg {
let arg_text = opt_arg.text().to_string();
let arg_text = &arg_text[1..(arg_text.len() - 1)];
let arg_text = arg_text.trim();
// example: \includegraphics[width=0.5\textwidth, height=3cm,
// angle=45]{example-image} split by comma and convert
// to key-value pairs
let args = arg_text.split(',').collect::<Vec<_>>();
let args = args
.iter()
.map(|arg| {
let arg = arg.trim();
let arg = arg.split('=').collect::<Vec<_>>();
let key = arg[0].trim();
let value = if arg.len() == 2 { arg[1].trim() } else { "" };
(key, value)
})
.collect::<Vec<_>>();
for (key, value) in args.iter() {
if matches!(key, &"width" | &"height") {
f.write_str(key)?;
f.write_char(':')?;
f.write_char(' ')?;
if value.ends_with("\\textwidth") {
let value = value.trim_end_matches("\\textwidth");
f.write_str(value)?;
f.write_str(" * 100%")?;
} else if value.ends_with("\\textheight") {
let value = value.trim_end_matches("\\textheight");
f.write_str(value)?;
f.write_str(" * 100%")?;
} else {
f.write_str(value)?;
}
f.write_char(',')?;
f.write_char(' ')?;
}
}
}
// image path
f.write_char('"')?;
f.write_str(body)?;
f.write_char('"')?;
f.write_char(')')?;
Ok(())
}

/// Convert normal command
fn convert_normal_command(
&mut self,
Expand Down
4 changes: 2 additions & 2 deletions crates/mitex/tests/cvt/figure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use super::prelude::*;
fn figure() {
assert_snapshot!(convert_text(r###"\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\includegraphics[width=0.5\textwidth, height=3cm, angle=45]{example-image.png}
\caption{This is an example image.}
\label{fig:example}
\end{figure}"###).unwrap(), @r###"
#figure(caption: [This is an example image.],)[
#miteximage[\[width=0.5 \]][example-image];
#image(width: 0.5 * 100%, height: 3cm, "example-image.png")
];<fig:example>
Expand Down
2 changes: 1 addition & 1 deletion packages/mitex/specs/latex/standard.typ
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
tabular: define-env(1, kind: "is-table", alias: "mitextable", handle: (..args) => "tabular()"),
// commands for figure
centering: ignore-sym,
textwidth: ignore-sym,
textwidth: sym,
caption: define-cmd(1, alias: "mitexcaption", handle: ignore-me),
includegraphics: define-glob-cmd("{,b}t", "#miteximage", handle: ignore-me),
// Spaces: \! \, \> \: \; \ \quad \qquad
Expand Down

0 comments on commit 368fc8b

Please sign in to comment.