Jump to content

Indentation (typesetting)

From Wikipedia, the free encyclopedia

Template:Short description {{#invoke:redirect hatnote|redirect|1}} Template:Use dmy dates Template:Sister project

In the written form of many languages, indentation describes empty space (white space) used before or around text to signify an important aspect of the text such as:

Many computer languages use block indentation to demarcate blocks of source code.

Indentation is essentially the same regardless of whether the writing system is left-to-right (e.g. Latin and Cyrillic) or right-to-left (e.g. Hebrew and Arabic) when considering line beginning and end. For example, indenting at the beginning of a line means on the left for a left-to-right script and on the right for right-to-left script.

Indent is both a noun and a verb. The verb is the act of formatting text to be indented whereas the noun refers to the resulting empty space.

Types

There are three main types of indentation: first-line, hanging and block.

Each example below is in a box that represents the page boundary and uses the common typesetting lorem ipsum content. The width of indentation here is in units of em spaces.

Template:Anchors For first-line indentation the first line of a paragraph is indented, Template:Unichar

A first-line indentation of 2 em:

Template:Anchor For hanging indentation all but the first line of a paragraph is indented.

A hanging indentation of 2 em:

Template:Anchor For block indentation the entire paragraph is indented relative to the preceding paragraph.

A block indentation of 2 em on the left:

Block indentation on both sides is commonly used for a quotation, a block quotation, here shown with 2 em on the left and right but since it is not right-aligned, the empty space on the right varies based on line length:

Here's the same as the previous but justified (left and right aligned):

Indentation in programming

{{#invoke:Labelled list hatnote|labelledList|See also|ifexists=true}}

In computer programming, indentation describes formatting source code with whitespace to the left of code text Template:Endash often to visually show that a sequence of code lines is syntactically a code block. Typically, the lines of a block are aligned with an amount of white space that indicates the block's depth in the hierarchical structure of the code. Each inner level of the hierarchy is indented by a multiple of this indentation width.

White space in code is typically stored as whitespace characters.

For a free-form language, indentation is exclusively for the programmer since a code processor (i.e. compiler, interpreter) ignores whitespace characters. Code can have inconsistent or even no indentation, but in general is formatted with somewhat consistent indentation.

Some languages rely on indentation to demarcate block structure, often via the off-side rule. Due to this syntax requirement, the code must have a level of consistency that is not required in free-form language code.

The neologisms outdent, unindent and dedent describe the opposite of indentation Template:Endash aligning code text of a line to the left of the previous line.

Variations

Common variations in the implementation of indentation include: how much to indent a block at each level of the code hierarchy, usually measured in spaces, and whether to store whitespace characters as space or tab characters. Although there are common practices, consensus is not universal.[1] These variations are driven by factors that may include but are not limited to: language syntax, organizational mandate and personal preference.

Common practices

The following table identifies notable practices with respect to code indentation.

Language Free-form? Note
Bash Template:Yes Fritz Mehner's style guide suggests 2, 4, or 8 spaces and uses 2 in all examples[2]

Google uses 2 spaces[3]

C Template:Yes The Linux kernel uses 1 tab[4][5]

NASA uses 4 spaces.[6]

Clinton Staley advocates 3 spaces[7]

C++ Template:Yes WebKit recommends 4 spaces[8]

Google uses 2 spaces[9]

C# Template:Yes Microsoft convention: 4 spaces[10]
CSS Template:Yes Drupal, GitHub, and Google use 2 spaces[11][12][13][14]

WordPress uses tabs[15]

Dart Template:Yes dart format uses 2 spaces
Delphi Template:Yes Delphi style guide: 2 spaces[16]
F# Template:Yes F# style guide: 4 spaces[17]
Go Template:Yes Effective Go: 1 tab[18]
Haskell Template:No Google uses 2 spaces[19]
HTML Template:Yes Google uses 2 spaces[13]

HTML Tidy defaults to 2 spaces[20]

Java Template:Yes Oracle uses 4 spaces[21]

Android uses 4 spaces[22]

Most Eclipse IDE components use tabs

JavaScript Template:Yes Douglas Crockford advocates 4 spaces[23]

GitHub and Google use 2 spaces[24][25]

jQuery uses tabs[26]

Firefox's built-in jsbeautifier defaults to 2 spaces

prettyprinter in Google Chrome and Internet Explorer use 4 spaces

Julia Template:Yes Julia style guide: 4 spaces[27]
Kotlin Template:Yes Kotlin style guide: 4 spaces[28]
Lua Template:Yes Lua style guide: 2 spaces[29]
Perl Template:Yes Larry Wall prefers 4 spaces[30]
PHP Template:Yes Drupal use 2 spaces[31]

PEAR and Zend use 4 spaces[32][33]

CodeIgniter and WordPress use tabs[34][35]

PSR-2 specifies 4 spaces[36]

PowerShell Template:Yes Unofficial PowerShell Best Practices and Style Guide: 4 spaces[37]
Python Template:No PEP 8: 4 spaces[38]
Ruby Template:Yes Ruby style guide: 2 spaces[39]
Rust Template:Yes Rust style guide: 4 spaces[40]
Scala Template:Yes Scala style guide: 2 spaces[41]
Swift Template:Yes Google uses 2 spaces[42]
Tcl Template:Yes Tcl style guide: 4 spaces[43]
Visual Basic Template:Yes Microsoft convention: 4 spaces[44]
XML Template:Yes Google uses 2 spaces[45]

Elastic tabstops

In 2006, a new method of indentation was proposed, called elastic tabstops.[46][47][48][49]

References

Template:Reflist

Template:Commons cat

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  3. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  7. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  8. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  9. ↑ [1] Template:Webarchive
  10. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  11. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  12. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  13. ↑ 13.0 13.1 {{#invoke:citation/CS1|citation |CitationClass=web }} This is an HTML-rendered copy of the official version, which is maintained in XML format
  14. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  15. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  16. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  17. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  18. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  19. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  20. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  21. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  22. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  23. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  24. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  25. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  26. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  27. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  28. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  29. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  30. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  31. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  32. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  33. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  34. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  35. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  36. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  37. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  38. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  39. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  40. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  41. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  42. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  43. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  44. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  45. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  46. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  47. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  48. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }} 111 comments.
  49. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}