Skip to content
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

Change TeX-style math delimiters to LaTeX-style #18

Open
leingang opened this issue Sep 3, 2024 · 9 comments
Open

Change TeX-style math delimiters to LaTeX-style #18

leingang opened this issue Sep 3, 2024 · 9 comments

Comments

@leingang
Copy link

leingang commented Sep 3, 2024

Congrats on the new project! I just took it for a spin.

In addition to indenting, I'm looking for a formatter that changes TeX-style math delimiters ($...$ and $$...$$) to the LaTeX style (\(...\) and \[...\]). I've used the LaTeX style for display math mode for decades, but experience has taught me to prefer it for inline math mode too.

I know a 100% perfect solution isn't possible with regular expressions only, so I'm hoping your project reads input files like TeX does natively. I've never done anything in Rust before so I can't tell from the source how it works.

See also:

@WGUNDERWOOD
Copy link
Owner

Thanks for this suggestion! It seems from the StackOverflow discussion that providing a good solution to this problem is difficult; tex-fmt is based on pattern matching and regex, so likely is unable (at least in its current form) to handle the edge cases described. If a robust solution can be formulated, I would be happy to incorporate it, probably with an optional flag.

@WGUNDERWOOD WGUNDERWOOD changed the title change TeX-style math delimiters to LaTeX-style Change TeX-style math delimiters to LaTeX-style Sep 3, 2024
@leingang
Copy link
Author

leingang commented Sep 4, 2024

Thanks, I'll keep my eyes open and my fingers crossed.

@lartpang
Copy link

lartpang commented Sep 19, 2024

@leingang
Perhaps manual replacement based on regular expressions is a more convenient strategy.

@PiotrSokol
Copy link

It's doable using regular expressions, though I only managed to get it to work with pcre2.

@WGUNDERWOOD
Copy link
Owner

@PiotrSokol would you be able to share your implementation of this? It could be a good starting point.

@PiotrSokol
Copy link

this doesn't cover all the corner cases, but should work with other regex flavors

(?'inline'(?<![\\\$])\$)((?:[^\$]|\\\$)+)(?&inline)

and

(?<display>\${2})((?:[^\$]|\\\$)+)((?&display))

@WGUNDERWOOD
Copy link
Owner

WGUNDERWOOD commented Oct 28, 2024

I'm going to start a list here of potential pitfalls for implementing such ideas.

  • Back-to-back inline math: $x=1$$y=2$
  • Inline or display math over multiple lines
  • Dollar signs: $\$10$
  • Text in equations: x=1 \text{and $y=2$}
  • TikZ uses dollars for coordinate calculations
  • Comments and verbatim environments should be skipped

See the math-delimiters branch for a test file.

@deivisCode
Copy link

Hi, I dont know how this is works behind the scenes but wanted to give some ideas (sorry if it makes no sense). Also, really thankfull for all your work, hope everyone here the best.

It seems like regex-matching arbitrary latex files is prone to fail in this scenario (see the pitfalls in previous comment from WGUNDERWOOD). I dont know if its just hard to implement or impossible at all, I never found a perfect solution so without technical background I bet the latter.

IDEA: run only from STDIN

I personally (just my preference) only run tex-fmt as a filter from nvim, using :<range>!tex-fmt --stdin --keep --tab 4, so I thought about this feature only applying when using the program with STDIN and not if you pass it a whole file.

The rationale is that running it only as a filter, over a limited, known text should be a lot safer, even if the regex is limited for whatever reason (classical example with html). So for the user, It sould be easier to see if the formatting missbehaves if its only done over a couple lines/paragraphs, compared to a whole file. For example, if the above pitfalls aren't solved, and the formatting messes up indentation, inserts whatever symbols, or any random fail, the user will inmediatly see the error and could just ctrl-z back. If trying to use it over a whole file it could give a warn like Caution: regex not reliable. Or maybe it should be only doable with a flag like --delimiters that depends on --stdin. Obviously, brave users could just use cat to format the whole file.

It isnt perfect because it doesnt solve the issue, just tries to lessen the errors. But could be usefull to some extent. At least for me it would. Thanks for your time!

@WGUNDERWOOD
Copy link
Owner

WGUNDERWOOD commented Dec 2, 2024

Thanks for these thoughts @deivisCode ! I agree that we should take care when implementing these "risky" regexes, and I like the suggestion of adding a flag to substitute delimiters. I would probably prefer to do this than to have it coupled to existing flags such as --stdin, as this risks obscuring the meaning of each option or causing unexpected behaviour. That way, you can, for example, pass the relevant flags in your neovim binding while maintaining the safer behavior on the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants