-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsymbol_parser.py
54 lines (47 loc) · 2.3 KB
/
symbol_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# coding=UTF-8
# Define a few special charecters in a dictonary
symbol_dictionary = {'&': '\\&',
'π': '\\pi ',
'α': '\\alpha ',
'': '\\epsilon ',
'φ': '\\phi ',
'θ': '\\theta ',
'ρ': '\\rho ',
'µ': '\\mu ',
'∆': '\\Delta ',
'ε': '\\varepsilon ',
'ϕ': '\\Phi ',
# For now, don't use these chars in mathmode...
#'æ': '\\textit{æ} ',
#'Æ': '\\textit{Æ} ',
#'ø': '\\textit{ø} ',
#'Ø': '\\textit{Ø} ',
#'å': '\\textit{å} ',
#'Å': '\\textit{Å} ',
'⇕': '\\Updownarrow ',
'⇔': '\\Leftrightarrow ',
'ω': '\\omega',
'Ω': '\\Omega',
'&': '\\&',
'γ' : '\\gamma',
'τ' : '\\tau',
'σ' : '\\sigma'} # ToDo: Add more of these...
def symbol_parser(str_with_symbols, mathmode):
"""Parses symbols into LaTeX symbols
:param str_with_symbols: A string which is the given symbol that needs to be checked
:return: Formatted LaTeX symbol or what was given
"""
if mathmode: # return the "pure" string
for unicode_symbol in symbol_dictionary: # Check entire dictionary
try: # Try to replace the given symbol
str_with_symbols = str_with_symbols.replace(unicode_symbol, symbol_dictionary[unicode_symbol])
except unicode_symbolError: # If it fails, just skip it
pass
return str_with_symbols # Return the parsed symbol
elif mathmode is False: # return the parsed string with $ $ to create a LaTeX math mode region
for unicode_symbol in symbol_dictionary: # Check entire dictionary
try: # Try to replace the given symbol
str_with_symbols = str_with_symbols.replace(unicode_symbol, "$" + symbol_dictionary[unicode_symbol] + "$")
except unicode_symbolError: # If it fails, just skip it
pass
return str_with_symbols # Return the parsed symbol