Help:Newlines and spaces

From Meta, a Wikimedia project coordination wiki

Edit box[edit]

In the edit box, if a line of text is so short that the first word of the next line would fit on it, this indicates that there is an invisible newline code at the end. In other cases it is not directly visible whether there is a newline or just word wrap, with a space between the last word of one line and the first word of the next line. One way to detect an unwanted newline is to try changing the width of the window. Or, to eliminate a suspected newline whether or not it is really there, simply delete characters until the last word of the first line runs together with the first word of the next, and enter the desired separator. This method also works when there were one or more spaces before the newline.

To detect unwanted space characters at end of line without making changes, put the cursor to the right of the last visible character and move it using the right arrow key. If there is no space at end of line, the cursor will move to the beginning of the next line; in this case there is almost always a newline present (but see the last example below). Otherwise, the cursor moves rightward and there is a space to its left. Continued use of the right arrow key will reveal any additional space characters, but the right arrow key cannot distinguish the presence or absence of a newline.

For page rendering these distinctions are not relevant in plain text (see below): all three possibilities render as a space. However, table and list code is sensitive to newlines, and if template expansion produces a newline at the start (or end), this cumulates with a newline before (or respectively after) the template call, giving a new paragraph.

Word wrap might also take place without a space, as in the last example below. Thus, if a line ends with a non-alphanumeric and the next starts with one, there might be nothing, a space, a newline or both between them. In at least one browser, it is not possible to distinguish "nothing" from a newline without either changing the window width or deleting characters. To reliably eliminate a possible newline between the end of one line and the beginning of the next, place the cursor to the right of the last character of the line and delete the next character (or place the cursor to the left of the first character of the next line and delete the previous character) until the opposite character disappears. Then replace it. See the last example below.

Examples (see edit box and vary the window width):

Here is a newline: Here is a newline with a space: Next line. Table code follows after a newline:

a bcd
efg h

The same without a newline: {|class="wikitable" |- |a||bcd |- |efg||h |}

  1. This ends with a newline.
  2. Pqr
  3. No newline after this. #Pqr

4545 4545

"{{{{{1}}}|{{{2}}}=|{{{4}}}=|{{{6}}}=}}" gives "{{{{{1}}}|{{{2}}}=|{{{4}}}=|{{{6}}}=}}" [1]

A tab character is for example produced by Special:AllPages. It shows up in the edit box as at least one and at most 8 spaces, namely such a number that the last space is at a position of a multiple of 8, counting from the left from 1. Thus when inserting a character on the left, the character after the tab either does not move, or move 8 positions.

#ifeq considers a space, a tab and a newline as all different, e.g. {{#ifeq:a b|ab|1|0}} (with first a space and then a tab) gives 0.

Rendering[edit]

Two or more spaces are rendered as one; one newline is rendered as a space; two or more newlines are rendered as such:


p q  r   s    t
u
v
w

gives


p q r s t u v w


A single newline is rendered as such using the parser function #tag:poem:

{{#tag:poem|abc [[meta:Sandbox|abc]] abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
def def def {{tc}} def def def def def def def def def def def def def def def def def def def def def def
ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi}}
gives

abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc
def def def in def def def def def def def def def def def def def def def def def def def def def def
ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi ghi

[2]

A tab is rendered as a space.

Non-breaking spaces[edit]

The non-breaking space is a space which can't be "broken" by an automatic line break (line wrap), and which therefore ensures that the word before it and after it are not separatedly displayed on two lines. Its usage is required in several cases, for instance units of the International System of Units (they must have a space before, but would make values unreadable if on different lines), and recommended or required in many other cases depending on the language- and typographic-specific rules, which sometimes also require a non-breaking thin space instead (half of a normal space).

MediaWiki makes typing easier by automatically transforming normal spaces into non-breaking spaces when required. This is what it does currently (quoting from a bugzilla comment):

  • If you have a character (any character including spaces), followed by a space, followed by any of the following characters: ?,:,;,!,% or » (U+BB), the space gets replaced with a non-breaking space.
  • If you have a « (U+AB) followed by a space, that space is replaced by a non-breaking space.
  • As an exception to these rules, if you have a non-breaking space followed by "!important", the non-breaking space is turned back into a normally breaking space. This is to prevent messing up CSS style attributes. (This isn't perfect, there's an open bug somewhere about css styles being messed up by this in edge cases).

There are some proposals to add more such rules and to make them localisable so that each language can set its own rules, see bug 13619 and its duplicate bug 18443. This would be analogous to what we already done for linktrail rules.

Sometimes this also gives problems, see testwiki:Wikitable and leading space (bug 38797).

Trimming on save[edit]

