Any simple way for latex migration in str

*Without a package
(I saw mitex – Typst Universe)

I ended up using this thing

(feel free to roast my code in the comment :slight_smile: )

#let render_text(val) = {
  let render_text_str(val) = {
    let parse_url(value) = {
      let regex_full_url = regex("(.*)\\\href\{([^\{]*?)\}\{(.*)\}(.*)")
      let matched = value.match(regex_full_url)
      if matched == none {
        return value
      }
      let captures = matched.captures
      let before = captures.at(0)
      let url = captures.at(1)
      let (url_link, after) = (captures.at(2), captures.at(3))
      let url_link = render_text_str(url_link)
      return before + "#link(\"" + url + "\")[" + url_link + "]" + after
    }
    let parse_textbf(val) = {
      let regex_bf = regex("(.*)\\\\textbf\{([^\{]*?)\}(.*)")
      let mat = val.match(regex_bf)
      if mat == none {
        return val
      }
      return mat.captures.at(0) + " *" + mat.captures.at(1) + "* " + mat.captures.at(2)
    }
    let remove_new_line(val_li) = {
      return val_li.replace("\\newline", " \\ ")
    }
    let text = remove_new_line(val)
    let text = parse_url(text)
    let text = parse_textbf(text)
    return text
  }
  let final_text = render_text_str(val)
  eval(final_text, mode: "markup")
}

Could you please provide further explanation (maybe in OP) of what exact problem you have and what kind of help you want? I just see a big function definition with some LaTeX commands in strings. The title doesn’t make it clear what this is about, not 100%.

Yes indeed this function is used to decode some latex string into typist

Typical usage is that you have a latex string
This functions convert it to typist
But as you can see it’s not very clean and mostly incomplete

#let my_latex="\\textbf{testing}"

#render_text(my_latex)

So my question was, is there a better way to do it
(without a package)

no, that’s why the package exists >:) any reason you’re avoiding it?

I wanted to have a no dependency project. I will accept this answer if it’s not possible :confused:

Thanks!

It is difficult for machine conversion to avoid human edits. How about converting the markup before writing the document? You’ll end up with a dependency-free *.typ, but need two binaries (pandoc+typst) to start, however.

Try pandoc online

thanks for that idea!

If you want some features, you need the code. You can always take the package’s source code and put it in your project (with open source software at least – just be sure to respect the license) but I don’t really see the point in this – it pollutes your project with code that is not about the actual document, and gives you no benefits whatsoever over using the same code from the package.

(well, I can think of one benefit: it makes it a tiny bit easier to compile your code on a machine without internet connection. If that’s your concern, we can certainly help with that, without sacrificing one of Typst most convenient features)

2 Likes

It’s nowhere near node_modules. It’s better to use packages than hack your way around it. Makes it possible to keep the project lean, but also potentially have only one Typst file, which is very convenient to share/embed etc. In the endgame, you probably will need fewer packages, as Typst become much more complete.