How to make non-breaking spaces (ties) in Org-mode that exports properly to LaTeX
Asked Answered
O

6

32

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.

Oldline answered 16/2, 2012 at 12:48 Comment(2)
Belongs on TeX.SE.Gambetta
@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.Oldline
S
30

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
Semicircle answered 23/4, 2013 at 18:37 Comment(2)
There was a typo in the link. (Not sure if it's link rot or a mistranscription.) I think the intended link was orgmode.org/manual/Special-Symbols.html (note the second capital S).Abram
doesn't the {} get translated as literal braces (appearing in the output) if exporting to something other than latex?Educatory
C
12

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>
Callous answered 7/6, 2015 at 4:29 Comment(1)
the idea looks good, but \space is a standard control sequence name, expanding to a normal space, so redefining it as a non-breakable space is problematic. "nbsp" would be better.Educatory
S
7

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.

Org
#+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
LaTeX
\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
Rendered

enter image description here

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

Shamrao answered 16/2, 2012 at 14:26 Comment(11)
When I export e.g.\nbsp example I get e.g.~ example which renders as a non-breaking space followed by a normal space. Am I doing something wrong? I just copied the snippet from you. Is is a special space I need to enter in a special way?Oldline
As I tried to mention (I'll edit to make it more clear) the space between \nbsp and example is C-x 8 Space (the non-breaking space). It just doesn't copy into stack overflow sadly.Shamrao
I used the correct space and the document compiles and the output is correct but there is an error: Exporting to PDF...done, with some errors: [undefined control sequence]. More specifically: ! Package inputenc Error: Unicode char \u8:  not set up for use with LaTeX.Oldline
Not sure why you'd get that. Tested it on mine again just now and mine states Exporting to PDF...done without any errors. It could be a matter of versions of LaTeX, Emacs (24 in my case) or Org-Mode.Shamrao
It seems to be LaTeX connected. I use TeX Live 2011 which is updated today.Oldline
Googling for issues with inputenc and TeX Live does seem to indicate this issue has occurred before/is present. I'm using MikTeX (Windows XP environment) and it works fine.Shamrao
I saw that, no solution I found worked though so I posted a question about it: tex.stackexchange.com/questions/44794/…Oldline
The answers I got for the question linked above leads to double spacing for this solution. It seems this solution works for you by a coincident because the correct behavior of exporting an UTF-8 non-breaking space followed by a TeX non-breaking space should be two non-breaking spaces and that is one too many.Oldline
With that change does your original attempt #4 (non-breaking space) properly export properly? (I suspect it should since it should now be recognized)Shamrao
You are right, the fourth one exports correctly if I use either of the solutions given in the TeX.SX question.Oldline
Maybe you should add a note that either solution given in the TeX.SX question is needed for updated TeX distributions and how to make an Org-mode add those preamble lines.Oldline
C
7

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.

Cenotaph answered 21/2, 2014 at 13:51 Comment(1)
Somehow C-x 8 SPC works with both HTML and LaTeX exporters even without declaring a macro explicitly. I'm using TexLive 2013 on Windows with latexmk -shell-escape -pdf -f.Piston
O
1

I think one of the best ways is @@latex:e.g.~example@@, embedding inline latex code for exporting.

Orthohydrogen answered 20/5, 2022 at 9:34 Comment(0)
B
1

For those still searching for advice on that issue and not wishing latex-style botherations like \nbsp or \space in their texts, I'll add my solution.

I'm inserting unbreakable space (U+00A0) and thin unbreakable space (U+202F) as Unicode glyphs, having them set to handy shortcuts in my emacs's init.el (handy to me, that is; those come from my OpenOffice years):

(global-set-key [?\C-\S-\ ] [?\x00A0])
(global-set-key [?\C-\ ] [?\x202F])

The glyphs are displayed as thin colored underscores of tiny and normal width respectively. The text is easy on eyes with those.

The Org's export filter exports those glyphs as they are, so I have to filter the resulting TeX source through

sed --in-place \
    -e 's/ /\\:/g' \
    -e 's/ /\\,/g' \
mytexsource.tex

Those spaces in sed's expressions are glyphs 160 (00A0h) and 8239 (202Fh), respectively.

The solution admittedly uses extra processing stage, which, I imagine, could be possibly remedied by Org's filters.

On the other hand it looks nice and easy on eyes in Org/emacs.

By the way, isn't tilde a breakable space in latex?

Bitch answered 6/4 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.