In the case of one or more newlines at the start of the wikitext of a page, this number is reduced by one on every edit. Thus just pressing edit and save changes the page; this can be repeated, until no newline is left. Spaces (and hence lines with just spaces) are preserved. This unwanted behaviour has been fixed in MediaWiki 1.18 (see bug 12130).

Spaces and newlines at the end of the wikitext of the page are deleted right away on saving.

Thus a stored page has no newline at the end but may have one or more newlines at the start.

To preserve spaces and newlines at the start and end of the included part of a template, add a noinclude part (it can be dummy, in which case the pair of tags can be reduced to <noinclude />) at the start and/or end, see Template:T spaces and Template:T newlines. Alternatively includeonly tags can be used.

Conversely, avoid an unwanted newline between the included part and the noinclude part.

Automatic newline[edit]

Automatically a newline is added before the expanded wikitext of a template, parser function or variable if this expanded wikitext starts with "#" (ASCII 35), "*" (42), ":" (58), ";" (59), or "{|":

"{{T num}}" → "

  1. p" [3]

"{{T ast}}" → "

"{{T colon}}" → "

p" [5]

"{{T table lb}}
|}"
→ "

" [6]

It is not prevented by a noinclude part at the start:

"{{T ast noinclude}}" → "

This also occurs when the result of a parser function (and hence also the result of the invocation of a module) starts with these characters:

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

C1, C2." [8]

If this is not desired, as a workaround some unconditional text, if any, can be moved into the then and else parts:

"The tuples include A1, A2; B1, B{{#if:x|2; C1, C2|2}}." → "The tuples include A1, A2; B1, B2; C1, C2." [9]

pstart-#-endq

A newline is also added when the result of a variable starts with one of these characters. However, for one of the few variables where this could happen, PAGENAME (and related parser functions), this has been disabled, see *demo.

Thus string processing that processes characters one by one typically adds newlines before "*", "#", ":", ";". See e.g. Template:Sub with the second parameter not equal to 0.

Workarounds that may be suitable in some cases:

Use a parameter tag, see below.

Instead of p{{q|r}}, use {{q1|r|p}}, where q1 is an adapted version of template q putting {{{p}}} in front of the result that q would give, excluding the possible automatic newline.

Similarly for some parser functions:

- for conditional functions (those with "#if..", and #switch) put the p as prefix before the then and else parts, like in the example above, and:

-- "p{{lc::ABC|}}" gives "p

abc" [10]

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

One might also remove the undesired newline(s) with a template that invokes a module (because a module is much more suitable for string operations). See also Template:Rmanl, which does not invoke a module.

In some cases a suitable workaround is to use <nowiki>#</nowiki> etc. or the numeric character references (NCR) &#35; &#42; &#58; &#59;, or to insert a zero-width non-breaking space or similar like &#65279;. This depends on whether there is any further processing of the expanded wikitext, and if so, whether this is sensitive to this change.

For a conditional anchor in a link, you can make the # unconditional: without any anchor id after it, it has no effect.

If you have a template/parser function that produces * and you WANT it to be parsed as wikitext — but without adding a newline, you can write <li>; a <ul> element will be added by the software.

If you want the same for : you can wrap the string in a <dl><dd> ... </dd></dl>. This typically requires you to be able to add characters at the end of the string; without the closing tags, subsequent lines are indented as well.

See also:

Using a parameter tag to avoid an automatic newline[edit]

A string starting with "*", "#", ":", or ";", not being prefixed by a newline, can only be produced in two ways: by explicitly putting at least the first character in the wikitext, or by using a parameter tag:

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

This does not allow control on the page itself, but only when it is transcluded or substituted. If the template call contains one or more parameter definitions, and p is an expression that may evaluate to one of the parameter names, this provides branching with ";" etc. without newline as possible result.

For example, the template call is {{t|x|y}}. If p evaluates to 1 or 2, {{{p|q}}} gives x or y respectively, otherwise q.

Typically the template call has to contain parameter definitions which do not represent choices of the end user. Therefore, to avoid bothering the end user with them, this is a call of an auxiliary template, while an outer template provides this call, while additionally passing on its own parameters.

For example, the template call is {{t|x|y|n={{{1}}}}}. If {{p|{{{n}}}}} evaluates to 1 or 2, {{{{{p|{{{n}}}}}|q}}} gives x or y respectively, if it evaluates to "n" it gives {{{1}}}, and otherwise it gives q. In this example the case that {{p|{{{n}}}}} evaluates to "n" would typically be avoided, the result being an unintended side effect. For an example, see Template:Chr7a with auxiliary template Template:Chr7a/1. With the parameter name n there is no clash with the parameters with the implicit and explicit numerical names 0 - 99, and the result of Template:Digit is also numerical, so not equal to the character n.

Trimming on expansion[edit]

Spaces, tabs and newlines are stripped from:

  • the start and end of the expanded wikitext of a title, i.e., the start and end of the expanded wikitext of the name of a template, variable, and parameter,
  • the start and end of the expanded wikitext of the value of a named template parameter; however, the code "&#10;" is not stripped, and equally causes for example "*" to be rendered as a bullet
  • the start and end of the expanded wikitext of the value of a parameter of a parser function

Spaces, tabs and newlines are preserved:

  • between the name of a parser function and the colon (with the effect that with spaces or newlines it does not work!)
  • at the start and end of the expanded wikitext of the value of an unnamed template parameter
  • at the start and end of the expanded wikitext of the default value of a template parameter
  • at the start and end of the expanded wikitext of a template

For example:

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

"{{1x|1=
a}}"
gives "a"

