Help:Newlines and spaces

From SacredWiki
Jump to navigation Jump to search
MediaWiki Handbook: Contents, Readers, Editors, Moderators, System admins +/-

Edit box

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|a b|1|0}} (with first a space and then a tab) gives 0.

Rendering

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:

Template:Xpdop4oc5c

A tab is rendered as a space.

Non-breaking spaces

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 in 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

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

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}}" → "Template:T num" [2]

"{{T ast}}" → "Template:T ast" [3]

"{{T colon}}" → "Template:T colon" [4]

"{{T table lb}}
|}"
→ "Template:T table lb |} " [5]

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

"{{T ast noinclude}}" → "Template:T ast noinclude" [6]

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:

Template:Xpd5op3c

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

Template:Xpd5op3p3c

pTemplate:T1q

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:

-- Template:Xpd5op3c

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

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

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

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
  • 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 " [8]
  • "{{1x|1= a }}" gives "a" [9]

"{{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

pTemplate:NlnTemplate:Nlnq

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 pTemplate:NlnTemplate:Nlnq, pTemplate:NlnTemplate:Nlnq, and pTemplate:NlnTemplate:Nlnq, because in each case somewhere in the expansion chain the newlines are stripped. Similarly, using Template:Spc, p{{2x|{{spc}}}}q gives pTemplate:SpcTemplate:Spcq, while p{{2x|1={{spc}}}}q, p{{#if:x|{{2x|{{spc}}}}}}q, and p{{2x|{{#if:x|{{spc}}}}}}q give pTemplate:SpcTemplate:Spcq, pTemplate:SpcTemplate:Spcq, and pTemplate:SpcTemplate:Spcq.

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"

"{{#switch:

2

|

1 =

pq

|

2 =

rs

|

3 =

tu

}}" gives "rs"

Preservation of spaces and newlines using unnamed parameters

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

*"{{#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 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 P21 P22 P23 P24 P25 P26 P27 P28 P29 P30 P31 P32 P33 P34 P35 P36 P37 P38 P39 P40 P41 P42 P43 P44 P45 P46 P47 P48 P49 P50 P51 P52 P53 P54 P55 P56 P57 P58 P59 P60 P61 P62 P63 P64 P65 P66 P67 P68 P69 P70 P71 P72 P73 P74 P75 P76 P77 P78 P79 P80 P81 P82 P83 P84 P85 P86 P87 P88 P89 P90 P91 P92 P93 P94 P95 P96 P97 P98 P99 P100

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

Other parser functions

"{{lc: AbC}}

dEf "
→ "abc

dEf " [10]


"{{uc: AbC}}

dEf "
→ "ABC

dEf " [11]


"{{lcfirst: AbC}}

dEf "
→ "abC

dEf " [12]


"{{ucfirst: AbC}}

dEf "
→ "AbC

dEf " [13]


Template:Xpd5o5c

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


Template:Xpd5o5c


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

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


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

Conditional table rows

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

Alternatives

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


Links to other help pages

Help contents
Meta · Wikinews · Wikipedia · Wikiquote · Wiktionary · Commons: · mw: · b: · s: · mw:Manual · Google
Versions of this help page (for other languages see further)
Meta · Wikinews · Wikipediahttp://en.wikipedia.org/index.php/Help:Newlines_and_spaces · Wikiquote · Wiktionary
What links here on Meta or from Meta · Wikipedia · MediaWiki
Reading
Go · Search · Stop words · 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 · User style
Editing
Starting a new page · Advanced editing · Editing FAQ · Edit toolbar · Export · Import · Shortcuts · Edit conflict · Page size
Referencing
Links · URLs · Piped links · Interwiki linking · Footnotes
Style and formatting
Wikitext examples · CSS · Reference card · HTML in wikitext · Formula · List · Table · Sorting · Colors · Images and file uploads
Fixing mistakes
Show preview · Testing · Reverting edits
Advanced functioning
Expansion · Template · Advanced templates · Parser function · Parameter default · Variable · System message · Substitution · Array · Calculation · Embed page
Others
Special characters · Renaming (moving) a page · Talk page · Signatures · Sandbox · Legal issues for editors