Skip to content

Conversation

@AndPuQing
Copy link
Contributor

@qwinsi
Copy link
Owner

qwinsi commented Nov 29, 2025

Sorry but I think this feature has better way to implement.
To implement it please have a look on:

  • how \operatorname{XXX} is tokenized in tex-tokenizer.ts
  • how \operatorname{XXX} is converted in convert.ts

Or I will consider this work once I have time in the feature.

@AndPuQing
Copy link
Contributor Author

The LaTeX \mbox{} command needs to preserve its content as literal text in Typst, but unlike \operatorname{} or \text{}, it can contain nested LaTeX commands:

  • \mbox{mc}
  • \mbox{\textrm{a}}

The standard approach used for other commands: String.raw\\(text|operatorname|...){(.+?)}

This regex uses non-greedy matching (.+?) which stops at the first closing brace:

  • Input: \mbox{\textrm{a}}
  • Regex captures: \textrm{a (stops at first })
  • Remaining: } (unmatched, causes parse error)

I use manual brace counting in the tokenizer:

  1. Match \mbox{ with regex
  2. Scan character-by-character, maintaining a brace counter
  3. Increment on {, decrement on }
  4. Stop when counter reaches 0 (found matching brace)

This correctly handles: \mbox{\textrm{a}} → captures \textrm{a} as LITERAL

In convert.ts, I check for \mbox before calling convert_tex_node_to_typst(node.args[0]) because:

  • The LITERAL token contains raw LaTeX like \textrm{a}
  • The converter doesn't recognize \textrm{a} as a valid token (it's meant to stay as literal text)
  • We access the raw content directly and escape backslashes for Typst strings

Result: \mbox{\textrm{a}} → "\textrm{a}" (Typst text with escaped backslash)

@qwinsi
Copy link
Owner

qwinsi commented Nov 30, 2025

So the support for this feature is quite challenging.

Letting it alone until an enhancement is applied on the tex parser in the future. (e.g. LatexParser should have a member vairable such as this.envronment: "literal" | "math")

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.

2 participants