2

I am using Vim to edit LaTeX documents. I know I can use :set linebreak to prevent Vim from breaking lines in the middle of a word, but this works globally.

Is there a way to have line breaking respect word boundaries on text, but not break wherever in code?

For example if I have a line:

The discussed algorithm is given in pseudo code in \Cref{alg_main_pseudocode}.

I don't mind the text being broken in the middle of the reference command, but I would prefer the text itself be unbroken.

Is something like this even possible? It seems like it might be, since Vim can obviously differentiate code from text (something like this works for spell check at least)

EDIT: for clarification, I want Vim to not visually break lines in the middle of words in text, but keep breaking them in the middle of words in code (as in LaTex code, stuff like equations, commands etc.). What I want to achieve is purely visual, only to change the way Vim displays the file.

Images to show what I want:

This is without :set linebreak. As you can see, the words are broken wherever the line happens to end. enter image description here

This is with :set linebreak. As you can see, the lines are broken on white spaces. enter image description here

I want something in between, for the text words to not be broken, and for the tex code to be broken. For example, (going by the first Image), I want the word system not to be broken, but the command \MotionEqs to be broken; and so on for the whole file. (Both the images show the same single line being displayed)

Purely visual aspect of the editor, without changing the file in question.

3
  • Can you use non breaking space in your pseudo code? Feb 7 at 14:29
  • Thanks for the example. I understood wrongly your demand. The solution I have posted was to not broken the text in the middle and not to broke the code anywhere even at space. Feb 8 at 9:19
  • Out of the box, I think you can only add some LaTeX special characters to the breakat option (like :set breakat+=\\${}) but this will not be aware of context and also look ugly. I'd take advantage of the fact that LaTeX mostly ignores whitespace and add a lot of newlines into the source. Makes it easier to read and easier to edit.
    – Friedrich
    Feb 8 at 10:20

1 Answer 1

0

I would use non breaking space instead of space in the pseudo code to avoid Vim breaking the pseudo code into chunk.

You can insert non breaking space using the NS digraph: Ctrl kNS.

The answer about How to enter tilde, back tick and special chars with a non-US keyboard? provides more advice how to easily insert special characters.

7
  • 1
    I'll raise the random concern that a LaTeX compiler with unicode support might keep non-breaking space in the resulting document which is most certainly not what one would want. A non-unicode compiler will probably stumble right away. I might try later this day.
    – Friedrich
    Feb 7 at 15:10
  • 1
    Good remark I had the same thought but if the solution is acceptable maybe the input should just be preprocessed right before compilation. Feb 7 at 15:12
  • 1
    Just confirmed that XeTeX will honor non-breaking spaces and produce a very long line in PDF output. I hate to say it, but: inserting invisible characters and stripping them in a preprocessing step is probably more complicated than formatting the source manually.
    – Friedrich
    Feb 7 at 21:42
  • Thanks for the experiment. That seems indeed very reasonable behavior. I would indeed not rely on a preprocessing. Either the code should be in one line in both source and output or it could be split in both source and output. But this is only my point of view :-) Feb 8 at 5:21
  • 2
    In that case I do not really understand the answer at all. I want the code snippets and commands to be broken, not the other way around. There is no spaces in them. How would a non-breaking space help? Also changing the resulting file is not something I would do for a little visual aid
    – aky-her
    Feb 8 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.