Sage cells can be used for Python examples, but Sage uses a mild amount of pre-parsing, so that might not be a wise decision, especially in instructional settings. We might implement Skulpt or Brython (in-browser Python) or the Python language argument to the Sage Cell Server. To see examples of authoring Sage cells, have a look at Section 3.

In the meantime, program listings, especially with syntax highlighting, is useful all by itself. The “R” language might not be a bad stand-in for pseudo-code, as it supports assignment with a left arrow and has fairly generic procedural syntax for control structures and data structures. Or maybe Pascal would be a good choice? Here is an example of R. Note in the source that the entire block of code is wrapped in a CDATA section due to the four left angle brackets. We do not recommend this technique for isolated problem characters, but it is a life-saver for situations like the XSLT code just following.

n_loops <- 10
x.means <- numeric(n_loops) # create a vector of zeros for results
for (i in 1:n_loops){
x <- as.integer(runif(100, 1, 7)) # 1 to 6, uniformly
x.means[i] <- mean(x)
}
x.means

You can write made-up pseudo-code and get reasonable syntax highlighting, but you might explain to a reader what your symbols all mean. This routine takes the \(m\times n\) marix \(A\) to reduced row-echelon form. Note in the source the use of escaped characters for the four angle brackets.

input m, n and A
r := 0
for j := 1 to n
i := r+1
while i <= m and A[i,j] == 0
i := i+1
if i < m+1
r := r+1
swap rows i and r of A (row op 1)
scale A[r,j] to a leading 1 (row op 2)
for k := 1 to m, k <> r
make A[k,j] zero (row op 3, employing row r)
output r and A

Look in the mathbook-common.xsl file to see the strings to use to identify languages. Always all-lowercase, no symbols, no punctuation.

Note that the above examples all have slightly different widths (theser are very evident in print with the frames). As 2-D atomic objects, to place them in the narrative requires the layout features of a sidebyside element. Then width and/or margin attributes will influence the width of the panel.

A program may also be nested inside a listing, which behaves similar to a figure. You can provide a caption, and the listing will be numbered along with tables and figures. This then makes it possible to cross-reference the listing, such as Listing 20.1. It also removes the requirement of wrapping the program in a sidebyside. For technical reasons, the three examples above will not split across a page break in PDF output, but the placement inside a listing will allow splits, as you should see in at least one example following.

If you are discussing algorithms in the abstract (or even concretely), you can set them off like a theorem, with a number, a title and a target for cross-references. Sometimes you claim an algorithm produces something in particular, or has certain properties, such as a theoretical run time, so a proof may be included. See the discussion just preceding about (limited) options for pseudo-code.

Algorithm20.2Sieve of Eratosthenes

On input of a positive integer n this algorithm will compute all the prime numbers up to, and including, n. It was named for Eratosthenes of Cyrene (c. 276 BC–c. 195/194 BC) by Nicomachus (c. 60–c. 120 CE) in Introduction to Arithmetic. (Wikipedia, 2015)

Input: n

Form the list of all integers from 2 to n

Set p = 2

While p < sqrt(n)

If present, remove from the list multiples 2p, 3p, ...

If p is now the last element of the list, stop

Otherwise, set p to the element of the list immediately after current p

Any element removed is a non-trivial product of two integers and hence composite. So no prime is is ever removed from the list.

Each composite number is a multiple of some prime, and since no prime is ever removed, each composite will be removed. Hence the removed elements are precisely the set of composite numbers in the list and thus the remainder are precisely the primes on the list.

If you are writing about system-level software, you may need to write numbers in hexadecimal or binary. Here we use a numbered, displayed equation (mathematics) and LaTeX macros such as \texttt for a monospace text font, and \; for spacing/grouping the bits of the binary number.

If you use these constructions repeatedly, then some LaTeX macros might be useful. It might also be beneficial for us to add some PreTeXt markup for such numbers used in a paragraph—send us a feature request.

Theorem20.3

This is a spurious theorem to break up the run of consecutive listing so we might test the effect.

This is a proof that is authored “detached.” It is not associated with the theorem above in a way other than simply following it.

A specialized version of a program listing is an interactive command/response session at a command-line, where differing fonts are used to differentiate the system prompt, the user's commands, and the system's reaction. A console session may be used by itself inside a sidebyside, or it can be wrapped in a listing to get a number and a caption. As elsewhere, you will need to escape ampersands and angle brackets (such as if you have a command using redirection), using &, <, and > in your source.

Here is the plain version, placed inside a sidebyside for layout control. We simply place a small margin on the left (at 4%).

pi@raspberrypi ~/progs/chap02 $ gcc -Wall -o intAndFloat intAndFloat.c
pi@raspberrypi ~/progs/chap02 $ ./intAndFloat
The integer is 19088743 and the float is 19088.742188
pi@raspberrypi ~/progs/chap02 $

If your console input exceeds more than one line, you can author it across several lines and your choice of line breaks will be reflected in the rendering. You can decide to indent lines after the first one for clarity, if desired. You can also decide if your audience needs line-continuation characters or not. (But be careful, a backslash, “\,” will require you to define the latex.console.macro-char xsltproc stringparam to something else, as explained below.)

Notice in the HTML version of the above example that when you highlight all, or a portion, of the listing for a cut&paste that the prompts are not included.

There is one subtlety with a console session rendered as LaTeX output. The user input is made bold by a LaTeX macro, which means that your code cannot contain the special LaTeX characters “\”, “{”, and “}”, which are used to begin a macro, begin a group, and end a group (respectively). You will get an error message if this condition exists, and there are parameters latex.console.macro-char, latex.console.begin-char, and latex.console.end-char that will allow you to specify alternatives (which need to be characters that do not appear in any of your console sessions, document-wide). The characters & % $ # _ { } ~ ^ \ have special meaning in LaTeX but should be available for duty as these alternative characters (though not all have been tested). The backslash used in pathnames for Windows is a highly likely case where this needs adjustment.

There is no good way to provide an example of this situation, without making a document with an error in it, out-of-the-box. So experiment by using --stringparam on the xsltproc command-line with alternative characters that will behave with the example above, and with characters that will cause the example above to raise errors. In practice, you may want to specify alternative characters in a thin XSL extension file specific to your project.

We conclude with a longer example, an assembly language program from Bob Plantz, included to test a listing breaking across pages in PDF output.

In HTML output, a program can be interactive. This is an example program provided by Python Tutor.