Skip to content

Commit

Permalink
Added \eqref support
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed May 7, 2023
1 parent 7965227 commit 51e4698
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
16 changes: 13 additions & 3 deletions pydetex/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
_TAG_BRACE_CLOSE = '⇱BRACE_CLOSE⇲'
_TAG_BRACE_OPEN = '⇱BRACE_OPEN⇲'
_TAG_CLOSE_CITE = '⇱CLOSE_CITE⇲'
_TAG_CLOSE_CITE_EQN = '⇱CLOSE_CITE_EQN⇲'
_TAG_DOLLAR_SYMBOL = '⇱DOLLAR_SYMBOL⇲'
_TAG_FILE_ERROR = '⇱FILE_ERROR⇲'
_TAG_ITEM_SPACE = '⇱ITEM_SPACE⇲'
_TAG_NEW_LINE = '⇱NEW_LINE⇲'
_TAG_OPEN_CITE = '⇱OPEN_CITE⇲'
_TAG_OPEN_CITE_EQN = '⇱OPEN_CITE_EQN⇲'
_TAG_PERCENTAGE_SYMBOL = '⇱COMMENT_PERCENTAGE_SYMBOL⇲'

# Others
Expand Down Expand Up @@ -246,7 +248,10 @@ def process_cite(
"""
assert isinstance(cite_separator, str)
cites = {}
look = ['\\cite*{', '\\citet*{', '\\citep*{', '\\cite{', '\\citet{', '\\citep{', '\\newcite{', '\\newcite*{']
look = ['\\cite*{', '\\citet*{', '\\citep*{', '\\cite{', '\\citet{', '\\citep{',
'\\newcite{', '\\newcite*{']
look_eqn = ['\\eqref{']
look += look_eqn
k = -1
while True:
run_j = ''
Expand Down Expand Up @@ -307,8 +312,11 @@ def process_cite(
new_cites.append(str(w))

c = cite_separator.join(new_cites)
s = s[:k] + FONT_FORMAT_SETTINGS['cite'] + _TAG_OPEN_CITE + c + \
_TAG_CLOSE_CITE + FONT_FORMAT_SETTINGS['normal'] + s[k + j + 1:]
eqn_mode = run_j in look_eqn
open_cite = _TAG_OPEN_CITE if not eqn_mode else _TAG_OPEN_CITE_EQN
close_cite = _TAG_CLOSE_CITE if not eqn_mode else _TAG_CLOSE_CITE_EQN
s = s[:k] + FONT_FORMAT_SETTINGS['cite'] + open_cite + c + \
close_cite + FONT_FORMAT_SETTINGS['normal'] + s[k + j + 1:]
break


Expand Down Expand Up @@ -368,7 +376,9 @@ def replace_pydetex_tags(
"""
assert len(cite_format) == 2
s = s.replace(_TAG_OPEN_CITE, (cite_format[0]))
s = s.replace(_TAG_OPEN_CITE_EQN, '(')
s = s.replace(_TAG_CLOSE_CITE, (cite_format[1]))
s = s.replace(_TAG_CLOSE_CITE_EQN, ')')
s = s.replace(_TAG_ITEM_SPACE, ' ')
s = s.replace(_TAG_PERCENTAGE_SYMBOL, '%')
s = s.replace(_TAG_BRACE_OPEN, '{')
Expand Down
2 changes: 1 addition & 1 deletion pydetex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def split_tags(s: str, tags: List[str]) -> List[Tuple[str, str]]:
[TAG1]new line[TAG2]this is[TAG1]very epic
Output:
[('TAG1', 'newline'), ('TAG', 'this is), ('TAG1', 'very epic')]
[('TAG1', 'newline'), ('TAG', 'this is), ('TAG1', 'very epic') ... ]
:param s: String
:param tags: Tag list
Expand Down
5 changes: 5 additions & 0 deletions test/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def test_process_cite(self) -> None:
self.assertEqual(
par.replace_pydetex_tags(par.process_cite(s)),
'This is another example, [1] et al. suggests that yes, but [2] not')
# Test equation cite
s = 'Here, we test an equation with \\eqref{mycite}'
self.assertEqual(
par.replace_pydetex_tags(par.process_cite(s)),
'Here, we test an equation with (1)')
# Test multiple cites
s = 'This is an example \\cite{b} \\cite{a, b, c , d, e}'
self.assertEqual(par.replace_pydetex_tags(par.process_cite(s)), 'This is an example [1] [1-5]')
Expand Down

0 comments on commit 51e4698

Please sign in to comment.