Reputation: 16796
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.
Upvotes: 33
Views: 8380
Reputation: 897
A third alternative besides a literal NBSP and a custom "unicode" substitution is including a reStructuredText Standard Definition File, e.g.
.. include:: <isonum.txt>
xx\ |nbsp|\ xx
The escaped spaces are required, because the standard definition file does not use the :trim:
option.
Upvotes: 0
Reputation: 67073
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.
Upvotes: 2
Reputation: 1330
You can also use |_|
in place of |nbsp|
which is less visually intrusive, given reStructuredText's goal of being readable as plain text.
Upvotes: 10
Reputation: 1497
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.
Upvotes: 44
Reputation: 1494
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.
Upvotes: 5
Reputation: 800
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
Upvotes: 1