diff --git a/latex.py b/latex.py index cc6ec44..35cc848 100644 --- a/latex.py +++ b/latex.py @@ -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: @@ -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) diff --git a/test/integration_latex.py b/test/integration_latex.py index aa950f5..680fa3d 100644 --- a/test/integration_latex.py +++ b/test/integration_latex.py @@ -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") @@ -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() diff --git a/test/snippets/latex_02.txt b/test/snippets/latex_02.txt new file mode 100644 index 0000000..ed4a65b --- /dev/null +++ b/test/snippets/latex_02.txt @@ -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} \ No newline at end of file