Skip to content

Commit

Permalink
Merge pull request #182 from torik42/Issue-181
Browse files Browse the repository at this point in the history
Issue 181
  • Loading branch information
matze-dd authored Feb 1, 2021
2 parents 576d214 + b537d09 commit 7abc725
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Work in progress
----------------
- LaTeX macros / environments
- builtins
- fixed \\hspace, only add space if argument is not of zero length;
**thanks to @torik42** (PR [#182](../../pull/182))
- added CONTRIBUTING.md (issue [#167](../../issues/167))

Version 1.3.0 (2021/01/31)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_packages/test_latex_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def test_macros_latex(latex, plain_expected):
(r'A\hphantom{\label{l}}B', 'AB'),
(r'A\hspace{1em}B', 'A B'),
(r'A\hspace*{1em}B', 'A B'),
(r'A\hspace{0cm}B', 'AB'),
(r'A\hspace{ .0cm}B', 'AB'),
(r'A\hspace{ ,5cm}B', 'A B'),
(r'A\hspace{0.2pt}B', 'A B'),
(r'A\hspace{0,2cm}B', 'A B'),
(r'A\hspace{1.2,3cm}B', 'A B'),
(r'A\newcommand{\xxx}{X}B', 'AB'),
(r'A\newcommand*{\xxx}[1][x]{X}B', 'AB'),
(r'A\newtheorem{xxx}{XYZ} B', 'AB'),
Expand Down
15 changes: 14 additions & 1 deletion yalafi/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from . import defs
from . import utils
import re

# macros \newcommand, \renewcommand
#
Expand Down Expand Up @@ -67,7 +68,7 @@ def handler (parser, buf, mac, args, delim, pos):
out.append(defs.TextToken(pos, '.', pos_fix=True))
out.append(defs.SpaceToken(pos, '\n', pos_fix=True))
return out

# this creates a closure
return handler

Expand Down Expand Up @@ -100,6 +101,18 @@ def h_phantom(parser, buf, mac, args, delim, pos):
return [defs.SpecialToken(pos, '\\;')]
return []

# \hspace
# at least, we detect lentghs that are explicitely zero
#
numbers = re.compile(r'\s*(\d+[.,]?\d*|[.,]\d+)\D')

def h_hspace(parser, buf, mac, args, delim, pos):
arg = parser.get_text_expanded(args[1])
match = numbers.match(arg)
if match and float(match.group(1).replace(',', '.')) == 0:
return []
return [defs.TextToken(pos, ' ')]

# macro \cite[opt]
#
def h_cite(parser, buf, mac, args, delim, pos):
Expand Down
2 changes: 1 addition & 1 deletion yalafi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def init_macros(self):
Macro(self, '\\footnotetext', args='OA', extract='#2'),
Macro(self, '\\framebox', args='OOA', repl='#3'),
Macro(self, '\\hphantom', args='A', repl=hs.h_phantom),
Macro(self, '\\hspace', args='*A', repl=' '),
Macro(self, '\\hspace', args='*A', repl=hs.h_hspace),
Macro(self, '\\newcommand', args='*AOOA', repl=hs.h_newcommand),
Macro(self, '\\newtheorem', args='AOAO', repl=hs.h_newtheorem),
Macro(self, '\\part', args='*OA', repl=hs.h_heading),
Expand Down

0 comments on commit 7abc725

Please sign in to comment.