Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forty-suns-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tex-to-typst': patch
---

Support quoted math text
5 changes: 5 additions & 0 deletions .changeset/little-foxes-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tex-to-typst': patch
---

Support cases
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,10 @@ function convert(state: IState, node: LatexNode) {
}

function convertText(state: IState, text: string): string {
if (!(text in typstStrings)) return text;
const result = typstStrings[text];
if (typeof result === 'function') return result(state) || text;
return result || text;
if (typeof result === 'function') return result(state);
return result;
}

export function writeTypst(node: LatexNode, state: IState = new State()) {
Expand Down
42 changes: 39 additions & 3 deletions src/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ function isEmptyNode(node?: LatexNode): boolean {
}

export const typstStrings: Record<string, string | ((state: IState) => string)> = {
',': (state) =>
state.data.inFunction && (state as any)._currentFunctions.slice(-1)[0] !== 'text'
',': (state) => {
// LaTeX cases rows are often written as `i, & condition`; the comma is
// typographic and `&` already maps to the Typst cases argument separator.
if (state.data.inCases) return '';
return state.data.inFunction && (state as any)._currentFunctions.slice(-1)[0] !== 'text'
? 'comma'
: ',',
: ',';
},
'&': (state) => (state.data.inArray ? ',' : '&'),
'/': '\\/',
';': '\\;',
Expand Down Expand Up @@ -77,6 +81,17 @@ for (const [typst, sym] of Object.entries(symbols)) {
}
}

function quotedMathText(state: IState, node: LatexNode, wrapper: string) {
const arg = node.args?.[0] as LatexNode;
node.args = [];
state.write(`${wrapper}(`);
state.openFunction('text');
state.writeChildren(arg);
state.closeFunction();
state.write(')');
return '';
}

const baseMacros: Record<string, string | ((state: IState, node: LatexNode) => string)> = {
$: '\\$',
dfrac: 'frac',
Expand All @@ -99,6 +114,10 @@ const baseMacros: Record<string, string | ((state: IState, node: LatexNode) => s
mathrm: 'upright',
textrm: 'upright',
rm: 'upright',
textit: (state, node) => quotedMathText(state, node, 'italic'),
emph: (state, node) => quotedMathText(state, node, 'italic'),
textbf: (state, node) => quotedMathText(state, node, 'bold'),
texttt: (state, node) => quotedMathText(state, node, 'mono'),
mbox: (state, node) => {
const arg = node.args?.[0] as LatexNode;
node.args = [];
Expand Down Expand Up @@ -176,6 +195,9 @@ const baseMacros: Record<string, string | ((state: IState, node: LatexNode) => s
mathop: 'op',
'\\': (state, node) => {
node.args = [];
if (state.data.inCases) {
return ',';
}
if (state.data.inArray) {
state.data.previousMatRows = (state.data.previousMatRows ?? 0) + 1;
if ((state as any)._value.slice(-1) === ']') state.addWhitespace();
Expand All @@ -185,6 +207,9 @@ const baseMacros: Record<string, string | ((state: IState, node: LatexNode) => s
},
cr: (state, node) => {
node.args = [];
if (state.data.inCases) {
return ',';
}
if (state.data.inArray) {
state.data.previousMatRows = (state.data.previousMatRows ?? 0) + 1;
if ((state as any)._value.slice(-1) === ']') state.addWhitespace();
Expand Down Expand Up @@ -348,13 +373,24 @@ const matrixEnv = (delim?: string) => (state: IState, node: LatexNode) => {
state.data.inArray = false;
};

const casesEnv = (state: IState, node: LatexNode) => {
state.data.inArray = true;
state.data.inCases = true;
state.openFunction('cases');
state.writeChildren(node);
state.closeFunction();
state.data.inArray = false;
state.data.inCases = false;
};

export const typstEnvs: Record<string, (state: IState, node: LatexNode) => void> = {
array: matrixEnv(),
matrix: matrixEnv(),
pmatrix: matrixEnv('('),
bmatrix: matrixEnv('['),
Bmatrix: matrixEnv('{'),
vmatrix: matrixEnv('|'),
cases: casesEnv,
aligned(state, node) {
state.writeChildren(node);
},
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type StateData = {
writeOutBrackets?: boolean;
inFunction?: boolean;
inArray?: boolean;
inCases?: boolean;
previousMatRows?: number;
macros?: Set<string>;
};
Expand Down
12 changes: 12 additions & 0 deletions tests/math.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ cases:
- title: text subscript in function
tex: '\text{SR} = \sum{t_{active}}'
typst: '"SR" = sum t_(a c t i v e)'
- title: textit
tex: '\textit{Enrichment score} = \frac{\textit{Count}}{\textit{Pool}}'
typst: 'italic("Enrichment score") = frac(italic("Count"), italic("Pool"))'
- title: tilde
tex: '\tilde{I}'
typst: 'tilde(I)'
Expand Down Expand Up @@ -393,6 +396,9 @@ cases:
- title: left right angle
tex: '\left\langle A \right\rangle'
typst: 'chevron.l A chevron.r'
- title: triangleq
tex: 'F_A(k) \triangleq \mathcal{F}[\tilde{A}](k)'
typst: 'F_A (k) eq.delta cal(F) [ tilde(A) ] (k)'
- title: horizontal space
tex: '\hspace{1cm}'
typst: '#h(1cm)'
Expand Down Expand Up @@ -459,3 +465,9 @@ cases:
- title: mismatched bracket delimiters around matrix use lr
tex: '\left[ \begin{matrix} a & b \end{matrix} \right)'
typst: 'lr([mat(delim: #none, a, b)))'
- title: cases environment
tex: 'A_0(x) = \begin{cases} A(x), & A(x) \geq c \\ 0, & \text{otherwise} \end{cases}'
typst: 'A_0 (x) = cases(A (x), A (x) gt.eq c, 0, "otherwise")'
- title: cases environment with floor
tex: '\text{wrap}(i; n) = \begin{cases} i, & i \leq \lfloor n/2 \rfloor \\ i - n, & \text{otherwise.} \end{cases}'
typst: '"wrap"(i \; n) = cases(i, i lt.eq floor.l n \/ 2 floor.r, i -n, "otherwise.")'
Loading