Indentation (typesetting)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search


In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or other logical blocks in the program.

For example, the following lines are indented, using between one and six spaces:

 This paragraph is indented by 1 space.

   This paragraph is indented by 3 spaces.

      This paragraph is indented by 6 spaces.

In computer programming, the neologisms outdent, unindent and dedent are used to describe the reversal of the indentation process, realigning text with the page margin (or with previous, lesser, levels of indentation).

In right-to-left languages (e.g. Hebrew and Arabic), indentation is used just the same, but from the right margin of the paper, where the line begins.

Indentation in typesetting[edit]

There are three main types of indentation, illustrated below in relation to borders representing the page dimensions.

A first-line indentation indents only the first line of each paragraph.
A first-line indentation of 2 em:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

 
A hanging indentation indents the remaining lines of each paragraph while leaving the first line of each paragraph in place.
A hanging indentation of 2 em:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

A block indentation indents the entire block of text.
A block indentation of 2 em on the left:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Indentation from both sides is commonly used for block quotations, here shown with 2 em on the left and right (which may amount to more on the right for certain lines, depending on word wrapping, if the text does not have justified alignment):

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

A justified-text example of a block quotation that has been block-indented from both sides by 3 em:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Indentation in programming[edit]

In computer programming languages, indentation is used to format program source code to improve readability. Indentation is generally only of use to programmers; compilers and interpreters rarely care how much whitespace is present in between programming statements. However, certain programming languages rely on the use of indentation to demarcate programming structure, often using a variation of the off-side rule. The Haskell, Occam, Python, MoonScript, and Ya programming languages rely on indentation in this way.

Opinions about where to indent, whether to use spaces or tabs, and how many spaces to use are often hotly debated among programmers, leading some to describe indentation disputes as akin to a religious war.[1] In 2006 a third method of indentation was proposed, called elastic tabstops.

In addition to general indentation of statements, different bracket indentation styles are commonly used.

Language Indentation Note
Bash varies Fritz Mehner's Style Guide[2] suggests 2, 4, or 8 spaces and uses 2 in all examples. Google uses 2 spaces.[3]
C varies The Linux kernel uses 1 tab.[4][5] NASA uses 4 spaces.[6] Clinton Staley advocates 3 spaces.[7]
C++ varies WebKit recommends 4 spaces.[8] Google uses 2 spaces.[9]
C# 4 spaces Per Microsoft's C# Coding Conventions.[10]
CSS varies Drupal, GitHub, and Google use 2 spaces.[11][12][13][14] WordPress uses tabs.[15]
Dart dart format uses 2 spaces.
Delphi 2 spaces Default in RAD Studio.[citation needed]
F# 4 spaces As per the F# style guide.[16]
Go 1 tab Per "Effective Go".[17]
Haskell varies Google uses 2 spaces.[18]
HTML varies Google uses 2 spaces.[13] HTML Tidy defaults to 2 spaces.[19]
Java varies Oracle uses 4 spaces.[20] Android uses 4 spaces.[21] Most Eclipse IDE components use tabs.
JavaScript varies Douglas Crockford advocates 4 spaces.[22] GitHub and Google uses 2 spaces.[23][24] jQuery uses tabs.[25] Firefox's built-in jsbeautifier defaults to 2 spaces. The built-in prettyprinter in Google Chrome and Internet Explorer uses 4 spaces.
Julia 4 spaces Per Julia style guide.[26]
Kotlin 4 spaces Per Kotlin style guide.[27]
Lua 2 spaces Per Lua Style Guide.[28]
Perl varies "perlstyle: Perl style guide" says Larry Wall prefers but does not mandate 4 spaces.[29]
PHP varies Drupal use 2 spaces.[30] PEAR and Zend use 4 spaces.[31][32] CodeIgniter and WordPress uses tabs.[33][34] PSR-2 specifies 4 spaces.[35]
PowerShell varies 4 spaces in The Unofficial PowerShell Best Practices and Style Guide.[36]
Python 4 spaces Per PEP-8.[37] (1 tab is also sometimes used, but 4 spaces is preferred.)
Ruby 2 spaces Per Ruby Style Guide.[38]
Rust 4 spaces Per Rust Style Guide.[39]
Scala 2 spaces Per Scala style guide.[40]
Swift varies Google uses 2 spaces.[41]
Tcl 4 spaces Per Tcl style guide.[42]
Visual Basic 4 spaces Per Microsoft's Visual Basic Coding Conventions.[43]
XML varies Google uses 2 spaces.[44]

