Skip to content

Commit

Permalink
Merge pull request #62 from r-stein/latex_inline_math
Browse files Browse the repository at this point in the history
[LaTeX] Added support for inline math ($...$) in a single line
  • Loading branch information
r-stein committed Mar 29, 2016
2 parents d9c00ab + fb2d274 commit 3b3d29b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
48 changes: 48 additions & 0 deletions latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,47 @@ def expand_against_surrounding_command(string, start, end):
"latex_command_surround")


def expand_to_inline_math(string, start, end):
# don't expand if a dollar sign is inside the string
if re.search(r"(?:[^\\]|^)\$", string[start:end]):
return

line = utils.get_line(string, start, end)
escape = inside = False
open_pos = close_pos = None
# we only need to consider one position, because we have checked it does
# not contain any $-signs
pos = start - line["start"]
for i, char in enumerate(string[line["start"]:line["end"]]):
# break if we are behind
behind = pos < i
if not inside and behind:
return
if escape:
escape = False
elif char == "\\":
escape = True
continue
elif char == "$":
if not inside:
# the inner end of the $-sign
open_pos = i + 1
elif behind:
close_pos = i
break
inside = not inside

if open_pos is not None and close_pos is not None:
open_pos = line["start"] + open_pos
close_pos = line["start"] + close_pos
# expand to the outer end
if open_pos == start and close_pos == end:
open_pos -= 1
close_pos += 1
return utils.create_return_obj(
open_pos, close_pos, string, "latex_inline_math")


# TODO could be moved to utils?
def _closest_result(result1, result2):
if result1 is None:
Expand Down Expand Up @@ -244,6 +285,13 @@ def expand(string, start, end):
result["expand_stack"] = expand_stack
return result

expand_stack.append("latex_inline_math")

result = expand_to_inline_math(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result

expand_stack.append("latex_environment_matching")

result = expand_against_matching_env(string, start, end)
Expand Down
22 changes: 22 additions & 0 deletions test/integration_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class LatexIntegrationTest(unittest.TestCase):
def setUpClass(self):
with open("test/snippets/latex_01.txt", "r") as myfile:
self.string1 = myfile.read()
with open("test/snippets/latex_02.txt", "r") as myfile:
self.string2 = myfile.read()

def test_expand_to_word1(self):
result = expand("\\section*{My Section}", 3, 3, "latex")
Expand Down Expand Up @@ -105,5 +107,25 @@ def test_expand_to_env6(self):
self.assertEqual(result["start"], 73)
self.assertEqual(result["end"], 311)

def test_expand_to_inline_math1(self):
result = expand(self.string2, 137, 137, "latex")
self.assertEqual(result["start"], 136)
self.assertEqual(result["end"], 139)

def test_expand_to_inline_math2(self):
result = expand(self.string2, 136, 139, "latex")
self.assertEqual(result["start"], 135)
self.assertEqual(result["end"], 139)

def test_expand_to_inline_math3(self):
result = expand(self.string2, 135, 139, "latex")
self.assertEqual(result["start"], 130)
self.assertEqual(result["end"], 145)

def test_expand_to_inline_math4(self):
result = expand(self.string2, 130, 145, "latex")
self.assertEqual(result["start"], 129)
self.assertEqual(result["end"], 146)

if __name__ == "__main__":
unittest.main()
12 changes: 12 additions & 0 deletions test/snippets/latex_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{xcolor}

\begin{document}

\section{My Section}

In the function $f(x, \phi) = 5$ we see....


\end{document}

0 comments on commit 3b3d29b

Please sign in to comment.