MCPcopy Index your code
hub / github.com/evalplus/evalplus / align_ampersands

Function align_ampersands

tools/render.py:53–82  ·  view source on GitHub ↗

This function takes two strings containing various "&" characters and transforms them so that the indices of "&" are aligned. This is useful for formatting LaTeX tables. Args: str1 (str): First input string containing "&" characters. str2 (str): Second input string cont

(str1, str2)

Source from the content-addressed store, hash-verified

51
52
53def align_ampersands(str1, str2):
54 """
55 This function takes two strings containing various "&" characters and transforms them so that the indices of "&"
56 are aligned. This is useful for formatting LaTeX tables.
57
58 Args:
59 str1 (str): First input string containing "&" characters.
60 str2 (str): Second input string containing "&" characters.
61
62 Returns:
63 Tuple[str, str]: Two transformed strings with aligned "&" indices.
64 """
65 # Find indices of "&" characters in both strings
66 amp_idx1 = [i for i, char in enumerate(str1) if char == "&"]
67 amp_idx2 = [i for i, char in enumerate(str2) if char == "&"]
68
69 assert len(amp_idx1) == len(amp_idx2)
70
71 # Replace "&" characters in the strings with "\&" at the aligned indices
72 acc1, acc2 = 0, 0
73 for i, j in zip(amp_idx1, amp_idx2):
74 diff = (j + acc2) - (i + acc1)
75 if diff > 0:
76 str1 = str1[: i + acc1] + " " * diff + str1[i + acc1 :]
77 acc1 += diff
78 elif diff < 0:
79 str2 = str2[: j + acc2] + " " * (-diff) + str2[j + acc2 :]
80 acc2 -= diff
81
82 return str1, str2
83
84
85def texprint(before_summary, after_summary, bfgreedy, afgreedy):

Callers 1

texprintFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected