Skip to content

Commit 1e65bdc

Browse files
authored
Merge pull request #131 from barucden/hyphen
Replace hyphen for minus sign only inside math
2 parents 2158451 + f78d792 commit 1e65bdc

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

src/parser/commands_registration.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ for symbol in spaced_symbols
110110
symbol_to_canonical[symbol] = TeXExpr(:spaced, symbol_expr)
111111
end
112112

113-
# Special case for hyphen that must be replaced by a minus sign
114-
# TODO Make sure it is not replaced outside of math mode and when starting a group
115-
symbol_to_canonical['-'] = TeXExpr(:spaced, TeXExpr(:symbol, ''))
116-
117113
for com_str in spaced_commands
118114
symbol = get_symbol_char(com_str)
119115
symbol_expr = TeXExpr(:symbol, symbol)

src/parser/parser.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ function push_down!(stack)
6262

6363
if head(first(stack)) in [:subscript, :superscript]
6464
decoration = pop!(stack)
65-
decorated = pop!(first(stack))
65+
decorated = pop!(first(stack))
6666
decorated.args[subsuperindex(head(decoration))] = first(decoration.args)
6767
push!(first(stack).args, decorated)
68-
end
68+
end
6969

7070
conclude_command!!(stack)
7171
end
7272

7373
function conclude_command!!(stack)
74-
com = first(stack)
74+
com = first(stack)
7575
head(com) != :command && return false
7676
nargs = length(com.args) - 1
7777

@@ -111,7 +111,10 @@ arguments.
111111
Setting `showdebug` to `true` show a very verbose break down of the parsing.
112112
"""
113113
function texparse(tex ; root = TeXExpr(:lines), showdebug = false)
114+
contains_math = occursin(raw"$", tex)
115+
114116
stack = Stack{Any}()
117+
inside_math = false
115118
push!(stack, root)
116119
push!(stack, TeXExpr(:line))
117120

@@ -125,12 +128,14 @@ function texparse(tex ; root = TeXExpr(:lines), showdebug = false)
125128
try
126129
if token == dollar
127130
if head(first(stack)) == :inline_math
131+
inside_math = false
128132
push_down!(stack)
129133
else
134+
inside_math = true
130135
push!(stack, TeXExpr(:inline_math))
131136
end
132137
elseif token == newline
133-
if length(stack) > 2
138+
if length(stack) > 2
134139
throw(TeXParseError("unexpected new line", stack, length(tex), tex))
135140
end
136141

@@ -188,7 +193,20 @@ function texparse(tex ; root = TeXExpr(:lines), showdebug = false)
188193
push!(stack, TeXExpr(dec))
189194
end
190195
elseif token == char
191-
push!(stack, canonical_expr(tex[pos]))
196+
c = tex[pos]
197+
198+
# hyphen replaced by minus sign if expression does not contain
199+
# inline math at all, or the hyphen is inside inline math
200+
if c == '-'
201+
if !contains_math || inside_math
202+
expr = TeXExpr(:spaced, TeXExpr(:symbol, ''))
203+
else
204+
expr = TeXExpr(:char, '-')
205+
end
206+
else
207+
expr = canonical_expr(c)
208+
end
209+
push!(stack, expr)
192210
push_down!(stack)
193211
end
194212
catch err
@@ -210,4 +228,4 @@ function texparse(tex ; root = TeXExpr(:lines), showdebug = false)
210228
else
211229
return lines
212230
end
213-
end
231+
end

src/parser/texexpr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ end
125125
function Base.:(==)(tex1::TeXExpr, tex2::TeXExpr)
126126
childs1 = children(tex1)
127127
childs2 = children(tex2)
128-
128+
129129
length(childs1) != length(childs2) && return false
130130

131131
return all(childs1 .== childs2)
@@ -141,7 +141,7 @@ function leafmap(f, texexpr::TeXExpr)
141141
isleaf(texexpr) && return f(texexpr)
142142

143143
args = map(texexpr.args) do arg
144-
isnothing(arg) && return nothing
144+
arg isa TeXExpr || return arg
145145
return leafmap(f, arg)
146146
end
147147

test/parser.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ end
5656
@testset "Fonts" begin
5757
test_parse(raw"\mathrm{u}", (:font, :rm, (:char, 'u')))
5858
test_parse(raw"\text{u}", (:text, :rm, (:char, 'u')))
59-
60-
test_parse(raw"\mathrm{u v}", (:text, :rm,
59+
60+
test_parse(raw"\mathrm{u v}", (:text, :rm,
6161
(:group,
6262
(:char, 'u'),
6363
(:char, ' '),
6464
(:char, 'v')
6565
)))
66-
test_parse(raw"\text{u v}", (:text, :rm,
66+
test_parse(raw"\text{u v}", (:text, :rm,
6767
(:group,
6868
(:char, 'u'),
6969
(:char, ' '),
@@ -177,6 +177,14 @@ end
177177
)
178178
# Hyphen must be replaced by a minus sign
179179
test_parse(raw"-", (:spaced, (:symbol, '')))
180+
181+
test_parse(raw"a-b $c-d$",
182+
(:char, 'a'), (:char, '-'), (:char, 'b'),
183+
(:char, ' '),
184+
(:inline_math,
185+
(:char, 'c'),
186+
(:spaced, (:symbol, '')),
187+
(:char, 'd')))
180188
end
181189

182190
@testset "Subscript and superscript" begin
@@ -205,4 +213,4 @@ end
205213
(:decorated, (:symbol, 'ω'), 'k', nothing)
206214
)
207215
end
208-
end
216+
end

0 commit comments

Comments
 (0)