References[edit]

  1. ^ "Tabs versus Spaces: An Eternal Holy War". Jwz.org. 5 January 2007. Retrieved 18 January 2014.
  2. ^ "Bash Style Guide and Coding Standard" (PDF). Lug.fh-swf.de.
  3. ^ "Archived copy". Archived from the original on 26 June 2014. Retrieved 23 June 2014.{{cite web}}: CS1 maint: archived copy as title (link)
  4. ^ "Archived copy". Archived from the original on 14 February 2015. Retrieved 15 February 2015.{{cite web}}: CS1 maint: archived copy as title (link)
  5. ^ "Linux kernel coding style". Archived from the original on 11 June 2022. Retrieved 21 July 2022.
  6. ^ "C STYLE GUIDE". NASA Technical Reports Server. August 1994.
  7. ^ Staley, Clinton (2006). "C Program Style Rules". Cal Poly Department of Computer Science & Software Engineering.{{cite web}}: CS1 maint: url-status (link)
  8. ^ "Code Style Guidelines". WebKit. 7 November 2015. Retrieved 31 December 2021.
  9. ^ [1] Archived 2014-10-06 at the Wayback Machine
  10. ^ Bill Wagner. "C# Coding Conventions (C# Programming Guide)". Msdn.microsoft.com. Retrieved 3 September 2017.
  11. ^ "CSS formatting guidelines". Drupal.org. 14 January 2013. Retrieved 18 January 2014.
  12. ^ "CSS · Styleguide · GitHub". Github.com. Retrieved 18 January 2014.
  13. ^ a b "Google HTML/CSS Style Guide". Google GitHub. Indentation. Archived from the original on 13 March 2016. Retrieved 13 March 2016. Indent by 2 spaces at a time. This is an HTML-rendered copy of the official version, which is maintained in XML format
  14. ^ "styleguide: Style guides for Google-originated open-source projects". Github.com. 3 September 2017. Retrieved 3 September 2017.
  15. ^ "WordPress › CSS Coding Standards « Make WordPress Core". Make.wordpress.org. 17 July 2012. Retrieved 20 July 2018.
  16. ^ "F# code formatting guidelines". docs.microsoft.com. Retrieved 14 October 2020.
  17. ^ "Effective Go – The Go Programming Language". golang.org. Retrieved 3 September 2017.
  18. ^ "HaskellStyleGuide – ganeti – Style Guide for the Haskell code – Cluster-based virtualization management software – Google Project Hosting". Code.google.com. 8 January 2014. Archived from the original on 27 June 2013. Retrieved 18 January 2014.
  19. ^ "HTML Tidy Configuration Options Quick Reference". Tidy.sourceforge.net. 18 June 2008. Retrieved 18 January 2014.
  20. ^ "Code Conventions for the Java Programming Language: 4. Indentation". Oracle.com. Retrieved 18 January 2014.
  21. ^ "Code Style Guidelines for Contributors | Android Developers". Source.android.com. Retrieved 18 January 2014.
  22. ^ "Code Conventions for the JavaScript Programming Language". Javascript.crockford.com. 13 November 2006. Retrieved 18 January 2014.
  23. ^ "JavaScript 路 Styleguide". Github.com. Archived from the original on 15 August 2013. Retrieved 18 January 2014.
  24. ^ "JaveScript Style Rules". Google-styleguide.googlecode.com. Archived from the original on 16 February 2014. Retrieved 18 February 2014.
  25. ^ jQuery Foundation - jquery.org. "JavaScript Style Guide | Contribute to jQuery". Contribute.jquery.org. Retrieved 18 January 2014.
  26. ^ "Style Guide · The Julia Language". docs.julialang.org.
  27. ^ "Coding Conventions – Kotlin Programming Language". Kotlin.
  28. ^ "lua-users wiki: Lua Style Guide". lua-users.org. Retrieved 3 September 2017.
  29. ^ Allen, Jon (JJ) (ed.). "perlstyle: Perl style guide". Perl 5 Version 32.1 Documentation. Perl Foundation. Retrieved 19 April 2021.
  30. ^ "Coding standards". Drupal.org. Retrieved 18 January 2014.
  31. ^ "Manual :: Indenting and Line Length". Pear.php.net. Retrieved 18 January 2014.
  32. ^ "PHP Coding Standard (draft) – Contributors – Zend Framework Wiki". Framework.zend.com. Archived from the original on 3 January 2008. Retrieved 18 January 2014.
  33. ^ "Style Guide : CodeIgniter User Guide". Ellislab.com. Retrieved 18 January 2014.
  34. ^ "WordPress › PHP Coding Standards « Make WordPress Core". Make.wordpress.org. 23 July 2012. Retrieved 18 January 2014.
  35. ^ "PHP : Coding Style Guide". Php-fig.org. Archived from the original on 15 March 2014. Retrieved 18 February 2014.
  36. ^ "Code Layout and Formatting – PowerShell Practice and Style". PowerShell Practice and Style. Retrieved 17 October 2021.
  37. ^ "PEP 8 – Style Guide for Python Code". Python.org. Retrieved 3 September 2017.
  38. ^ Batsov, Bozhidar (3 September 2017). "ruby-style-guide: A community-driven Ruby coding style guide". Github.com. Retrieved 3 September 2017.
  39. ^ "rust: A safe, concurrent, practical language". Github.com. 3 September 2017. Retrieved 3 September 2017.
  40. ^ "Indentation – Scala Documentation". docs.scala-lang.org. Retrieved 3 September 2017.
  41. ^ "Swift Style Guide". google.github.io. Retrieved 17 October 2021.
  42. ^ Ray Johnson. "Tcl Style Guide" (PDF). Tcl.tk. Retrieved 3 September 2017.
  43. ^ "Structured Coding Conventions". msdn.microsoft.com. Retrieved 3 September 2017.
  44. ^ "Google XML Document Format Style Guide". google.github.io. Google. Retrieved 17 October 2021.