28
votes

In (La)TeX non-breaking spaces are called ties and they are made by ~. Ties are for instance used after abbreviations so that there is no line break directly after them and so that dots ending abbrevations are not treated as ending sentences. The latter use is important because standardly LaTeX puts a longer space after dots than between words.

When exporting from Org-mode to LaTeX ~ is treated as an explicit character and not as a tie. Ignoring the use of non-breaking spaces is not an alternative because it leads to the wrong spacing (see the second sentence in the example above). One alternative is to force Org-mode to treat ~ as LaTeX with #+BEGIN_LaTeX ~ #+END_LaTeX but it is verbose and export to other formats breaks. Finally, using UTF-8 non-breaking spaces, as suggested in http://comments.gmane.org/gmane.emacs.orgmode/24716, does not work because LaTeX does not treat it as a space. So, how can I use non-breaking spaces in Org-mode that are properly exported to LaTeX?

Here is an example to clarify. The first sentence fails because ~ is treated as an explicit character. The second sentence fails, obviously, because the last dot is treated as ending a sentence by LaTeX. The third sentence exports properly but it is verbose and breaks export to other formats. The fourth line (separated by an UTF-8 non-breaking space which is inserted by C-x 8 Space) fails because it is not treated as a space by LaTeX:

#+title:Title

e.g.~example

e.g. example

#+BEGIN_LaTeX
e.g.~example
#+END_LaTeX

e.g. example

This exports (C-x C-e L) to the following LaTeX code:

e.g.\~{}example

e.g. example

e.g.~example

e.g. example

Which renders as:

LaTeX rendered

I am running Org-mode 7.6 in Emacs 23.3.1.

4
Belongs on TeX.SE.Fred Foo
@larsmans I disagree. This is not primarily about LaTeX but about Org-mode which is an Emacs mode. It is a question about how to use Org-mode which may involve how to properly configure it via elisp code.N.N.

4 Answers

25
votes

On http://orgmode.org/manual/Special-symbols.html I found the solution to the double spacing problem:

Org

e.g.\nbsp{}example

LaTeX

e.g.~example
11
votes

All the other answers work well, however I would like to mention org-entities for the sake of completeness. More so, because you can leverage this mechanism to do more generic export customisations, as well as support multiple backends.

Set org-entities to something like this:

(setq org-entities-user
      '(("space" "~" nil " " " " " " " ")))

The following Org source:

E.g.\space{}a, b, c

exports to LaTeX as:

E.g.~a, b, c

and exports to HTML as:

<p>
E.g.&nbsp;a, b, c</p>
7
votes

The answer is hinted at within the gmane thread you linked. In particular the last reply says to use \nbsp as your non-breaking space. By itself this won't work since you need a space after the \nbsp for it to recognize it as a command and not just part of your text.

However the following works (adding to your list of attempts:

#+title:Title

* Test
1) e.g.~example

2) e.g. example

3) 
  #+BEGIN_LaTeX
    e.g.~example
  #+END_LaTeX

4) e.g. example

5) e.g.\nbsp example
   (non-breaking \240 space between \nbsp and example)

Which outputs to LaTeX as:

\item e.g.\~{}example
\item e.g. example
\item e.g.~example
\item e.g. example
\item e.g.~ example %\240 non-breaking between ~ and example

And this renders as:

Rendered


EDIT

I just retested my results, somehow my non-breaking spaces had not exported properly the first time and my results have changed (towards what should be expected rather than what was displayed). New results below to retain history and keep comments making sense.

#+title:Title

* Test
1) e.g.~example

2) e.g. example

3) 
  #+BEGIN_LaTeX
    e.g.~example
  #+END_LaTeX

4) e.g. example   %Space is non-breaking

5) e.g.\nbsp example    %Space is non-breaking
\item e.g.\~{}example
\item e.g. example
\item e.g.~example    
\item e.g. example    %Space is non-breaking
\item e.g.~ example   %Space is non-breaking

enter image description here

This corresponds to the stated double-spacing (as well as the non-breaking space exporting properly)

6
votes

I included the following in the heading of my document/template/skeleton:

\#+LaTeX_HEADER: \DeclareUnicodeCharacter{00A0}{~}

And I use C-x 8 SPC to insert the unicode non-breaking space in the text. This should work exporting to LaTeX or to HTML.

Explanation:

The macro DeclareUnicodeCharacter is defined in the file utf8.def (the file used by inputenc to define the utf8 mapping) to define the meaning of Unicode characters. The first argument is the hexadecimal code for the character, and the second is the replacement LaTeX code.

The non breaking space is U+00A0, therefore the first argument is 00A0. In LaTeX, a non-breaking space is generated with a tilde, therefore the second argument is a single tilde.