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)
| 51 | |
| 52 | |
| 53 | def 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 | |
| 85 | def texprint(before_summary, after_summary, bfgreedy, afgreedy): |