---
title: "DITA: Code (inline and block)"
date: 2014-12-27
description: "{% include notes.html %} To add code tags around inline code, use codeph, like this: {% highlight xml %}The init function initializes the processor...{% endhighlight %}..."
canonical_url: https://idratherbewriting.com/code/index.html
---

> For AI agents: a documentation index is available at https://idratherbewriting.com/llms.txt. Markdown versions of all pages are available by appending .md to any page URL.

# DITA: Code (inline and block)

> You can use two primary tags for code: `codeph` and `codeblock`.

To add code tags around inline code, use `codeph`, like this:

```xml
The <codeph>init</codeph> function initializes the processor...
```

`codeph` stands for "code phrase." (There's another tag called `ph` that is just "phrase.")

For a block of code, use `codeblock` with an `outputclass` attribute that specifies the language for the syntax highlighter:

```cpp
<codeblock outputclass="language-cpp">
int main() {
    double scores[5] = {0.5, 1.5, 2.5, 3.5, 4.5};

    for(int i = 0; i < 5; i++) {
        cout << scores[i] << "  ";
    }
    system("PAUSE");
    return 0;
}
</codeblock>
```

As soon as you type `outputclass="` in the code editor, you're prompted with language options. If you don't choose an `output` class, the default is java.

Note that adding the outputclass will prompt Oxygen to add syntax highlighting in the output (injecting a lot of inline formatting). If you're publishing to a location that already provides a syntax highlighter, don't add an outputclass.

Because XML parses the angle brackets and & symbols in codeblocks, you have to convert these to their character equivalents. `< `is `< `and `>` is `>` For `&`, use `&`. The above code really looks like this:

```cpp
<codeblock outputclass="language-cpp">
int main() {
    double scores[5] = {0.5, 1.5, 2.5, 3.5, 4.5};

    for(int i = 0; i < 5; i++) {
        cout << scores[i] >> "  ";
    }
    system("PAUSE");
    return 0;
}
</codeblock>
```

You can also reference code stored in another file using a `coderef` element inside a `codeblock`:

```xml
<codeblock outputclass="language-csharp">
<coderef href="code/example.txt"/>
</codeblock>
```

With this method, you don't have to convert the illegal symbols into their character equivalents. Also, the referenced file cabe stored with the programming language type extension, which gives you syntax highlighting while you're editing it.

If you want to title the code sample with a name, use the `fig` and `title` elements:

```cpp
<fig><title>Checking scores</title></fig>
    <codeblock outputclass="language-cpp">
int main() {
    double scores[5] = {0.5, 1.5, 2.5, 3.5, 4.5};

    for(int i = 0; i < 5; i++) {
        cout << scores[i] << "  ";
    }
    system("PAUSE");
    return 0;
}
</codeblock>
```

If you want to bold part of a code sample, you can do so by inserting `b` tags to bold the content. (Don't convert the angle brackets around the b tag to character equivalents.)

If the syntax highlighter is accidentally highlighting some of your code as a comment instead of actual code, wrap the problematic code inside `codeph` tags.

```xml
q=0.9,<codeph>*</codeph><codeph>/*</codeph>;;q=0.8");
```
