Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbconvert/filters/latex.py: 56%

9 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 06:10 +0000

1"""Latex filters. 

2 

3Module of useful filters for processing Latex within Jinja latex templates. 

4""" 

5# ----------------------------------------------------------------------------- 

6# Copyright (c) 2013, the IPython Development Team. 

7# 

8# Distributed under the terms of the Modified BSD License. 

9# 

10# The full license is in the file COPYING.txt, distributed with this software. 

11# ----------------------------------------------------------------------------- 

12 

13# ----------------------------------------------------------------------------- 

14# Imports 

15# ----------------------------------------------------------------------------- 

16import re 

17 

18# ----------------------------------------------------------------------------- 

19# Globals and constants 

20# ----------------------------------------------------------------------------- 

21 

22LATEX_RE_SUBS = ((re.compile(r"\.\.\.+"), r"{\\ldots}"),) 

23 

24# Latex substitutions for escaping latex. 

25# see: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates 

26 

27LATEX_SUBS = { 

28 "&": r"\&", 

29 "%": r"\%", 

30 "$": r"\$", 

31 "#": r"\#", 

32 "_": r"\_", 

33 "{": r"\{", 

34 "}": r"\}", 

35 "~": r"\textasciitilde{}", 

36 "^": r"\^{}", 

37 "\\": r"\textbackslash{}", 

38} 

39 

40 

41# ----------------------------------------------------------------------------- 

42# Functions 

43# ----------------------------------------------------------------------------- 

44 

45__all__ = ["escape_latex"] 

46 

47 

48def escape_latex(text): 

49 """ 

50 Escape characters that may conflict with latex. 

51 

52 Parameters 

53 ---------- 

54 text : str 

55 Text containing characters that may conflict with Latex 

56 """ 

57 text = "".join(LATEX_SUBS.get(c, c) for c in text) 

58 for pattern, replacement in LATEX_RE_SUBS: 

59 text = pattern.sub(replacement, text) 

60 

61 return text