Skip to content

Commit

Permalink
Merge pull request #704 from Ducasse/BetterMathSupport
Browse files Browse the repository at this point in the history
Improving math support
  • Loading branch information
Ducasse authored Apr 14, 2024
2 parents 2c839b0 + 5dd8e0d commit 7f4b772
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
52 changes: 52 additions & 0 deletions src/Microdown-LaTeXExporter-Tests/MicLaTeXWriterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,37 @@ MicLaTeXWriterTest >> testLinkBold [
'\href{/anUrl}{\textbf{Foo}}', newLine
]

{ #category : 'tests - math' }
MicLaTeXWriterTest >> testMathBloc [

self
parseInlined:'$$
f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz
$$'

andCheckWeGet:

'\begin{equation}', newLine,
'f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz', newLine,
'\end{equation}'
]

{ #category : 'tests - math' }
MicLaTeXWriterTest >> testMathBlocWithLabel [

self
parseInlined:'$$ % label=eq1
f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz
$$'

andCheckWeGet:

'\begin{equation}', newLine,
'\label{eq1}', newLine,
'f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz', newLine,
'\end{equation}'
]

{ #category : 'tests - formats' }
MicLaTeXWriterTest >> testMonospace [

Expand Down Expand Up @@ -417,6 +448,27 @@ MicLaTeXWriterTest >> testRealLinkSample [
'\href{http://www.pharo.org}{The Pharo Project}', newLine
]

{ #category : 'tests - math' }
MicLaTeXWriterTest >> testSimpleMathBloc [

| root math |
root := parser parse: '
$$
f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz
$$'.
math := root children first.
self
parseInlined:'$$
f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz
$$'

andCheckWeGet:

'\begin{equation}', newLine,
'f(a) = \frac{1}{2\pi i} \oint_{\gamma} \frac{f(z)}{z - a} dz', newLine,
'\end{equation}'
]

{ #category : 'tests - formats' }
MicLaTeXWriterTest >> testStrike [

Expand Down
17 changes: 17 additions & 0 deletions src/Microdown-LaTeXExporter/MicLaTeXWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,23 @@ MicLaTeXWriter >> visitListItem: anItem [
canvas newLine
]

{ #category : 'visiting' }
MicLaTeXWriter >> visitMath: aMicMathEnvironment [
"here we should add a label if there is one"

canvas newLine.
canvas environment
name: 'equation';
with: [
| args |
args := aMicMathEnvironment arguments.
(args includesKey: 'label')
ifTrue: [ canvas raw: '\label{', (args at: 'label'), '}'.
canvas newLine.].

canvas raw: aMicMathEnvironment body. canvas newLine ]
]

{ #category : 'blocks - inline' }
MicLaTeXWriter >> visitMonospace: aMonospace [
canvas command
Expand Down
13 changes: 13 additions & 0 deletions src/Microdown-Tests/MicArgumentsSplitterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ MicArgumentsSplitterTest >> testArguments [
self assert: (subject at: #foo) equals: 'bar'
]

{ #category : 'tests' }
MicArgumentsSplitterTest >> testArgumentsForMath [
| subject |
subject := self subjectUnderTestWith: '%foo=bar'.
self assert: (subject at: #foo) equals: 'bar'.

subject := self subjectUnderTestWith: ' %foo=bar'.
self assert: (subject at: #foo) equals: 'bar'.

subject := self subjectUnderTestWith: ' % foo=bar'.
self assert: (subject at: #foo) equals: 'bar'.
]

{ #category : 'tests' }
MicArgumentsSplitterTest >> testCanInstantiate [
| subject |
Expand Down
8 changes: 8 additions & 0 deletions src/Microdown-Tests/MicMathBlockTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ V_i = C_0 - C_3
MicMathBlockTest >> testMathBlockWithParametersAndLatexComment [

| root math |

"note that in the following
$$%label=eq11
The % is needed else it does not work on github and the label is part of the equation."



root := self parserClass new parse: '
',MathOpeningBlockMarkup,'%key=cite&label=42
Expand Down
22 changes: 16 additions & 6 deletions src/Microdown/MicArgumentList.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,24 @@ MicArgumentList >> defaultValue [
{ #category : 'initialization' }
MicArgumentList >> from: fullString [
"I am the 'parser' of the argument splitter"
| barSplit |
fullString isEmpty ifTrue: [ ^ self ].
(fullString intersection: '|=&') isEmpty
ifTrue: [ ^self setDefaultNoArguments: fullString ].
barSplit := fullString splitOn: $|.
| barSplit string |
string := fullString.
string isEmpty ifTrue: [ ^ self ].

"for math equation we can have
$$ % label=eqq1 or
$$ %label=eqq2"
(string intersection: '%') isNotEmpty
ifTrue: [ string := string trim.
string first = $%
ifTrue: [ string := string allButFirst trim ] ].

(string intersection: '|=&') isEmpty
ifTrue: [ ^self setDefaultNoArguments: string ].
barSplit := string splitOn: $|.
barSplit size > 1
ifTrue: [ ^ self setDefaultAndArguments: barSplit ].
self setNoDefaultButArguments: fullString
self setNoDefaultButArguments: string

]

Expand Down
12 changes: 12 additions & 0 deletions src/Microdown/MicMathBlock.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ $$
V_i = C_0 - C_3
$$
Now if you want to have label and references to an equation as in LaTeX you should prefix the parameter with % (else it will break on github).
```
$$ % label=theEq
V_i = C_0 - C_3
$$
See *@theEq@*
```
"
Class {
Expand Down

0 comments on commit 7f4b772

Please sign in to comment.