-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for the basic proof envs. However cross references to theorems are not working * Add myst-to-tex to dev deps of jats-to-myst * Add ruleid --------- Co-authored-by: Rowan Cockett <rowanc1@gmail.com> Co-authored-by: Franklin Koch <franklin@curvenote.com>
- Loading branch information
1 parent
27f48db
commit fde2144
Showing
12 changed files
with
373 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'myst-transforms': patch | ||
'myst-ext-proof': patch | ||
'jats-to-myst': patch | ||
'myst-to-tex': patch | ||
--- | ||
|
||
Add support for proofs in LaTeX |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { proofDirective } from './proof.js'; | ||
export { ProofKind } from './types.js'; | ||
export type { ProofKind, ProofContainer } from './types.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,26 @@ | ||
export enum ProofKind { | ||
proof = 'proof', | ||
axiom = 'axiom', | ||
lemma = 'lemma', | ||
definition = 'definition', | ||
criterion = 'criterion', | ||
remark = 'remark', | ||
conjecture = 'conjecture', | ||
corollary = 'corollary', | ||
algorithm = 'algorithm', | ||
example = 'example', | ||
property = 'property', | ||
observation = 'observation', | ||
proposition = 'proposition', | ||
assumption = 'assumption', | ||
theorem = 'theorem', | ||
} | ||
import type { Container } from 'myst-spec'; | ||
|
||
const PROOF_KINDS = [ | ||
'proof', | ||
'axiom', | ||
'lemma', | ||
'definition', | ||
'criterion', | ||
'remark', | ||
'conjecture', | ||
'corollary', | ||
'algorithm', | ||
'example', | ||
'property', | ||
'observation', | ||
'proposition', | ||
'assumption', | ||
'theorem', | ||
] as const; | ||
type ProofKinds = typeof PROOF_KINDS; | ||
|
||
export type ProofKind = ProofKinds[number]; | ||
|
||
export type ProofContainer = Omit<Container, 'kind'> & { | ||
kind: ProofKind; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { fileError, writeTexLabelledComment, RuleId } from 'myst-common'; | ||
import type { GenericNode } from 'myst-common'; | ||
import type { Text } from 'myst-spec'; | ||
import type { ProofContainer, ProofKind } from 'myst-ext-proof'; | ||
import { select } from 'unist-util-select'; | ||
import { remove } from 'unist-util-remove'; | ||
import type { Handler } from './types.js'; | ||
|
||
function kindToEnvironment(kind: ProofKind): string { | ||
switch (kind) { | ||
case 'theorem': | ||
return 'theorem'; | ||
case 'proof': | ||
return 'proof'; | ||
case 'proposition': | ||
return 'proposition'; | ||
case 'definition': | ||
return 'definition'; | ||
case 'example': | ||
return 'example'; | ||
case 'remark': | ||
return 'remark'; | ||
case 'axiom': | ||
return 'axiom'; | ||
case 'conjecture': | ||
return 'conjecture'; | ||
case 'lemma': | ||
return 'lemma'; | ||
case 'observation': | ||
return 'observation'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
|
||
export const proofHandler: Handler = (node, state) => { | ||
state.usePackages('amsthm'); | ||
|
||
const p = node as ProofContainer; | ||
const env = kindToEnvironment(p.kind); | ||
if (!env) { | ||
fileError(state.file, `Unhandled LaTeX proof environment "${p.kind}"`, { | ||
node, | ||
source: 'myst-to-tex', | ||
ruleId: RuleId.texRenders, | ||
}); | ||
return; | ||
} | ||
|
||
const t = select('admonitionTitle > text', node); | ||
if (t) { | ||
// Do not render the title twice | ||
t.type = '__delete__'; | ||
} | ||
const newNode = remove(node, '__delete__') as GenericNode; | ||
|
||
state.write('\\begin{'); | ||
state.write(env); | ||
state.write('}'); | ||
if (t) { | ||
state.write('['); | ||
state.write((t as Text).value); | ||
state.write(']'); | ||
} | ||
if (newNode.identifier && newNode.identifier.length > 0) { | ||
state.write('\\label{'); | ||
state.write(newNode.identifier); | ||
state.write('}'); | ||
} | ||
state.renderChildren(newNode, true); | ||
state.write('\\end{'); | ||
state.write(env); | ||
state.write('}'); | ||
|
||
state.data.hasProofs = true; | ||
}; | ||
|
||
export class TexProofSerializer { | ||
static COMMENT_LENGTH = 50; | ||
|
||
preamble: string; | ||
|
||
constructor() { | ||
this.preamble = this.renderThmDefinitions(); | ||
} | ||
|
||
private renderThmDefinitions(): string { | ||
const definitions = [ | ||
'\\newtheorem{theorem}{Theorem}[section]', | ||
'\\newtheorem{corollary}{Corollary}[theorem]', | ||
'\\newtheorem{lemma}[theorem]{Lemma}', | ||
'\\newtheorem{proposition}{Proposition}[section]', | ||
'\\newtheorem{definition}{Definition}[section]', | ||
'\\newtheorem{example}{Example}[section]', | ||
'\\newtheorem{remark}{Remark}[section]', | ||
'\\newtheorem{axiom}{Axiom}[section]', | ||
'\\newtheorem{conjecture}{Conjecture}[section]', | ||
'\\newtheorem{observation}{Observation}[section]', | ||
]; | ||
const block = writeTexLabelledComment( | ||
'theorem', | ||
definitions, | ||
TexProofSerializer.COMMENT_LENGTH, | ||
); | ||
const percents = ''.padEnd(TexProofSerializer.COMMENT_LENGTH, '%'); | ||
return `${percents}\n${block}${percents}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.