ehuss / Sublime-Wrap-Plus

Enhanced "wrap lines" command for Sublime Text 2 or 3.
MIT License
195 stars 37 forks source link

Latex comments broken by wrap #31

Open mpf opened 10 years ago

mpf commented 10 years ago

This line

This is a test. This is a test. This is a test. This is a test. % This is a comment

gets wrapped to this

This is a test. This is a test. This is a test. This is a test. % This is a
comment

and now the word "comment" is no longer a comment. Ideally, the wrap would result in

This is a test. This is a test. This is a test. This is a test. % This is a
% comment
ph03 commented 10 years ago

I can confirm this annoying behavior and would also love to see this corrected. Maybe it is related to my issue / workaround #27.

rosshemsley commented 10 years ago

Also the following can happen:

% Must not omit this very important statement: 
Very important statement

becomes

% Must not omit this very important statement:  Very important statement

(and is thus commented out...) I actually did exactly this to a scientific paper without realising.. Thankfully a co-author noticed before we submitted...

Also another example that can be a major major hassle to fix if you don't realise until later:

We refer the reader to the proof in Lemma~\ref{lem:FLT}
% Here is some text
% that I have decided
% to remove.
the proof given is particularly elegant because of reasons X,Y,Z...

becomes

We refer the reader to the proof in Lemma~\ref{lem:FLT} % Here is some text %
that I have decided % to remove. the proof given is particularly elegant because
of reasons X,Y,Z...
ph03 commented 10 years ago

@rosshemsley This is another problem that is fixed in my workaround #27, but the pull requiest doesn't seems to be interresting for the maintainer. As you describe it, I also think that this can lead to serious problems when typesetting latex.

ehuss commented 10 years ago

I'll take a look at it soon. I don't use LaTeX, so it hasn't been a priority for me. I think it will need to distinguish the boundaries between comments and non-comments. Right now, it assumes comments are on the entire line, and also doesn't even look for comments if you initiate the command outside of a comment scope.

rosshemsley commented 10 years ago

If you do find yourself looking to support LaTeX officially, maybe I can give a couple of useful comments:

In LaTeX, whitespace has meaning, so if you write:

sometext here
% comment
sometext here.

you get different output if you write:

sometext here

% comment

some text here.

Which means the comments often end up being next to the text, and so when wrapping a block (which, by the way, is an incredibly useful thing to be able to do to LaTeX!), the comments are currently wrapped too (so the % operation is placed at random in the text, creating junk). Since the blocks of text can be very long, it's easy to not realise that you've done this.

Also, I spotted that there was a rule for hyphens. In LaTex, the default should probably be to leave them alone, since LaTeX renders "hello-there" differently to "hello- there".

Finally: equations and commands. It would be great to have a couple of rules for equations and commands: For example, automatically put them on their own lines? wrap them? That would be super useful:

This is some latex text and it has a lot of text here, and an equation: $x+\sqrt{\frac{2/ \pi}$ some
more text.

->

This is some latex text and it has a lot of text here, and an equation: 
$x+\sqrt{\frac{2/ \pi}$
some more text.

Just some thoughts.

Anyway, when I'm more free I might be interested in helping out if I get the time (I use this package with Latex every day - mostly because I work with mathematicians and they have this horrible habit of writing entire files on a single line... ;) ).

Ross

ph03 commented 10 years ago

@rosshemsley Nice ideas! The block-wise comment distinguishing already works for me with the workaround, but I guess a better latex parsing would also make other applications possible. For the hyphenation you can already add - to the sublime "word_separators" option variable to don't wrap on -.

Regarding equations, I think we shouldn't change something on $ $ equations, as they are explicitly defined to be inline (or at least make this optional / configurable in the settings, I would personally opt for a default off here ^^). Currently, the strategy is to not change lines that start with \, which comprises most of the latex math markup and already works fine for me. Oftentimes, for readability, I set equations that are longer than the margin, but actually I don't want them to be wrapped as this would destroy the tedious manual aligning :)

I would also like to volunteer for testing possible LaTeX enhancements.

ehuss commented 10 years ago

I pushed a change that should help with some of the more fatal problems. Let me know if that helps. There are still quite a few situations it may have trouble with.

If there is a comment at the end of a line, for the most part it will ignore it. I was playing around with AUCTeX for emacs, which does a pretty good job of wrapping/formatting latex, and that's what it does.

Having language-specific parsing/recognition would be nice. That could lead to a wide range of beautifying possibilities.

rosshemsley commented 10 years ago

@ehuss Great, I'll take a look. @ph03 There might be some nice middle-ground type behaviour, worth thinking about though.

I wonder if the irony has escaped anyone that parsing and formatting latex to make it more readable is exactly the problem that LaTeX was created to solve in the first place.

ph03 commented 10 years ago

@ehuss That changes work good for most of my usual use cases. However, in this case

We refer the reader to the proof in Lemma~\ref{lem:FLT} % Here is some text that I have decided to remove.
the proof given is particularly elegant because of reasons X,Y,Z...

is wrongly modified to

We refer the reader to the proof in Lemma~\ref{lem:FLT} % Here is some text that
I have decided to remove. the proof given is particularly elegant because of
reasons X,Y,Z...
ehuss commented 10 years ago

Where on the line was the cursor when you executed the wrap command? If it was inside the comment, then I am aware of that. That particular case is somewhat more difficult to address, partially because there are different ways to approach it. AUCTeX which I mentioned before will wrap it justified like:

We refer the reader to the proof in Lemma~\ref{lem:FLT} % Here is some text
                                                        % that I have decided 
                                                        % to remove.
the proof given is particularly elegant because of reasons X,Y,Z...

Alternatively it could wrap it left-flushed like:

We refer the reader to the proof in Lemma~\ref{lem:FLT} % Here is some text
% that I have decided to remove.
the proof given is particularly elegant because of reasons X,Y,Z...

Neither approach seems particularly great to me. I guess that could be an option?

If you didn't execute inside the comment, then make sure you have version 2014.05.12.07.17.37. I tested your text, and trying to wrap anywhere outside the comment should leave the comment untouched.

ph03 commented 10 years ago

Indeed, I executed it inside of the comment. Executing the script in the regular text works fine. Either option is fine for me. Could it be possible to bring all trailing comments into individual directly attached lines, like

We refer the reader to the proof in Lemma~\ref{lem:FLT}
% Here is some text that I have decided to remove.
the proof given is particularly elegant because of reasons X,Y,Z...