Skip to content

Commit a3b1651

Browse files
authored
Merge pull request #24 from nschloe/si-percentage
siunitx percentage
2 parents e517a8d + 54a8cbd commit a3b1651

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

blacktex/main.py

+7
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ def _add_spaces_around_equality_sign(string):
308308
return string
309309

310310

311+
def _si_percentage(string):
312+
# match float like https://stackoverflow.com/a/12643073/353337
313+
string = re.sub(r"([+-]?([0-9]*[.])?[0-9]+)[ \t]*\\%", r"\\SI{\1}{\%}", string)
314+
return string
315+
316+
311317
def clean(string, keep_comments=False):
312318
out = string
313319
out = _remove_trailing_whitespace(out)
@@ -324,6 +330,7 @@ def clean(string, keep_comments=False):
324330
out = _replace_double_nbsp(out)
325331
out = _replace_nbsp_space(out)
326332
out = _replace_over(out)
333+
out = _si_percentage(out)
327334
out = _add_linebreak_after_double_backslash(out)
328335
out = _add_backslash_for_keywords(out)
329336
out = _add_curly_brackets_around_round_brackets_with_exponent(out)

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = blacktex
3-
version = 0.3.4
3+
version = 0.3.5
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Cleans up your LaTeX files

test/test_blacktex.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ def test_comment_last():
5858
assert out == ref, f"{repr(out)} != {repr(ref)}"
5959

6060

61-
def test_escapted_percentage_sign():
62-
input_string = "25\\% gain"
63-
out = blacktex.clean(input_string)
64-
assert out == "25\\% gain"
65-
66-
6761
def test_trailing_whitespace():
6862
input_string = "lorem \n sit amet"
6963
out = blacktex.clean(input_string)
@@ -334,5 +328,17 @@ def test_space_around_operators():
334328
assert out == "a+b &=& c"
335329

336330

331+
def test_si_percentage():
332+
input_string = "20\\% \\SI{30}{\\%}"
333+
out = blacktex.clean(input_string)
334+
assert out == "\\SI{20}{\\%} \\SI{30}{\\%}"
335+
336+
337+
def test_escaped_percentage_sign():
338+
input_string = "25\\% gain"
339+
out = blacktex.clean(input_string)
340+
assert out == "\\SI{25}{\\%} gain"
341+
342+
337343
if __name__ == "__main__":
338-
test_comment_last()
344+
test_si_percentage()

0 commit comments

Comments
 (0)