-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: add LaTeX math mode with parentheses #51903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mroeschke merged 14 commits into pandas-dev:main from natmokval:50040-add-math-mode-formatter-escape=latex-part2 Mar 31, 2023
Merged
Changes from 2 commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
ba72f26 ENH: add math mode with parentheses
natmokval 0801f6a Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval 2320fb3 ENH: add math mode with parentheses II
natmokval c20e795 Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval 2e283a7 Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval 29e357c ENH: add math mode with parentheses III
natmokval 849bf36 Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval 9be5c22 ENH: add an example to latex-math mode and a line to whatsnew
natmokval 7b4b53a Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval 25e6b1f ENH: update docs for Styler: add description latex-math mode to escape
natmokval ed27582 improve code style
natmokval 3daf4d5 add an example to test and correct _escape_latex_math
natmokval 78dcdf8 Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval c06695e Merge branch 'main' into 50040-add-math-mode-formatter-escape=latex-p…
natmokval File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -1117,7 +1117,8 @@ def format( | |
| 2 & \textbf{\$\%\#} \\ | ||
| \end{tabular} | ||
| | ||
| Using ``escape`` in 'latex-math' mode. | ||
| Applying ``escape`` in 'latex-math' mode. In the example below | ||
| we enter math mode using the charackter ``$``. | ||
| | ||
| >>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \ | ||
| ... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]]) | ||
| | @@ -1129,6 +1130,20 @@ def format( | |
| 1 & \%\#\textasciicircum \space $ \$x^2 $ \\ | ||
| \end{tabular} | ||
| | ||
| We can use the charackter ``\(`` to enter math mode and the charackter ``\)`` | ||
natmokval marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| to close math mode. | ||
| | ||
| >>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \ | ||
| ... = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]]) | ||
| >>> df.style.format(escape="latex-math").to_latex() | ||
| ... # doctest: +SKIP | ||
| \begin{tabular}{ll} | ||
| & 0 \\ | ||
| 0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha | ||
| = \frac{\beta}{\zeta^2}\) \\ | ||
| 1 & \%\#\textasciicircum \space \( \$x^2 \) \\ | ||
| \end{tabular} | ||
| | ||
| Pandas defines a `number-format` pseudo CSS attribute instead of the `.format` | ||
| method to create `to_excel` permissible formatting. Note that semi-colons are | ||
| CSS protected characters but used as separators in Excel's format string. | ||
| | @@ -2357,17 +2372,20 @@ def _escape_latex(s): | |
| .replace("~", "\\textasciitilde ") | ||
| .replace("^ ", "^\\space ") # since \textasciicircum gobbles spaces | ||
| .replace("^", "\\textasciicircum ") | ||
| .replace("ab2§=§8yz(", "\\( ") | ||
| .replace("ab2§=§8yz)", "\\) ") | ||
| .replace("ab2§=§8yz", "\\textbackslash ") | ||
| ) | ||
| | ||
| | ||
| def _escape_latex_math(s): | ||
| r""" | ||
| All characters between two characters ``$`` are preserved. | ||
| All characters in LaTeX math mode are preserved. | ||
| | ||
| The substrings in LaTeX math mode, which start with the character ``$`` | ||
| and end with ``$``, are preserved without escaping. Otherwise | ||
| regular LaTeX escaping applies. See ``_escape_latex()``. | ||
| The substrings in LaTeX math mode, which either are surrounded | ||
| by two characters ``$`` or start with the character ``\(`` and end with ``\)``, | ||
| are preserved without escaping. Otherwise regular LaTeX escaping applies. | ||
| See ``_escape_latex()``. | ||
| ||
| | ||
| Parameters | ||
| ---------- | ||
| | @@ -2379,16 +2397,37 @@ def _escape_latex_math(s): | |
| str : | ||
| Escaped string | ||
| """ | ||
| s = s.replace(r"\$", r"rt8§=§7wz") | ||
| pattern = re.compile(r"\$.*?\$") | ||
| pos = 0 | ||
| ps = pattern.search(s, pos) | ||
| res = [] | ||
| while ps: | ||
| res.append(_escape_latex(s[pos : ps.span()[0]])) | ||
| res.append(ps.group()) | ||
| pos = ps.span()[1] | ||
| | ||
| def _math_mode_with_dollar(s): | ||
attack68 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| s = s.replace(r"\$", r"rt8§=§7wz") | ||
| pattern = re.compile(r"\$.*?\$") | ||
| pos = 0 | ||
| ps = pattern.search(s, pos) | ||
| res = [] | ||
| while ps: | ||
| res.append(_escape_latex(s[pos : ps.span()[0]])) | ||
| res.append(ps.group()) | ||
| pos = ps.span()[1] | ||
| ps = pattern.search(s, pos) | ||
| | ||
| res.append(_escape_latex(s[pos : len(s)])) | ||
| return "".join(res).replace(r"rt8§=§7wz", r"\$") | ||
| | ||
| def _math_mode_with_parentheses(s): | ||
| s = s.replace(r"\(", r"LEFT§=§6yzLEFT").replace(r"\)", r"RIGHTab5§=§RIGHT") | ||
| res = [] | ||
| for item in re.split(r"LEFT§=§6yz|ab5§=§RIGHT", s): | ||
| if item.startswith("LEFT") and item.endswith("RIGHT"): | ||
| res.append(item.replace("LEFT", r"\(").replace("RIGHT", r"\)")) | ||
| else: | ||
| res.append( | ||
| _escape_latex(item).replace("LEFT", r"\(").replace("RIGHT", r"\)") | ||
| ) | ||
| return "".join(res) | ||
| | ||
| res.append(_escape_latex(s[pos : len(s)])) | ||
| return "".join(res).replace(r"rt8§=§7wz", r"\$") | ||
| if s.replace(r"\$", "ab").find(r"$") > -1: | ||
| return _math_mode_with_dollar(s) | ||
| elif s.find(r"\(") > -1: | ||
| return _math_mode_with_parentheses(s) | ||
| else: | ||
| return _escape_latex(s) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.