ENH: add math mode to formatter escape="latex-math"
State: closed

- [x] closes #50040
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests).
 
Added `latex-math` mode to avoid escaping `$`.
We probably shouldn't escape`\[` and `\]`. They can not be applied between `\begin{tabular}` and `\end{tabular}`, which is used for both Series and DataFrames.

Labels:
name: Enhancement
name: IO LaTeX
description: to_latex
name: Styler
description: conditional formatting using DataFrame.style

Comments:
Hi @attack68, could you please review this PR?
Hi @attack68, I checked the example` $ \alpha = \frac{\beta}{\zeta^2} $`, and it didn’t work with the original fix. So I created more LaTeX expressions in math mode to understand how math-mode should work. My suggestion: there is no easy way to escape characters in math-mode. We can rely on user, he knows how to deal with  LaTeX expressions. 

For example: On one hand, the characters` {` and `}` are used in expressions to select logically a part of the expression. On the other hand, `\{` and `\}` are curly brackets. We cannot decide for the user what he meant. 

I added an example for math-mode and corrected LaTeX mode example.
I think we may be able to use regex to detect these. Im not brialliant at regex, but doing a search on stack overflow we can use look back and look forward, I think. These would be the rules I thiunk we aim for:

- all of the usual characters, except `$`, are escaped, **unless**:
- any of these charcaters are detected as being between a left side `$` and a right side `$`, in which case they form part of the equation and should not be escaped.
> I think we may be able to use regex to detect these. Im not brialliant at regex, but doing a search on stack overflow we can use look back and look forward, I think. These would be the rules I thiunk we aim for:
> 
>     * all of the usual characters, except `$`, are escaped, **unless**:
> 
>     * any of these charcaters are detected as being between a left side `$` and a right side `$`, in which case they form part of the equation and should not be escaped.

Thank you, I understand. I'll try to use regex. 
Hi @attack68. As you suggested I made a solution using regex to detect `math-mode`. It works well for expressions that are surrounded by the character `$`. For example: `"abc $first\$math$ middle $secondmath$"`

On the other hand, math expressions can be surrounded by the characters `\(` and `\).` 
For example: `" ab \(firstmath\) middle \( secondmath \) %(x)^ "`
Or even with some mistakes, like `" \)ab \(firstmath\) middle \( secondmath \) %(x)^ \)"`
For this case, I have an approach as well. 

I was thinking about updating the PR but I still have one question. Should we consider the case when we have an expression containing both math-mode types, `$` and `\(`` \)`? For example: `"abc \(firstmath\) middle $secondmath$"`
It doesn’t look easy to process such expression.
I think a basic PR just to handle and test the $ math mode case will be a good start. Once that is in place we could extend it in the future, and with the tests developed provides a reliable base from which to work
Hi @attack68. I added a basic implementation for math mode between `$` and a test.
Could you please take a look?
Ah I just noticed there is no addition to the `whatsnew`. You might to add a simple one line addition.
Thank you, I added a line to `whatsnew`.
Could you merge in main and resolve the merge conflict?
I merged main in and resolved the merge conflict.
I resolved a new merge conflict and did merge in main. Sorry, it took so long.
Is there anything else that is needed to be done for this PR?
@mroeschke can we still get this in to 2.0?
Thanks @natmokval 
Thank you for reviewing my PR. 
I thought it might be useful to add to latex mode a case when a math expression starts with `\(` and ends with `\)`. I'd like to do it and open a new PR.
I think you can use all of the following:
![image](https://user-images.githubusercontent.com/24256554/223120855-97e0bf87-abb5-4a62-944a-5ac13047deb0.png)

These are more difficult to cover I think. I'm glad we have this PR to get a basic latex-math mode in there but these will require extensive tests I think.
A new PR will probably have to be quite test driven I would expect.

I dont know which of these tend to display in Mathjax either, for notebook display.