[Tex/LaTex] How to automatically remove space before punctuation and adding after sign

punctuationspacing

Sometimes when I write some text, I mistakingly put space before dot/comma or I forget to put space after dot/comma. I would like LaTeX to remove all space before dots and add space after punctuation.

For example:

"This is some text . This is some text"

should be

"This is some text. This is some text"

and

"This is some text.This is some text"

should be

"This is some text. This is some text"

I'm from Europe.

Best Answer

Ok, let's try a slacky trick.

Ingredients

For our next trick we will need:

  1. Sublime Text 2 editor installed (don't worry is multiplatform),
  2. A package (for this editor) called LaTeX Tools installed in turn by
  3. Package Control (A plugin or package that eases the installation of another packages).

Preparation

Once you have all installed following the instructions in each case, then you can use Sublime Text 2 as a LaTeX editor. I recommend, for convenience, use the color scheme called Monokai Bright (but you can experiment and use your favourite).

Open a single .tex file with Sublime Text 2 or load the directory of whole project (as you prefer). Once you have open your file, is easy search and replace what you need. Let's test:

  1. Press Ctrl + F
  2. In the bottom of the screen will appear a bar with the following options (from left to right):

    • Regular expressions (Alt+R)
    • Case sensitive (Alt+C)
    • Whole word (Alt+W)
    • Reverse direction
    • Wrap
    • In selection
    • Highlight matches

And a filed where you can input what are searching for. In our case will be . it means a dot with space before. Up here you can use the editor that you like for what I am suggesting. However Sublime Text 2 (and maybe emacs if you use AucTeX) has a very interesting feature that, in my opinion, deserves their use for this task.

Well, from the above list, I suggest enable the options:

  • Regular expresions (can be useful for this task),
  • Whole word (avoid to select ... by example if you have instead of \ldots) ,
  • Reverse direction (to review until the end of the document and return to the beginning, if you start seeking half of it) and highlight matches (the reason for select Monokai bright instead of Monokai).

Now press [Esc] before activate the options. Search by ona incidence of . and with the cursor of the mouse select carefully both, the space before and the dot. The final trick is to make multiple selections from the selected effect, press for that now Ctrl+D holding the keys.

If the selection was well done, then you can at the same time highlight the issues, even better, you'll have a real-time cursor in each and every one of the incidences detected.

This is important because it allows you to simultaneously edit all incidences, and correct all by removing the extra space at once (Use the arrow keys left and right direction to move the cursor to the beginning or end of the selection. Use it in this case left arrow key and press [Del]).

Is not exactly automatic, is not an script, but it works very well.

Remarks

Although there are programs and plugins to translate documents from Word to LaTeX and vice versa, I have not used any (almost all have licences and you must pay for them). Instead of that before I copied the text directly from Word and paste in Sublime Text 2 where I format it. Sometimes I prefer to open in AbiWord and export to LaTeX with a plugin included. But this plugin is almost experimental, and generates a lot of unnecessary code and not well placed some labels such as hierarchy ones (\part, \chapter, \section, etc.). To correct these imbalances do use tricks like that tricks like that just explained, that allow me to debug the code with relative ease.

I hope this long and overdetailed explanation will be helpful.

Related Question