Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Sublime Text to ensure one sentence per line in LaTeX documents

The goal is to adopt the third way---a.k.a. using semantic linefeeds. This involves typing one sentence per line of a TeX file. This makes it easier to use version control systems when working with TeX files and other, text-heavy files. (A similar recommendation was made in answer to this question.)

Of course, I could manually insert a line break at the end of every sentence. However, this would lead to bad habits (e.g. typing a sentence per line when writing emails). So I'm looking for an alternative way of implementing this.

One solution, for those of us using Sublime Text, is to use ST's keybindings. Essentially, this would turn the spacebar into a keyboard shortcut for a script that will

  • check whether what precedes it is the end of a sentence, and
  • if it is, insert a line break.

Here's the relevant snippet:

{ "keys": [" "],
        "context":  [
                {"key": "selector", "operator": "equal", "operand": "text.tex.latex"},
                {"key": "preceding_text", "operator": "regex_match", "operand": "(^.+\\.$)|(^.+\\?$)|(^.+\\!$)|(^.+\\.+\\)$)"},
                {"key": "preceding_text", "operator": "not_regex_match", "operand": "(^.*\\\\ex\\.$)|(^.*\\\\[abz]\\.$)"},
                {"key": "preceding_text", "operator": "not_regex_match", "operand": "(^.*[Cc]f\\.$)|(^.*\\b[Pp]+\\.$)|(^.*etc\\.$)|(^.*\\b[A-Z]\\.$)|(^\\w\\.$)|(^.*M[sr]\\.$)|(^.*Mrs\\.$)"}
                ],
        "command": "insert", "args": {"characters": " %\n"}
    }

I append this to Sublime/Packages/User/Default (OSX).sublime-keymap and it seems to do the trick.

The snippet is supposed to look for what precedes the space, and give you a match if you have a string that ends with one of the following:

  • a period,
  • an exclamation mark,
  • a question mark,
  • a closing parenthesis preceded by a period.

It will however ignore strings that end with a period if they are preceded by one of the following:

  • '\ex', '\a', '\b', '\z' (this is to be able to use the linguex package),
  • 'cf', 'Cf',
  • 'p', 'pp', 'P',
  • 'etc'
  • 'Mr', 'Mrs', 'Ms', 'Dr',
  • Single capital letters (for use of initials in a sentence).

I'm curious to see if there are other things I should add to that list ('Ibid', I suppose would be one).

I'm also wondering whether there are any less clunky solutions to this problem.

like image 220
apc Avatar asked Jul 14 '15 13:07

apc


1 Answers

Unless one is using \frenchspacing (1, 2) there should never be a period . followed by space if it is not the end of a sentence. Latex makes a difference between periods at the end of a sentence and periods in sentences. Space after a sentence will be a larger-than-usual amount of space.

So, if you want to ensure one sentence per line you only need to act on periods followed by a space.

Any period that is not an end of a sentence should be followed by either:

  • \, small space
  • \ interword spacing (space after \)
  • ~ non-breaking space

Just for reference: Latex uses \@ to explicitly mark a period as end of sentence (intersentence spacing).

like image 86
rkta Avatar answered Sep 20 '22 05:09

rkta