Are there specialized elements for including code samples in DITA?

DITA provides specialized elements for including code samples within topics. This feature is essential for technical documentation, software manuals, or any content that requires the presentation of code snippets.

These elements include the <code>, <codeblock>, and <codeph> elements.

<code> Element:

The <code> element is a versatile inline element used for marking up code samples in DITA. It is often used for short code phrases or single keywords, providing a simple way to differentiate code elements within text.

<codeblock> Element:

The <codeblock> element is a commonly used element for presenting code samples in DITA topics. It allows for the inclusion of code snippets, programming examples, or any text that should be displayed in a monospaced font, preserving formatting and indentation.

Attributes:

  • @outputclass: Specifies a class to define the type of code content (e.g., “language-java” or “language-python”).
  • @language: Identifies the programming language of the code.
  • @linenumbering: Enables line numbering.

<codeph> Element:

The <codeph> element is used to mark individual code phrases or inline code within regular text. This is helpful for highlighting code elements within the flow of a paragraph.

Example:

Some software documentation uses the <codeblock> and <codeph> elements to include code samples in a DITA topic.


<codeblock outputclass="language-python">print("Hello, World!")</codeblock> <codeph>for (i = 0; i < n; i++)</codeph>

In this example, the <codeblock> element is used to encapsulate the Python code snippet, and the <code@outputclass> attribute specifies the code language. The <codeph> element is employed to highlight the code phrase “for (i = 0; i < n; i++)” within the paragraph text.