Jump to content

Manual:Newlines and spaces

From mediawiki.org

Newlines and spaces make up sentences in a wiki. Newlines create paragraphs and when you are writing sentences, a single newline is ignored, but a double newline results in a paragraph break. On the other hand, spaces, particularly leading spaces at the beginning of a line, are used to create preformatted text blocks. It can also be used to create indentations which is useful for creating lists.

Rendering spaces, tabs, and new lines

[edit]

Multiple spaces are rendered as a space. A single new line is rendered as a space. Two new lines are rendered as a new paragraph, while multiple new lines are rendered as multiple paragraphs.

For example:

p q  r   s    t
u
v
w

The above example renders as:


p q r s t u v w


Non-breaking spaces

[edit]

A non-breaking space keeps words or numbers from being split into different lines. It's often used to keep units of measurement and other text together. When you have a text with regular spaces, the browser may break the line wherever it sees fit. Sometimes, a narrower version called a non-breaking-thin space (half the width of a normal space) is used instead.

You can use a non-breaking space by inserting the   character or the {{nbsp}} template.

You can use a non-breaking-thin space by inserting the   character.

Trimming on save

[edit]

Spaces and new lines at the end of wikitext are removed when saved, so a saved page has no new line at the end but might have new lines at the beginning.

To keep spaces and new lines at the start and end of a template, use ‎<noinclude>...‎</noinclude> tag (even just <noinclude />) at the beginning or end. You can also use ‎<includeonly>...‎</includeonly> tags.

For example:

<noinclude />
This is the content of the template.
<noinclude> </noinclude>

In the example above,

  • The <noinclude /> tag at the beginning helps retain any spaces or new lines before the content.
  • The ‎<noinclude>...‎</noinclude> tags at the end ensure that spaces or new lines after the content are preserved.

Avoid adding an extra new line between the included part and the ‎<noinclude>...‎</noinclude> tag part.

Trimming on expansion

[edit]

When it comes to trimming spaces, tabs, and new lines, two rules apply, stripped and preserved. here's how things work:

Stripped: This is the removal of spaces, tabs, and new lines from specific locations within the wikitext during the expansion process. The key scenarios where stripping occurs include:

  • Title expansion: The start and end spaces around expanded title (like template, variables, and parameter names) are trimmed.
  • Named template parameter values: The start and end of the expanded wikitext are trimmed, except for the code , which remains and may cause characters like * to render as bullet points.
  • Parser function parameters: Spaces at the start and end of a parameter's expanded wikitext are removed.

Preserved: This involves keeping spaces, tabs, and new lines in their original positions. The key scenarios where preservation occurs include:

  • Parser function names: Space between the name of a parser function and the colon is preserved, even though it may cause the function to malfunction if spaces or new lines are present.
  • Unnamed template parameter values: Space at the beginning and end of the expanded wikitext of unnamed template parameter values are kept intact.
  • Default template parameter values: The start and end of the expanded wikitext of default values for template parameters are preserved.
  • Templates: Space at the beginning and end of the expanded wikitext of templates is retained.

A few examples:

  • "{{1x| a }}" → " a " [1]
  • "{{1x|1= a }}" gives "a" [2]

Sequence of operations

[edit]

In a sequence of operations, each step must preserve spaces and new lines.

Example 1:

Using {{Nln }} (expanding to a newline) and {{2x }} (producing its output twice)

p{{2x|{{nln}}}}q results to:


p

q


