<pre>: In what situations is the <pre> element used for preformatted text in DITA, and how does it differ from <codeblock>?

Preformatted Text with <pre>

In DITA, the <pre> element is used for presenting preformatted text, typically in situations where line breaks and spacing are critical and should be preserved exactly as entered. This element is useful for displaying code snippets, poetry, ASCII art, or any content where formatting is essential.

Differences from <codeblock>

While both <pre> and <codeblock> elements are used for presenting code or preformatted text, they serve slightly different purposes.

  • Preservation of Line Breaks: The primary distinction is that the <pre> element is designed to preserve line breaks and spacing exactly as they are in the source code, while <codeblock> may apply additional formatting or syntax highlighting based on the specified language.
  • Language Specification: In contrast to <codeblock>, which uses the outputclass attribute to specify the programming language, <pre> does not provide language specification. It is typically used for content where language highlighting or specific formatting is not required.

Example:

Here’s an example of using the <pre> element to present a block of preformatted text:


<pre>
This is an example of preformatted text
with preserved line breaks and spacing.
    Indentation is also maintained.
</pre>

In this example, the <pre> element is used to display preformatted text exactly as it appears, preserving line breaks, spacing, and indentation.