Help:Newlines and spaces

From The Scuba Wiki

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

Contents

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:

abcd
efgh

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

Template:1x{{Template:1x|Template:1x=Template:1x|Template:1x=Template:1x|Template:1x=Template:1x}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x gives "{{{{{1}}}|{{{2}}}=|{{{4}}}=|{{{6}}}=}}" [1]

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


Stripping 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.

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 ( talk edit history links ) and template:t newlines ( talk edit history links ). Alternatively includeonly tags can be used.

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

Automatic newline at the start

Templates starting with "*", "#", ":", ";" or "{|" automatically get a newline at the start:

Template:Xpdoc

Template:Xpdoc

Template:Xpdoc

Template:Xpdoc

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

Template:Xpdoc

This also occurs when the result of a parser function starts with these characters:

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{Template:1x|Template:1x*Template:1x}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x gives "pq

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

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{Template:1x|Template:1xTemplate:1x|Template:1xTemplate:1xTemplate:1x}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x gives "pq*r" [3]

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{Template:1x|Template:1xTemplate:1x|Template:1xTemplate:1xTemplate:1x}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x gives "pqr" [4]

Stripping on expansion

Spaces and newlines are stripped from:

Spaces and newlines are preserved at:

  • the start and end of the expanded wikitext of the value of an unnamed template parameter
  • the start and end of the expanded wikitext of the default value of a template parameter
  • the start and end of the expanded wikitext of a template

For example:

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

Preservation, of course, requires cumulative preservation. For example, using template:nln ( talk edit history links ) and template:2x ( talk edit history links ), p{{2x|{{nln}}}}q gives

pTemplate:2xq

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:2xq, pTemplate:2xq, and pTemplate:2xq, because in each case somewhere in the expansion chain the newlines are stripped. Similarly, using template:spc ( talk edit history links ), p{{2x|{{spc}}}}q gives pTemplate:2xq, while p{{2x|1={{spc}}}}q, p{{#if:x|{{2x|{{spc}}}}}}q, and p{{2x|{{#if:x|{{spc}}}}}}q give pTemplate:2xq, pTemplate:2xq, and pTemplate:2xq.

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 "Template:2x"

"{{#switch:

2

|

1 =

pq

|

2 =

rs

|

3 =

tu

}}" gives "rs"

Spaces and/or newlines as value of an unnamed parameter

Using template:1x ( talk edit history links ), fixed spaces and/or newlines can be obtained using {{1x| }}, {{1x|
}}, etc., or template:spc ( talk edit history links ) and template:nln ( talk edit history links ). Conditional spaces and/or newlines can be obtained by also using template:void ( talk edit history links ), so that instead of "1x", one has a conditional template name that is either "1x" or "void".

See, e.g., w:template:Conditional table row Dutch municipality ( talk edit history links ).

Spaces and/or newlines as parameter default

Alternatively, use a parameter (e.g., "undefined") which is known to be undefined; see the templates above and e.g., w:template:Table row Dutch municipality (simple) ( talk edit history links ). Unfortunately, with substitution this does not allow production of plain spaces and/or newlines in the wikitext; instead it gives somewhat cluttering code rendered like the spaces and/or newlines; see e.g., Template:For/aux/few#Substitution.

Conditional spaces and/or newlines can be obtained with this method by having in addition a parameter "empty" which has to be defined as the null string in the template call ("empty="): put a parameter with a conditional name at the end, with the spaces and/or newlines as default value; depending on the condition the name should either be "empty" or "undefined". In the latter case we get the spaces and/or newlines. See e.g., w:template:Conditional table row Dutch municipality ( talk edit history links ).

Another sample of a template generating a newline on substitution can be found at w:template:Conditional template call with newline‎ ( talk edit history links )

To avoid using the cryptic "empty=", the template call can be put in another template that transfers its parameters.

More examples

Examples using #if, template:1x ( talk edit history links ), and template:strip ( talk edit history links ):

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{Template:1x|
Template:1x
}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x
gives "pqr" [6]

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{1x|
Template:1x
}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x
gives "pTemplate:1xr" [7]

Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x{{strip|
Template:1x
}}Template:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1xTemplate:1x
gives "pqr" [8]

Template with newline called inside parser function

As mentioned, if the template with the spaces and/or newlines at the end is called inside the expression for the value of an unnamed parameter of another template they are preserved. However, if the template with the spaces and/or newlines at the end is at the end of the then or else part of #if etc., or at the end of the expression for the value of a named parameter of another template, the spaces and/or newlines are stripped after all.

Using template:tnl1 ( talk edit history links ), template:3x ( talk edit history links ), and template:for ( talk edit history links ), compare:

Template:Xpdoc


{{3x|{{tnl1}}}}

{{3x|1={{tnl1}}}}

{{#if:x|{{tnl1}}}}d

{{for||call=tnl1|a|b}}

{{for||call=tnl1|a|b|nl=}}

expands to


abc
abc
abc


abcabcabc

abcd

abcabc

abcabc

Fix

Preserve newlines inside parser functions by prefacing them with <nowiki />:

NOTE: as of mediawiki 1.16, this example is no longer working, the solution marked "fixed" is still broken...

Markup Rendered Note
((#if: 1|a<nowiki />

b|c}}

a

b

preserves following newline
((#if: 1|a

*b|c}}

a
  • b
may not be what you want
((#if: 1|a<nowiki />

*b|c}}

a
  • b
fixed

Switch

*"{{#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 ( talk edit history links ) (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

To avoid the need of many auxiliary templates, the switch functionality can also be obtained with nested parser functions #ifeq, and hence with template:if ( talk edit history links ).

Fix

Preserve whitespace inside parser functions by using <nowiki /> tags:

Markup Rendered
{{#switch:p| p =<nowiki/> q <nowiki/>|r=s}} ' q '

Other parser functions

{{lc: AbC

dEf }} gives

"abc

def".


{{uc: AbC

dEf }} gives

"ABC

DEF".


{{lcfirst: AbC

dEf }} gives

"abC

dEf".


{{ucfirst: AbC

dEf }} gives

"AbC

dEf".


{{urlencode: AbC

dEf ghi}} gives

"AbC%0A%0AdEf+ghi".

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


{{anchorencode: AbC

dEf ghi}} gives

"AbC.0A.0AdEf_ghi".

Thus inner newlines become .0A and inner spaces become _.


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

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


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

Conditional table rows

In the case of HTML table code with conditional table rows, with newlines between row codes, if two or more consecutive rows are absent there are three or more newlines in the expanded code, giving one or more extra blank lines above the table. The remedy is to avoid the newlines in the code.

Examples (see edit box):

Extra blank line at the top due to two consecutive absent rows:

pqr


a
b
d
g

No extra blank line at the top in the case of a single absent row:

pqr

a
b
d

No unneeded newlines in the code:

pqr

a
b
d
g

pqr

a
b
d

If wikitable code is used, newlines cannot simply be dispensed with like above.

Empty rows (rendering depends on the browser):

pqr

a
c
f
j

Putting the newlines unconditionally gives excess newlines, which show up as additional vertical space in the rows that are present:

pqr

a


c



f




j

Therefore, a special technique is needed; see Help:Table#Conditional table row.

Alternatives

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

  • &nbsp;
  • <br />

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 below)
Meta | Wikinews | Wikipediahttp://en.wikipedia.org/Help:Newlines_and_spaces | Wikiquote | Wiktionary
What links here on Meta or from Meta | Wikipedia | MediaWiki
Reading
Go | Search | Stop words | URL | Namespace | Page name | Section
Backlinks | Link | Piped link | Interwiki link | Redirect | Category | Image page
Logging in and preferences
Logging in | Preferences | User style
Editing
Advanced editing | Editing FAQ | Edit toolbar | Export | Import | Shortcuts
Tracking changes
Recent changes (enhanced) | Related changes | Watching pages | Diff
Page history | Edit summary | User contributions | Minor edit | Patrolled edit
Style & formatting
Wikitext examples | Reference card | HTML in wikitext | List | Table | Sorting | Colors
Special input and output
Inputbox | Special characters | Displaying a formula | Images (uploads) | EasyTimeline
Advanced functioning
Template | Advanced templates | Parser function | ParserFunctions | Parameter default
Variable | Magic word | System message | Substitution | Array | Calculation
Page management
Starting a new page | Renaming (moving) a page | Protecting pages | Deleting a page
Special pages
Talk page | Testing | Sandbox | CentralNotice

Template:-

Personal tools
support the site