Safekipedia

Pretty-printing

Adapted from Wikipedia · Discoverer experience

A mathematical expression shown to demonstrate how math can be neatly typeset.

Pretty-printing is a way to make text easier to read and understand. It uses special formatting rules to organize things like source code, markup, and other text files. This can include making sure lines are indented correctly, using colors or different fonts to highlight syntactic elements, and changing sizes so everything looks neat.

When people work with computer code, pretty-printing helps them see patterns and understand what the code does. Tools that do this are sometimes called code formatters or beautifiers. They take messy or hard-to-read code and turn it into something that looks clean and tidy.

Using pretty-printing is important because it makes it easier for anyone reading the code to follow along. Whether you are learning to code or working with others, having well-formatted code can save time and reduce mistakes.

Pretty-printing mathematics

Pretty-printing helps show math problems in a way that looks neat and clear, just like in a real math book. Computers and calculators can change math symbols so they look better on the screen. For example, instead of showing "x ^ 2 + 3 * x", they might show it as a tidy "x2 + 3x".

A typeset mathematical expression

Some special calculators, like the Casio 9860 series, HP-49/50 series, HP Prime, TI-84 Plus, TI-89, TI-Nspire, and TI-83 Plus with extra tools, can also make math look nicer. Even newer scientific calculators with color screens can do this well. Programs like TeX are made just to make math look beautiful in printed work.

Pretty-printing markup and tag-based code

Pretty-printing helps make markup languages easier to read by adding spaces and line breaks to show how tags are nested inside each other. Even though different markup languages work in similar ways, how they handle spaces can vary a lot.

For example, in MathML, extra spaces don’t change the meaning of the content. But in HTML, spaces between tags are treated as part of the content. This means when we make HTML look nicer, we have to be extra careful to avoid adding or removing parts of the content by mistake. It’s simpler with MathML, but HTML needs more complex methods to keep the formatting consistent and correct. Tools like Pretty Diff use special steps to beautify HTML and similar languages.

Formatting of program source code

Programmers follow special styles when writing code to make it easier to read and understand. These styles include how they indent lines, add spaces, and place special characters like braces.

There are tools called beautifiers or code formatters that can automatically change the look of code to follow a chosen style. These tools read the code, understand its parts like loops and conditions, and then display it in a neat, organized way. Some of these tools work alone, while others are built into programs that help write code, like text editors and special coding environments. For example, the Emacs editor can be set to format code in specific ways depending on the programming language being used.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Pretty-printing, available under CC BY-SA 4.0.

Images from Wikimedia Commons. Tap any image to view credits and license.