Skip to content

Commit

Permalink
Merge pull request tgingold-cern#23 from lorenzschmid/latex-equ
Browse files Browse the repository at this point in the history
Equations in LaTeX documentation
  • Loading branch information
tgingold-cern authored Oct 13, 2023
2 parents 74b4e8e + 9512efa commit 5de7449
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions proto/cheby/print_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,27 @@ def escape_printable(text):
# Remove whitespaces at start and end of string
text = text.strip()

# Escape special characters
SPECIAL_CHARS = ['\\', '&', '%', '$', '#', '_', '{', '}', '~', '^']
for char in SPECIAL_CHARS:
text = text.replace(char, '\\' + char)

# Allow line breaks after '_' and '.' in long strings
ALLOW_BREAK_CHARS = ['.', '_']
for char in ALLOW_BREAK_CHARS:
text = text.replace(char, char + '\\allowbreak{}')
# Escape text only if not between two `$` signs (i.e., an equation)
pattern = r'(?<!\\)\$'
text = re.split(pattern, text)

# Escape only chars in text parts, which are not an equation
for idx, text_part in enumerate(text):
if idx % 2 == 0:

# Escape special characters
SPECIAL_CHARS = ['\\', '&', '%', '#', '_', '{', '}', '~', '^'] # '$'
for char in SPECIAL_CHARS:
text_part = text_part.replace(char, '\\' + char)

# Allow line breaks after '_' and '.' in long strings
ALLOW_BREAK_CHARS = ['.', '_']
for char in ALLOW_BREAK_CHARS:
text_part = text_part.replace(char, char + '\\allowbreak{}')

text[idx] = text_part

text = '$'.join(text)

return text

Expand Down

0 comments on commit 5de7449

Please sign in to comment.