@@ -27,100 +27,119 @@ function Base.get(d::CanonicalDict, key, default)
2727 return default
2828end
2929
30-
31- # Each symbol or command has a unique canonical representation
30+ # Each symbol or com_str has a unique canonical representation
3231const symbol_to_canonical = CanonicalDict {Char} ()
33- const command_to_canonical = CanonicalDict {String} ()
3432
3533function canonical_expr (char:: Char )
3634 haskey (symbol_to_canonical, char) && return symbol_to_canonical[char]
3735 return TeXExpr (:char , char)
3836end
3937
40- canonical_expr (command:: String ) = get (command_to_canonical, command, nothing )
41-
42- # Symbols missing from the REPL completion data
43- latex_symbols[raw " \n eq" ] = " ≠"
44-
45- function get_symbol_char (command)
46- if ! haskey (latex_symbols, command)
47- @warn " unknown command $command "
38+ function get_symbol_char (com_str)
39+ if ! haskey (latex_symbols, com_str)
40+ @warn " unknown com_str $com_str "
4841 return ' ?'
4942 end
5043
51- return first (latex_symbols[command])
52- end
53-
54- # Numbers
55- for char in join (0 : 9 )
56- symbol_to_canonical[char] = TeXExpr (:digit , char)
44+ return first (latex_symbols[com_str])
5745end
5846
5947# #
60- # # Special commands
48+ # # Commands
6149# #
6250
63- command_to_canonical[raw " \f rac" ] = TeXExpr (:argument_gatherer , [:frac , 2 ])
64- command_to_canonical[raw " \s qrt" ] = TeXExpr (:argument_gatherer , [:sqrt , 1 ])
65- command_to_canonical[raw " \o verline" ] = TeXExpr (:argument_gatherer , [:overline , 1 ])
66- command_to_canonical[raw " \{ " ] = TeXExpr (:delimiter , ' {' )
67- command_to_canonical[raw " \} " ] = TeXExpr (:delimiter , ' }' )
51+ function command_expr (com_str, args)
52+ template = copy (command_definitions[com_str][1 ])
53+ return TeXExpr (template. head, vcat (template. args, args))
54+ end
55+ required_args (com_str) = command_definitions[com_str][2 ]
56+
57+ const command_definitions = Dict (
58+ raw " \f rac" => (TeXExpr (:frac ), 2 ),
59+ raw " \s qrt" => (TeXExpr (:sqrt ), 1 ),
60+ raw " \o verline" => (TeXExpr (:overline ), 1 ),
61+ raw " \{ " => (TeXExpr (:delimiter , ' {' ), 0 ),
62+ raw " \} " => (TeXExpr (:delimiter , ' }' ), 0 ),
63+ )
64+
65+ for func in underover_functions
66+ com_str = " \\ " * func
67+ template = TeXExpr (:underover , Any[TeXExpr (:function , func), nothing , nothing ])
68+ command_definitions[com_str] = (template, 0 )
69+ end
70+
71+ for func in generic_functions
72+ com_str = " \\ " * func
73+ command_definitions[com_str] = (TeXExpr (:function , func), 0 )
74+ end
75+
76+ for (com_str, width) in space_commands
77+ command_definitions[com_str] = (TeXExpr (:space , width), 0 )
78+ end
79+
80+ for com_str in combining_accents
81+ combining_char = get_symbol_char (com_str)
82+ template = TeXExpr (:combining_accent , TeXExpr (:symbol , combining_char))
83+ command_definitions[com_str] = (template, 1 )
84+ end
85+
86+ for name in font_names
87+ com_str = " \\ math$name "
88+ command_definitions[com_str] = (TeXExpr (:font , Symbol (name)), 1 )
89+ com_str = " \\ text$name "
90+ command_definitions[com_str] = (TeXExpr (:text , Symbol (name)), 1 )
91+ end
92+ command_definitions[" \\ text" ] = (TeXExpr (:text , :rm ), 1 )
6893
6994# #
70- # # Commands from the commands_data.jl file
95+ # # Symbols
7196# #
7297
98+ # Symbols missing from the REPL completion data
99+ latex_symbols[raw " \n eq" ] = " ≠"
100+
101+ # Numbers
102+ for char in join (0 : 9 )
103+ symbol_to_canonical[char] = TeXExpr (:digit , char)
104+ end
105+
73106for symbol in spaced_symbols
74107 symbol_expr = TeXExpr (:symbol , symbol)
75108 symbol_to_canonical[symbol] = TeXExpr (:spaced , symbol_expr)
76109end
77110
78111# Special case for hyphen that must be replaced by a minus sign
79- # TODO Make sure it is not replaced outside of math mode
112+ # TODO Make sure it is not replaced outside of math mode and when starting a group
80113symbol_to_canonical[' -' ] = TeXExpr (:spaced , TeXExpr (:symbol , ' −' ))
81114
82- for command in spaced_commands
83- symbol = get_symbol_char (command )
115+ for com_str in spaced_commands
116+ symbol = get_symbol_char (com_str )
84117 symbol_expr = TeXExpr (:symbol , symbol)
85- symbol_to_canonical[symbol] = command_to_canonical[command] = TeXExpr (:spaced , symbol_expr)
118+ template = TeXExpr (:spaced , symbol_expr)
119+ symbol_to_canonical[symbol] = template
120+ command_definitions[com_str] = (template, 0 )
86121end
87122
88- for command in underover_commands
89- symbol = get_symbol_char (command )
123+ for com_str in underover_commands
124+ symbol = get_symbol_char (com_str )
90125 symbol_expr = TeXExpr (:symbol , symbol)
91- symbol_to_canonical[symbol] = command_to_canonical[command] = TeXExpr (:underover , Any[symbol_expr, nothing , nothing ])
92- end
93-
94- for func in underover_functions
95- command = " \\ " * func
96- command_to_canonical[command] = TeXExpr (:underover , Any[TeXExpr (:function , func), nothing , nothing ])
126+ template = TeXExpr (:underover , Any[symbol_expr, nothing , nothing ])
127+ symbol_to_canonical[symbol] = template
128+ command_definitions[com_str] = (template, 0 )
97129end
98130
99- for command in integral_commands
100- symbol = get_symbol_char (command )
131+ for com_str in integral_commands
132+ symbol = get_symbol_char (com_str )
101133 symbol_expr = TeXExpr (:symbol , symbol)
102- symbol_to_canonical[symbol] = command_to_canonical[command] = TeXExpr (:integral , Any[symbol_expr, nothing , nothing ])
103- end
104-
105- for func in generic_functions
106- command = " \\ " * func
107- command_to_canonical[command] = TeXExpr (:function , func)
108- end
109-
110- for (command, width) in space_commands
111- command_to_canonical[command] = TeXExpr (:space , width)
134+ template = TeXExpr (:integral , Any[symbol_expr, nothing , nothing ])
135+ symbol_to_canonical[symbol] = template
136+ command_definitions[com_str] = (template, 0 )
112137end
113138
114139for (symbol, width) in space_symbols
115140 symbol_to_canonical[symbol] = TeXExpr (:space , width)
116141end
117142
118- for command in combining_accents
119- combining_char = get_symbol_char (command)
120- symbol_expr = TeXExpr (:symbol , combining_char)
121- command_to_canonical[command] = TeXExpr (:argument_gatherer , [:combining_accent , 2 , symbol_expr])
122- end
123-
124143for symbol in punctuation_symbols
125144 symbol = first (symbol)
126145 symbol_to_canonical[symbol] = TeXExpr (:punctuation , symbol)
@@ -131,30 +150,21 @@ for symbol in delimiter_symbols
131150 symbol_to_canonical[symbol] = TeXExpr (:delimiter , symbol)
132151end
133152
134- for name in font_names
135- command = " \\ math$name "
136- command_to_canonical[command] = TeXExpr (:argument_gatherer , [:font , 2 , Symbol (name)])
137- command = " \\ text$name "
138- command_to_canonical[command] = TeXExpr (:argument_gatherer , [:text , 2 , Symbol (name)])
139- end
140- command = " \\ text"
141- command_to_canonical[command] = TeXExpr (:argument_gatherer , [:text , 2 , :rm ])
142-
143153# #
144154# # Default behavior
145155# #
146156# We put it at the end to avoid overwritting existing commands
147157
148- for (command , symbol) in latex_symbols
158+ for (com_str , symbol) in latex_symbols
149159 symbol = first (symbol) # Convert String to Char
150- symbol_expr = TeXExpr (:symbol , [ symbol] )
160+ symbol_expr = TeXExpr (:symbol , symbol)
151161
152162 if ! haskey (symbol_to_canonical, symbol)
153163 symbol_to_canonical[symbol] = symbol_expr
154164 end
155165
156166 # Separate case for symbols that have multiple valid commands
157- if ! haskey (command_to_canonical, command )
158- command_to_canonical[command ] = symbol_expr
167+ if ! haskey (command_definitions, com_str )
168+ command_definitions[com_str ] = ( symbol_expr, 0 )
159169 end
160170end
0 commit comments