Learn R Programming

Hmisc (version 5.3-0)

typstTranslate: Translate a Character String for Typst Markup

Description

Translates arbitrary character strings -- variable labels, titles, captions, and similar free text -- so they display correctly as literal text in Typst markup, with optional math-mode handling for superscripts and Greek letter names. This is the Typst analog of latexTranslate and htmlTranslate.

Usage

typstTranslate(object, greek = FALSE, na = "", ...)

Value

A character vector of the same length as object, containing valid Typst markup safe for direct insertion into Typst source (for example, inside a ```{=typst} raw block, or as the content of a native #table() cell).

Arguments

object

A character vector, or an object coercible to one via as.character, to be translated for display in Typst markup.

greek

Logical. If TRUE, whole-word matches of Greek letter names (alpha, beta, Gamma, etc.) are wrapped in Typst math mode so they render as the actual Greek glyph rather than the literal English name. Default is FALSE.

na

Character string to substitute for NA elements of object before translation. Default is ''.

...

Currently unused; present for signature compatibility with latexTranslate/htmlTranslate.

Details

typstTranslate is built entirely from vectorized base gsub calls rather than sedit, and is deliberately simpler than latexTranslate in several respects:

  • Several characters latexTranslate escapes (|, %, &, >, the pound sign) have no special meaning in Typst markup and are left untouched here -- carrying over LaTeX's escaping list unchanged would add noise, not correctness.

  • Where LaTeX requires math mode for symbols such as \leq/\geq (they do not exist in text mode at all), Typst can use the literal Unicode character directly in ordinary text, so <= and >= translate straight to Unicode rather than opening a math span.

  • Superscripts (^digits) are rendered with Typst's text-mode #super[] function rather than a bare math-mode span. A bare $^(3)$ span with no base inside the same span is invalid Typst syntax ("unexpected hat") unless something happens to merge a base into it, and even when made syntactically valid, pulling an ordinary word into math mode causes Typst to auto-italicize/treat it as bare variable letters rather than showing plain upright text. #super[] avoids both problems: it is valid immediately after any preceding content (or none), and does not restyle whatever precedes it.

  • Greek letter names are recognized as whole words (via a single vectorized regex alternation, not a per-letter loop) and wrapped in $...$ so they render as the actual Greek glyph.

Characters that are escaped, because they are syntactically meaningful in Typst markup mode and could otherwise corrupt output or (in the case of <) cause an outright compile error, are # < _ @ ` * ~ and the backslash itself. < specifically needs this because Typst reads a bare < as the start of label-reference syntax (<name>); an unescaped value such as a P-value formatted as "<0.001" will otherwise break Typst compilation with an "unclosed label" error. _ and * are escaped unconditionally (not only where they would definitely cause trouble) because ordinary variable names and labels routinely contain underscores or asterisks, and Typst's emphasis/strong-text shorthand can trigger across unrelated stretches of text if these are left unescaped.

Math spans that end up touching with no text between them (which can happen, for example, when a Greek-letter substitution lands immediately next to another math-producing substitution) are merged into one continuous span. Typst reads two adjacent $...$$...$ spans as an empty math block followed by bare text -- a real compile error ("unexpected hat"), not merely a cosmetic issue -- so this merge is applied unconditionally wherever it is safe to do so.

Not carried over from latexTranslate: the pb argument (auto-sizing \left/\right delimiters) and the inn/out extension arguments -- Typst does not need LaTeX's delimiter-sizing workaround for ordinary text-mode brackets, and the extension hooks were not needed for current use cases.

See Also

latexTranslate, htmlTranslate, typstAsis

Examples

Run this code
typstTranslate("P<0.001")                     # escapes the literal <
typstTranslate("Volume (cm^3)")                # #super[] superscript
typstTranslate("rho coefficient", greek=TRUE)  # real Greek rho glyph
typstTranslate("chi^2 statistic", greek=TRUE)  # merged math span

Run the code above in your browser using DataLab