##// END OF EJS Templates
update LATEX_SUBS table
MinRK -
Show More
@@ -1,114 +1,116
1 1 """Latex filters.
2 2
3 3 Module of useful filters for processing Latex within Jinja latex templates.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 import re
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Globals and constants
20 20 #-----------------------------------------------------------------------------
21 21
22 22 # Latex substitutions for escaping latex.
23 23 # see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
24
24 25 LATEX_SUBS = {
25 26 '&': r'\&',
26 '%': r'\%',
27 '$': r'\$',
28 '#': r'\#',
29 '_': r'\letterunderscore{}',
30 '{': r'\letteropenbrace{}',
31 '}': r'\letterclosebrace{}',
32 '~': r'\lettertilde{}',
33 '^': r'\letterhat{}',
34 '\\': r'\letterbackslash{}'}
27 '%': r'\%',
28 '$': r'\$',
29 '#': r'\#',
30 '_': r'\_',
31 '{': r'\{',
32 '}': r'\}',
33 '~': r'\textasciitilde{}',
34 '^': r'\^{}',
35 '\\': r'\textbackslash{}',
36 }
35 37
36 38
37 39 #-----------------------------------------------------------------------------
38 40 # Functions
39 41 #-----------------------------------------------------------------------------
40 42
41 43 __all__ = ['escape_latex',
42 44 'strip_math_space']
43 45
44 46 def escape_latex(text):
45 47 """
46 48 Escape characters that may conflict with latex.
47 49
48 50 Parameters
49 51 ----------
50 52 text : str
51 53 Text containing characters that may conflict with Latex
52 54 """
53 55
54 56 return ''.join([LATEX_SUBS.get(c, c) for c in text])
55 57
56 58
57 59 def strip_math_space(text):
58 60 """
59 61 Remove the space between latex math commands and enclosing $ symbols.
60 62 This filter is important because latex isn't as flexible as the notebook
61 63 front end when it comes to flagging math using ampersand symbols.
62 64
63 65 Parameters
64 66 ----------
65 67 text : str
66 68 Text to filter.
67 69 """
68 70
69 71 # First, scan through the markdown looking for $. If
70 72 # a $ symbol is found, without a preceding \, assume
71 73 # it is the start of a math block. UNLESS that $ is
72 74 # not followed by another within two math_lines.
73 75 math_regions = []
74 76 math_lines = 0
75 77 within_math = False
76 78 math_start_index = 0
77 79 ptext = ''
78 80 last_character = ""
79 81 skip = False
80 82 for index, char in enumerate(text):
81 83
82 84 #Make sure the character isn't preceeded by a backslash
83 85 if (char == "$" and last_character != "\\"):
84 86
85 87 # Close the math region if this is an ending $
86 88 if within_math:
87 89 within_math = False
88 90 skip = True
89 91 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
90 92 math_regions.append([math_start_index, index+1])
91 93 else:
92 94
93 95 # Start a new math region
94 96 within_math = True
95 97 math_start_index = index
96 98 math_lines = 0
97 99
98 100 # If we are in a math region, count the number of lines parsed.
99 101 # Cancel the math region if we find two line breaks!
100 102 elif char == "\n":
101 103 if within_math:
102 104 math_lines += 1
103 105 if math_lines > 1:
104 106 within_math = False
105 107 ptext = ptext+text[math_start_index:index]
106 108
107 109 # Remember the last character so we can easily watch
108 110 # for backslashes
109 111 last_character = char
110 112 if not within_math and not skip:
111 113 ptext = ptext+char
112 114 if skip:
113 115 skip = False
114 116 return ptext
General Comments 0
You need to be logged in to leave comments. Login now