Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic text mode #86

Merged
merged 23 commits into from Dec 31, 2023
Merged

feat: basic text mode #86

merged 23 commits into from Dec 31, 2023

Conversation

OrangeX4
Copy link
Member

@OrangeX4 OrangeX4 commented Dec 28, 2023

  • Basic text mode support, you can use it to write LaTeX drafts.
    • \section, \textbf, \emph.
    • Inline and block math equations.
    • \ref, \eqref and \label.
    • itemize and enumerate environments.

  • Use mitex-convert to convert LaTeX code into Typst code in string.
  • Use mi to embed an inline LaTeX equation into Typst.
  • Use mitex(numbering: none, supplement: auto, ..) to embed a block LaTeX equation into Typst.
  • Use mitext to embed a LaTeX text into Typst.
#import "@preview/mitex:0.1.0": *

#mitext(`
  \iftypst
    #set math.equation(numbering: "(1)", supplement: "equation")
  \fi

  \section{Title}

  A \textbf{strong} text, a \emph{emph} text and inline equation $x + y$.
  
  Also block \eqref{eq:pythagoras}.

  \begin{equation}
    a^2 + b^2 = c^2 \label{eq:pythagoras}
  \end{equation}
  
  \begin{enumerate}
    \item This is the first item
    \item This is the second item
    \begin{itemize}
      \item This is the first subitem
      \item This is the second subitem
    \end{itemize}
  \end{enumerate}
`)

@OrangeX4 OrangeX4 added the draft label Dec 28, 2023
@OrangeX4 OrangeX4 changed the title Text Mode for MiTeX feat: text mode Dec 28, 2023
@OrangeX4 OrangeX4 marked this pull request as draft December 28, 2023 06:34
@OrangeX4 OrangeX4 assigned OrangeX4 and unassigned OrangeX4 Dec 28, 2023
Copy link
Collaborator

@Myriad-Dreamin Myriad-Dreamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments

@@ -476,9 +611,39 @@ static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy:
#[cfg(test)]
mod tests {
use insta::{assert_debug_snapshot, assert_snapshot};
use mitex_parser::spec::CommandSpec;

static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy::new(|| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is already a DEFAULT_SPEC in crate, we can import it by use crate::DEFAULT_SPEC;

self.2.borrow_mut().push_str(&e.to_string());
let mut ctx = Converter::new(self.1);
if let Err(e) = ctx.convert(f, self.0.clone(), &self.2) {
self.3.borrow_mut().push_str(&e.to_string());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two many fields in a single tuple. I would prefer to refactoring TypstMathRepr as

struct TypstRepr  {
  elem: LatexSyntaxElem,
  mode: LaTeXMode,
  spec: CommandSpec,
  error: Rc<RefCell<String>>
}

@Myriad-Dreamin Myriad-Dreamin linked an issue Dec 28, 2023 that may be closed by this pull request
4 tasks
@OrangeX4 OrangeX4 removed the draft label Dec 30, 2023
@OrangeX4 OrangeX4 changed the title feat: text mode feat: basic text mode Dec 30, 2023
@OrangeX4 OrangeX4 marked this pull request as ready for review December 30, 2023 09:36
Copy link
Collaborator

@Myriad-Dreamin Myriad-Dreamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. 😁

README.md Outdated

## Features to Implement

- [ ] Pass command specification to MiTeX plugin dynamically. With that you can define a typst function `let myop(it) = op(upright(it))` and then use it by `\myop{it}`.
- [ ] Package support, which means that you can change set of commands by telling MiTeX to use a list of packages.
- [ ] Text mode support, enabling the rendering entire LaTeX documents in Typst!
- [ ] More text mode support, enabling the rendering entire LaTeX documents in Typst!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"More text mode support" is not rather descriptive, could we mention at least one direction to improve?

Comment on lines +281 to +294
TokenWhiteSpace => {
if self.skip_next_space {
self.skip_next_space = false;
return Ok(());
}
write!(f, "{}", elem.as_token().unwrap().text())?;
}
TokenLineBreak => {
write!(f, "{}", elem.as_token().unwrap().text())?;
// indent for itemize and enumerate
for _ in 0..self.indent {
f.write_char(' ')?;
}
self.skip_next_space = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A state switch self.skip_next_space looks easily broken to me. It doesn't quite resist AST changes. We may take a look on how typst processes spaces, which should be inspiring.

And I also think it is okay to have self.skip_next_space merged into main currently, but may we create a todo for it?

if name == "item" {
if matches!(self.env, LaTeXEnv::Itemize | LaTeXEnv::Enumerate) {
f.write_char('\n')?;
for _ in 0..(self.indent - 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can collect indent things into a helper function?

for _ in 0..(self.indent - 2) {
f.write_char(' ')?;
}
if matches!(self.env, LaTeXEnv::Itemize) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, we can have a helper to make conditional output, which doesn't change performance at all, but improves readability.

Comment on lines 25 to 42
\section{Title}

A \textbf{strong} text, a \emph{emph} text and inline equation $x + y$.

Also block \eqref{eq:pythagoras}.

\begin{equation}
a^2 + b^2 = c^2 \label{eq:pythagoras}
\end{equation}

\begin{enumerate}
\item This is the first item
\item This is the second item
\begin{itemize}
\item This is the first subitem
\item This is the second subitem
\end{itemize}
\end{enumerate}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we shorten example of text mode a bit? Just three to five lines would be okay. IMO, the example should be cool and representative and don't have to show all features.

if mode == "math" {
str(mitex-wasm.convert_math(bytes(get-elem-text(it)), spec))
} else {
str(mitex-wasm.convert_text(bytes(get-elem-text(it)), spec))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repeated pattern of code

/// Therefore you need to use `(.. arg) = > {..}` to receive them.
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-math-env(num, alias: none, handle: none) = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can have a common define-env and the rest of define-xxx-env are just alias to define-env.with(ctx_feature: ...)

@@ -210,7 +253,12 @@
spec.insert(key, value.at(0))
if value.at(1) != none {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many value.at(0) and value.at(1), could be destruct value into nice names?

@OrangeX4 OrangeX4 merged commit 6c19bab into main Dec 31, 2023
5 checks passed
@OrangeX4 OrangeX4 deleted the text-mode branch January 1, 2024 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plan to convert users' TeX document into Typst code
2 participants