Of course, in a chain of operations, preservation requires preservation in every operation. For example, using Template:Nln and Template:2x, p{{2x|{{nln}}}}q gives

p

q

due to the three preservation rules, while p{{2x|1={{nln}}}}q, p{{#if:x|{{2x|{{nln}}}}}}q, and p{{2x|{{#if:x|{{nln}}}}}}q give pq, pq, and pq, because in each case somewhere in the expansion chain the newlines are stripped. Similarly, using Template:Spc, p{{2x|{{spc}}}}q gives p q, while p{{2x|1={{spc}}}}q, p{{#if:x|{{2x|{{spc}}}}}}q, and p{{2x|{{#if:x|{{spc}}}}}}q give pq, pq, and pq.

Stripping of spaces and newlines can be inconvenient, but also convenient, it allows easy formatting of the wikitext without affecting the result:

"{{2x | 1 = pq }}"

gives "pqpq", and

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

gives "rs".

Preservation of spaces and newlines using unnamed parameters[edit]

For branching with preservation of spaces and newlines using unnamed parameters, seemingly trivial templates such as Template:Void and parameter selection templates can be used. This is for example done in various branching templates, e.g. Template:If.

Parameter selection templates[edit]

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

gives

  • "q"
  • "q"

Thus, both from index values and from results, spaces are stripped.

As a workaround one can use parameter selection templates Pn:

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

This technique is used in Template:Sw (which can be extended to cover more cases):

"{{sw|p||p| q |r|s}}" gives "q".

The allowed intermediate values are the positive integers for which there is a corresponding template Pn:

P1 P2 P3 P4 P5 P6 P7 P8 P9 P10

Alternatively one of the generic templates Template:Pp and Template:Ppp is used, see Help:Array#Parameter selection templates.

"{{lc: AbC}}

dEf"
→ "abc

dEf " [14]


"{{uc: AbC}}

dEf"
→ "ABC

dEf " [15]


"{{lcfirst: AbC}}

dEf"
→ "abC

dEf " [16]


"{{ucfirst: AbC}}

dEf"
→ "AbC

dEf " [17]


"{{urlencode: AbC

dEf ghi}}"
gives "AbC%0A%0AdEf+ghi" [18]

Thus inner newlines become %0A and inner spaces become +.


"{{anchorencode: AbC

dEf ghi}}"
gives "AbC__dEf_ghi" [19]


"{{padleft: bc d |8|a}}" gives "aaaabc d" [20]

"{{padright: bc d |8|a}}" gives "bc daaaa" [21]


"{{formatnum: 12345 67890}}" gives "12,345 67,890" [22]

Conditional table rows[edit]

See Help:Table#Producing table syntax using templates and/or parser functions.

Alternatives[edit]

Depending on the application, an alternative for a pure newline or blank space in the expanded wikitext can be used, e.g.:

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

Also the space etc. can be put between text and <nowiki/>.

See also[edit]

Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · Wikidata · MediaWiki · Wikibooks · Wikisource · MediaWiki: Manual · Google
Versions of this help page (for other languages see further)
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Namespace · Page name · Section · Backlinks · Redirect · Category · Image page · Special pages · Printable version
Tracking changes
Recent changes (enhanced) | Related changes · Watching pages · Diff · Page history · Edit summary · User contributions · Minor edit · Patrolled edit
Logging in and preferences
Logging in · Preferences
Editing
Starting a new page · Advanced editing · Editing FAQ · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URL · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · Lists · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Magic words · System message · Substitution · Array · Calculation · Transclusion
Others
Special characters · Renaming (moving) a page · Preparing a page for translation · Talk page · Signatures · Sandbox · Legal issues for editors
Other languages: