Printing

Add dynamic, computed output to a document with doc.print().

Basic printing

doc.print() works like Python’s built-in print(). It echoes to your terminal and appends the same text to the document as a code block.

with doc:
    doc.print("Hello, World!")
    doc.print("The answer is", 42)

Generated Markdown:

```
Hello, World!
The answer is 42
```

You pass any number of positional arguments, and each one is converted with str(). Multiple arguments are joined with a space by default.

Format strings

Use f-strings to print computed values. This is the common case: capture a result inside a with doc: block and report it.

with doc:
    total = sum([1, 2, 3, 4, 5])
    doc.print(f"Sum: {total}")
    doc.print(f"Mean: {total / 5:.2f}")

Generated Markdown:

```
Sum: 15
Mean: 3.00
```

Controlling sep and end

doc.print(*args, sep=" ", end="\n") accepts the same sep and end keywords as the built-in.

sep sets the string placed between arguments. end sets the string appended after the last argument.

with doc:
    doc.print("a", "b", "c", sep=" | ")
    doc.print("no newline here", end="")
    doc.print(" — same line")

Generated Markdown:

```
a | b | c
no newline here — same line
```

Coalescing consecutive prints

Consecutive doc.print() calls merge into a single code block. CMX appends each call’s text to the previous Print block instead of opening a new fence, so a run of prints reads as one continuous output stream.

This is why a loop produces one tidy block. Set end=" " to build a single line:

with doc:
    for i in range(5):
        doc.print(i, end=" ")

Generated Markdown:

```
0 1 2 3 4 
```

The merge only spans an unbroken run of prints. Any other content between them — a doc @ line, a table, an image — closes the current block, and the next doc.print() starts a fresh one.

with doc:
    doc.print("first block")
    doc @ "Some markdown in between."
    doc.print("second block")

Generated Markdown:

```
first block
```
Some markdown in between.
```
second block
```

Note

Coalescing also applies across separate with doc: blocks, as long as no other element is appended between the prints.

Next steps

  • Markdown — Add headings and prose with doc @, |, and the call form.

  • Context — Control what runs and what shows with with doc:, doc.hide, and doc.skip.