However, p{{2x|1={{nln}}}}q, p{{#if:x|{{2x|{{nln}}}}}}q, and p{{2x|{{#if:x|{{nln}}}}}}q respectively results to the below because spaces are stripped at some point.


pq, pq, and pq


Example 2:

Using {{Spc }} producing just a space:

p{{spc}}q results to


p q


"{{#switch:2|1=pq|2=rs|3=tu}}"

results to


"rs"


Automatic new line

[edit]

A new line is added before the expanded wikitext of a template, parser function, or variable if it starts with any of the following characters:

  • "#" (ASCII 35)
  • "*" (42)
  • ":" (58)
  • ";" (59)
  • "{|"

For example:

{{1x|*p}} renders as

  • p

{{1x|#p}} renders as

  1. p

{{1x|:p}} renders as

p

In the below example, even with a ‎<noinclude>...‎</noinclude> at the start, a new line is still added:

{{1x|<noinclude/>*p}} renders as

  • p

Handling module outputs

[edit]

The above rule also arises if the output of a parser function, or the result from invoking a module, starts with these characters:

The tuples include A1, A2; B1, B2{{#if:x|; C1, C2}}.

renders as


The tuples include A1, A2; B1, B2

C1, C2.

To work around this, move any unconditional text into the then and else sections:

The tuples include A1, A2; B1, B{{#if:x|2; C1, C2|2}}.

renders as


The tuples include A1, A2; B1, B2; C1, C2.


Handling variables

[edit]

A new line is also added if a variable's result starts with specific characters. This is disabled for the PAGENAME variable and related parser functions. See *demo.

There are a few workarounds:

  • Use parameter tags, as shown below:

Instead of p{{q|r}}, use {{q1|r|p}}, where q1 is a modified version of "q" that adds {{{p}}} in front of the result.

  • Use conditional functions (like "#if..." and "#switch"), and put "p" as a prefix before the then and else parts.

p{{lc::ABC|}} -> p

abc

"{{lc:p:ABC|}}" → "p:abc" [3]

  • You can remove unwanted new lines with a template that uses a module, as modules are generally better suited for string operations. For an example without a module, see Template:Rmanl.
  • Use nowiki tags: To work around certain issues in wikitext, you can use <nowiki>#</nowiki> etc. or their numeric equivalents (&#35; &#42; &#58; &#59;). A zero-width non-breaking space (&#65279;) can also be useful. Choose based on whether further processing will happen and if it's sensitive to these changes.
  • For conditional anchors in links, use # unconditional. Without an anchor ID after it, it has no effect.

Parsing list and definition items without new lines

[edit]

To make * from a template or function parsed as wikitext without adding a new line, use <li> so the software adds a <ul> element.

For :, wrap the string in <dl><dd> ... </dd></dl>, and remember to include closing tags to avoid unwanted indentation on subsequent lines.

See:

Avoiding automatic new lines with parameter tags

[edit]

To avoid a new line before a string starting with "*", "#", ":", or ";", you can either explicitly include the initial character in the wikitext or use a parameter tag.

Example 1:

  • a{{{p|q}}}b results to a;b if parameter "p" has value ";".
  • a{{{p|;}}}b results to a;b if parameter "p" is undefined.

This method works only when the template is transcluded or substituted, not directly on the page. If the template call includes parameter definitions, and "p" can evaluate one of these parameter names, it enables branching with ";" etc., without introducing a new line.

Example 2:

Template call - {{t|x|y}}

If "p" evaluates to 1 or 2, {{{p|q}}} will output "x" or "y" respectively; otherwise, it outputs "q".

Typically, parameter definitions that are not user choices are handled by an auxiliary template called an outer template, which also passes its parameters.

Example 3:

Template call - {{t|x|y|n={{{1}}}}}

If {{p|{{{n}}}}} evaluates to 1 or 2, {{{{{p|{{{n}}}}}|q}}} outputs "x" or "y".

If it evaluates to "n", it outputs {{{1}}}.

Otherwise, it outputs "q".

In this example, you typically avoid the case where {{p|{{{n}}}}} evaluates to "n" to prevent unintended side effects.

For an example, see Template:Chr7a with its auxiliary template Template:Chr7a/1. Using the parameter name "n" avoids clashes with parameters named 0-99, and the result of Template:Digit is also numerical, thus not equal to the character "n".

Preserving spaces and new lines with unnamed parameters

[edit]

To preserve spaces and new lines using unnamed parameters, you can use simple templates like Template:Void or parameter selection templates. For example, Template:If and other branching templates use this technique.

Parameter selection templates

[edit]
* "{{#switch: p |p= q |r=s}}"
* "{{#switch:p| p = q |r=s}}"

results to:

  • "q"
  • "q"

These examples show that spaces are stripped from index values and results:

To maintain spaces use parameter selection templates Pn:

"{{p{{#switch:p|p=1|r=2}}| q |s}}" results to " q "

This method is used in Template:Sw:

"{{sw|p||p| q |r|s}}" results to "q".

The allowed intermediate values are the positive integers for which there is a corresponding template Pn. On this wiki, that list only goes up to three, but additional templates could be created to support higher values:

P1 P2 P3

Alternately use the generic Template:Pp and Template:Ppp. See Manual:Representing arrays.

Alternatives

[edit]

Depending on the application, you can use the following instead of a pure new line or blank space in the expanded wikitext:

  • &nbsp; or &#160; (non-breaking space)
  • &#32; (normal space)
  • <br />

You can also place the space or other characters between text and <nowiki/>.

The Edit box

[edit]

If a line of text in the edit box is too short and the next word would fit on it, there might be an "invisible" new line at the end. Sometimes, whether a new line is hard (explicit) or soft (due to line wrap) is unclear.

There are various ways to detect this:

  • Changing the window width.
  • Delete characters until the last word of the first line joins with the first word of the next line, then add a separator.
  • Activate syntax highlighting in the edit box (It's represented with a pencil symbol). This automatically displays the line numbers.
  • Use an editor like Visual Studio Code, Eclipse, etc. before transferring your wikitext to the wiki.

Detecting unwanted space characters can be an issue too. To detect this:

Move the cursor to the right of the last visible character using the right arrow key. If the cursor jumps to the next line, there's a new line there. If the cursor moves right and there's a space on its left, that means there are extra spaces. Keep pressing the right arrow key to find more spaces, but this method won't show if there's a new line.

Word wrap can occur without spaces as demonstrated in the example below. If a line ends with a non-alphanumeric character and the next line starts with an alphanumeric character, there could be no space, a space, a new line, or both between them. In some browsers, it's hard to tell if there's a new line without adjusting the window or deleting characters. To remove a possible new line, position the cursor after the last character of the line, adjust right, and delete the next character, or place the cursor before the first character of the next line and delete the previous character until the new line is gone, then replace it.

Here are examples below demonstrating new lines:

Example 1:

A new line:
A new line with a space: 
Next line. The next line is a table:
{|class="wikitable"
|-
|a||bcd
|-
|efg||h
|}

The above example renders as:


A new line: A new line with a space: Next line. The next line is a table:

a bcd
efg h

Example 2:

A table on the same line as this sentence: {|class="wikitable"
|-
|a||bcd
|-
|efg||h
|}
# Another new line
# Pqr
# No new line after this. #Pqr

{{#expr:(2+3)*(4+5)}}{{#expr:(2+3)*(4+5)}} {{#expr:(2+3)*(4+5)}}{{#expr:(2+3)*(4+5)}}

Another new sentence on the same line as this template. {{xpda|gives={{{gives|gives}}}|l4={{{l|}}}|result={{{d|"}}}{{{{{1}}}|{{{2}}}={{{3|}}}|{{{4}}}={{{5|}}}|{{{6}}}={{{7|}}}}}{{{8|}}}{{{9|}}}{{{10|}}}{{{11|}}}{{{12|}}}{{{13|}}}{{{14|}}}{{{d|"}}}|{{{d|"}}}|{|{|{{{1}}}|&p|{{{2}}}|&e|{{{3|}}}|&p|{{{4}}}|&e|{{{5|}}}|&p|{{{6}}}|&e|{{{7|}}}|}|}|{{xpd/code|{{{8|}}}}}|{{xpd/code|{{{9|}}}}}|{{xpd/code|{{{10|}}}}}|{{xpd/code|{{{11|}}}}}|{{xpd/code|{{{12|}}}}}|{{xpd/code|{{{13|}}}}}|{{xpd/code|{{{14|}}}}}|{{{d|"}}}}}

The above example renders as:


A table on the same line as this sentence: {|class="wikitable" |- |a||bcd |- |efg||h |}

  1. Another new line
  2. Pqr
  3. No new line after this. #Pqr

4545 4545

Another new sentence on the same line as this template. "{{{{{1}}}|{{{2}}}=|{{{4}}}=|{{{6}}}=}}" gives "{{{{{1}}}|{{{2}}}=|{{{4}}}=|{{{6}}}=}}" [4]


From the above examples, you can see that in tables and lists, new lines matter. Also, when using a template that expands and produces a new line at the start (or end), adding a single line in the edit box after a previous word which normally wouldn't create a new paragraph, may end up creating one.

In addition to the above scenarios, MediaWiki can't render tabs. Usually, a tab is equivalent to 4 spaces, when you have more than one space before or after a word in wikitext, it renders as one space. However, the #ifeq function considers it.

The #ifeq function sees spaces, tabs, and new lines as different characters. For example, {{#ifeq:a b|ab|1|0}}, due to to the space between the "a" and "b", the function results to "0". This is because "a b" and "ab" are not considered identical due to the space.

See also

[edit]