32

How can I make a non-breaking space in reStructuredText?

An obvious but problematic solution is:

`word A`

But it might be treated differently by different implementations, such as rst2latex or rst2pdf. Plus it is rendered in italics.

Davoud Taghawi-Nejad
  • 16,142
  • 12
  • 62
  • 82

5 Answers5

41

You need the unicode directive, but it can only be used in substitutions. So you need to define a substitution like this:

.. |nbsp| unicode:: 0xA0 
   :trim:

and then use it like this:

xx |nbsp| xx

:trim: is there to get rid of those spaces around the substitution.

ynka
  • 1,457
  • 1
  • 11
  • 27
  • It seems that the 0xA0 substitution comes out as character 32 in HTML for me :( – jterrace Aug 27 '12 at 16:20
  • 1) the xx\ |nbsp|\ xx thing works in the renderer (http://www.tele3.cz/jbar/rest/rest.html)... No idea about 32 :( – ynka Aug 29 '12 at 08:32
  • Acc to the documentation, you can pass to the unicode directive the following formats: [decimal numbers or hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u or as XML-style hexadecimal character entities, e.g. ᨫ]. Maybe you can experiment with different notation for nbsp which acc to Wikipedia are [U+00A0 no-break space (HTML:    ). Can be encoded by UTF-8 as 0xC2 0xA0.] – ynka Aug 29 '12 at 08:41
  • I tried again and this does seem to be working. You can add ``:trim:`` to the directive to get it to ignore the spaces around the substitution. If you update the answer with the trim, the bounty is yours - thanks! – jterrace Aug 29 '12 at 16:39
  • pleasure doing business with you :) – ynka Aug 30 '12 at 12:33
  • `|nbsp|` is defined in https://docutils.sourceforge.io/docutils/parsers/rst/include/isonum.txt (though without the trim) – Jason S Jan 10 '23 at 22:16
10

You can also use |_| in place of |nbsp| which is less visually intrusive, given reStructuredText's goal of being readable as plain text.

Terry Brown
  • 1,300
  • 14
  • 13
4

I don't see the problem here, running docutils v0.9. At least rst2latex and rst2html are behaving properly regarding non-breaking whitespace. Latex generates ~ and html generates   when you input a non-breaking character (\xa0, \0240).

Maybe you have an editor issue ? If you can manage to input the character, docutils will do the job.

cJ Zougloub
  • 1,484
  • 10
  • 19
  • 5
    a non-visible character is not useful – jterrace Aug 29 '12 at 01:12
  • Then you can either configure your editor to make it visible or use an extra preprocessing pass in your input files... – cJ Zougloub Aug 29 '12 at 01:31
  • 1
    Usually, you are not the only person to edit that file, and other persons may use different editors, and even the persons using the same editor will, most likely, use different configurations... – Klaws Jan 22 '19 at 13:13
2

I ended up coming up with a workaround for Sphinx. I overwrite the HTML and LaTeX writers to convert the ~ character to a non-breaking space. Here's the HTML one:

import sphinx.writers.html
BaseTranslator = sphinx.writers.html.SmartyPantsHTMLTranslator

class CustomHTMLTranslator(BaseTranslator):
    
    def bulk_text_processor(self, text):
        if '~' in text:
            text = text.replace('~', ' ')
        return text

sphinx.writers.html.SmartyPantsHTMLTranslator = CustomHTMLTranslator

and the LaTeX one:

import sphinx.writers.latex
BaseTranslator = sphinx.writers.latex.LaTeXTranslator

class DocTranslator(BaseTranslator):

    def visit_Text(self, node):
        if self.verbatim is not None:
            self.verbatim += node.astext()
        else:
            text = self.encode(node.astext())
            if '\\textasciitilde{}' in text:
                text = text.replace('\\textasciitilde{}', '~')
            if not self.no_contractions:
                text = educate_quotes_latex(text)
            self.body.append(text)

sphinx.writers.latex.LaTeXTranslator = DocTranslator

It's not so pretty, and it doesn't even let you escape the ~ character, but it works for my purposes.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
jterrace
  • 64,866
  • 22
  • 157
  • 202
1

I have not tested it but maybe you can use http://docutils.sourceforge.net/docs/ref/rst/directives.html#unicode-character-codes and the unicode "no break space" character: http://www.fileformat.info/info/unicode/char/a0/index.htm

Roberto Alsina
  • 782
  • 4
  • 8