Pretty-printing
Pretty-printing is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions may entail adhering to an indentation style, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.
Pretty-printing mathematics
Pretty-printing usually refers to displaying mathematical expressions similar to the way they would be typeset professionally. For example, in computer algebra systems such as Maxima or Mathematica the system may write output like as Some graphing calculators can perform pretty-printing. Such calculators include the Casio 9860 series, HP-49/50 series and HP Prime, TI-84 Plus, TI-89, TI-Nspire, and the TI-83 Plus with the PrettyPt add-on. Similarly, the TI-84 Plus with the PrettPt add-on or with "MathPrint"-enabled operating systems. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series, Sharp EL-W series, HP SmartCalc 300s, TI-30XB, and Numworks.Many text formatting programs can also typeset mathematics: TeX was developed specifically for high-quality mathematical typesetting.
Pretty-printing markup and tag-based code
Pretty-printing in markup language instances is most typically associated with indentation of tags and string content to visually determine hierarchy and nesting. Although the syntactical structures of tag-based languages do not significantly vary, the indentation may vary significantly due to how a markup language is interpreted or due to the data it describes.In MathML, whitespace characters do not reflect data, meaning, or syntax above what is required by XML syntax. In HTML, whitespace characters between tags are considered text and are parsed as text nodes into the parsed result. While indentation may be generously applied to a MathML document, sufficient additional care must be taken in pretty-printing an HTML document to ensure additional text nodes are not created or destroyed in general proximity to the content or content-reflective tag elements. This difference in complexity is non-trivial from the perspective of an automated pretty-print operation where no special rules or edge cases are necessary, as in the more simple MathML example. The HTML example may require a series of progressive interrelated algorithms to account for various patterns of tag elements and content that conforms to a uniform style and is consistent in application across various instances, as evidenced by the markup.ts application component used to beautify HTML, XML, and related technologies for the Pretty Diff tool.
Formatting of program source code
Programmers are taught to adhere to a formatting style when writing program source code, to make code easier to read and understand. Styles vary. They cover matters such as indentation, whitespace surrounding keywords and punctuation marks, and positioning of braces.Tools exist to automate formatting. They are called beautifiers, code formatters, or code indenters. These tools convert source code from one format style to another. The implementation of such tools is relatively straightforward, because the syntax of programming languages is unambiguous. The tool reads the source code, recognizes the code's component structures, then displays the source code, formatted in the style specified in a configuration file.
Some code beautifiers are standalone applications. Others are built into text editors and integrated development environments. For example, the Emacs editor has settings to specify a programming language and to indent blocks of code accordingly.
HTML
Lisp pretty-printer
An early example of pretty-printing was Bill Gosper's "GRINDEF" program, which used combinatorial search with pruning, to format LISP programs. Early versions operated on the executable form of the Lisp program and were oblivious to the special meanings of various functions. Later versions had special read conventions for incorporating non-executable comments and also for preserving read macros in unexpanded form. They also allowed special indentation conventions for special functions such asif. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.Project style rules
Many free software projects have established rules for code layout. The most widely known arethe GNU formatting
and the BSD style.
The biggest difference between the two is the positioning of braces: in the GNU style, each brace is placed on a line by itself, and matched pairs are indented to the same level. The BSD style places an opening brace at the end of a line of code, and allows the closing brace to be followed by an else keyword. The size of indent and location of whitespace also differs.
Example of formatting and beautifying code
The following example shows some typical C structures and how various indentation style rules format them. Without any formatting at all, it looks like this:int foo
The GNU indent program produces the following output when asked to indent according to the GNU rules:
int
foo
It produces this output when formatting according to BSD rules:
int
foo