57

How do I force a line break/new line in rst? I don't want it to be a new paragraph (ie. no additional spaces between the lines), I just want the text to start on a new line. Thanks!

3 Answers 3

55

The line block syntax also worked, and was a bit cleaner:

| This is a line | This is another line | Another new line

5
  • 1
    And this works with github rst (where raw html and many directives are not available).
    – coldfix
    May 7, 2019 at 16:58
  • Works in VS Code on Windows 10 without anything else required (just add a | to every line beginning of the subsequent lines you would like to have as a contiguous block having newlines without empty lines between them)
    – Andreas L.
    Jun 10, 2021 at 12:49
  • 1
    Maybe it's just pypi.org, however it did not give me a line break without a new paragraph. To get a new line immediately following a few long lines I have to a) start a new (empty) line and b) THEN (on the next line) follow it with | <my new line>. This works ... <my new line> immediately comes after the previous text.
    – Aendie
    Aug 29, 2022 at 17:48
  • This should be the accepted solution, as it works for a much wider variety of use cases
    – Sigmatics
    Mar 23, 2023 at 14:42
  • @Aendie: You may put the entire paragraph in a line-block. Long lines may be broken in the source (indent the continuation to match the start) and will automatically be wrapped in the output. See the example Steve Piercy's answer below.
    – G. Milde
    Mar 5 at 0:23
31

According to the docs for docutils raw role, you can do this:

If there just *has* to be a line break here,
:raw-html:`<br />`
it can be accomplished with a "raw"-derived role.
But the line block syntax should be considered first.

You will need to define the raw role first:

.. role:: raw-html(raw)
    :format: html

As the example states, consider line block syntax first.

| Lend us a couple of bob till Thursday.
| I'm absolutely skint.
| But I'm expecting a postal order and I can pay you back
  as soon as it comes.
| Love, Ewan.
4
  • Thanks. This required defining the raw role first: .. role:: raw-html(raw) :format: html
    – tiggerae
    Jul 6, 2018 at 19:01
  • 1
    It might be worth noting that you must indent before each | for it to start a new line.
    – asrjarratt
    Sep 4, 2019 at 13:58
  • 2
    @asrjarratt not according to docutils. One may, not must, and only if nesting is desired. Sep 4, 2019 at 20:38
  • @StevePiercy I stand corrected. I think there was a case where I was having trouble getting it to work without indenting it, and I've just been indenting ever since. Thanks for pointing that out.
    – asrjarratt
    Sep 5, 2019 at 12:50
15

We added a substitution to our global.rst file so we didn't have to add raw tags all the time.

.. |br| raw:: html

  <br/>

This way we can just use |br| when we need a line break.

2
  • 3
    To avoid top & bottom padding of a linebreak, replace <br/> with <div style="line-height: 0; padding: 0; margin: 0"></div> Aug 28, 2020 at 19:16
  • This I like. Light and specific. The other general raw HTML role answer is good too, but this minimal markup is nice. When using restview I also find that the given answer with<br/> works best and does not add top and bottom padding with my CSS. Using the suggested <div>, though, leads to separation because the restview parser appears to trigger an end of paragraph when encountering the <div> tag, leading to exactly the opposite of what's needed.
    – NeilG
    Nov 14, 2023 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.