Correct Method For Indented *Pseudocode*

109 views
Skip to first unread message

JACOB DENSON

unread,
Jan 23, 2025, 3:45:17 PM1/23/25
to PreTeXt support
I'm currently editing together some questions for a computing science course into a PreTeXt document. Some of the questions involve pseudocode. What is the recommended way to write pseudocode in a PreTeXt document, especially in regards to pseudocode in which *indentation* is necessary (i.e. for and while loops). A cd tag is not viable since I would like to include mathjax in the pseudocode, and the algorithm documentation does not seem to indicate support for indentation.

Rob Beezer

unread,
Jan 23, 2025, 4:02:40 PM1/23/25
to pretext...@googlegroups.com
There is no support for a mix of verbatim text and math text, nor do I think that is a good idea.

You might study the source for:

Algorithm 24.4: Sieve of Eratosthenes
https://pretextbook.org/examples/sample-article/html/section-programs.html#algorithm-sieve-eratosthenes

which would support some math in the steps.

To answer your question, a #program will respect indentation, and you can use Unicode math symbols in some sort of code-comment pseudo-syntax, but pity your reader using braille.

A #cd can be structured as a #cline, which should respect leading whitespace. See parts of

Section 23: Pre-Formatted Text
https://pretextbook.org/examples/sample-article/html/section-pre-formatted.html

Rob

JACOB DENSON

unread,
Jan 23, 2025, 4:47:02 PM1/23/25
to PreTeXt support
My apologies I wasn't clear. I don't want to write any verbatim code at all, only pseudocode written in plain text with math jax. For the Sieve of Eratosthenes example I'd hope the display would look something like the text below, in particular, without the renumbering and the indentation issues caused by using a list within a list:

1: Form the list of all integers from 2 to n
2: Set p = 2
3: While p < sqrt(n):
4:     If present, remove from the list multiples 2p, 3p, ...
5:     If p is now the last element of the list, stop
6:     Otherwise, set p to the element of the list immediately after current p
7: Output the remaining elements of the list

where all the math in the example above is formatted using MathJax. The poem division seems to provide this functionality, though it doesn't seem to support line numbering as of now.

D. Brian Walton

unread,
Jan 23, 2025, 5:18:53 PM1/23/25
to pretext...@googlegroups.com
So are you asking for something like indented and nested #ol but without any bullets?

Brian

--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-support/f11be9d6-2f4b-4195-b58e-b066a5b903een%40googlegroups.com.

Rob Beezer

unread,
Jan 23, 2025, 5:22:41 PM1/23/25
to pretext...@googlegroups.com
Yes, a #poem allows for indentation of lines, I guess because poets think the physical layout of words is important. Unlike a "narrative" where a #p is logically one long line of text, which we typically word-wrap.

Perhaps the CSS crew would like to weigh-in on if the poem HTML and CSS needs work (maybe it is adjusted already). Of course LaTeX and braille would need work (basic lists in braille are not even right at the moment).

For my money, I might restrict this to an #ol in a #algorithm. To forestall abuse.

As a workaround until somebody gets fired-up about this, you could try a #c element with just spaces at the start of the #p in your 4,5,6. That would not be good practice, nor a good recommendation.

Rob
Message has been deleted

Andrew Scholer

unread,
Jan 23, 2025, 5:45:34 PM1/23/25
to pretext...@googlegroups.com
Indentation on poems looks like it is working fine:

Maybe algorithm//ol/li could be allowed to have @indent similar to poem/line? (And leverage the same code)

Another temporary hack that would be easy to fix later would be to use the entity code for a non-breaking space:
&#160;

Put a few of those before the text you want to indent. That will work in both HTML and PDF output.

Andrew Scholer (he/him/his)
Computer Science Instructor
Chemeketa Community College


On Thu, Jan 23, 2025 at 2:23 PM 'JACOB DENSON' via PreTeXt support <pretext...@googlegroups.com> wrote:
Using <ol> as in the example Rob mentioned has two problems in the context I am working in: 

Problem 1: the number of the indented list does not continue from the numbering of the original list (so that 4: in my last post would be listed as 1.)
Problem 2: the numbering of the indented list is also indented, whereas I would prefer the position of the numbering to be consistent between non-indented and indented lines.

The <poem> division seems to provide solutions to these two problems, except it doesn't seem to have line numbering support.

Charilaos Skiadas

unread,
Jan 23, 2025, 6:11:00 PM1/23/25
to pretext...@googlegroups.com
I have to say I find the idea of representing pseudocode for a CS book using a <poem> construct to be somewhat funny.
But I really feel that is not the way to go. The PreText tags are, at least in my opinion, first and foremost semantic constructs, that indicate what their contents mean in complete isolation with what the visual appearance of those contents in a certain medium might be. A poem tag suggests exactly that the contents are to be a poem, and a program tag suggests exactly that the contents are to be a program. What you have is a program and using anything else feels to me fundamentally wrong.

The other related part is to consider that the PreText source is meant to be consumed in other ways beyond a print or web form. The fact that some of the lines in that algorithm are to be indented is something that ideally would be communicated in the syntax. Admittedly the way to do that at the moment is not ideal, by using spaces in a verbatim context where spaces are considered significant. I don’t even want to imagine how a screen reader would read that block, but I don’t suppose it would be understandable. But that may all be too big of a slice to address here, and it’s a general problem with all code blocks I think. But what I am getting that it so keep in mind that the design of PreText is to consider these other forms of consuming the document and attempting to provide something that would work in all, braille being one of those particularly hard to think about for those of us who are not visually impaired.

I feel that expecting the program/input tags to treat math content in them in some appropriate way is a dangerous path, even if it could be done. I would tend to expect such blocks to show you exactly the code you might use to produce a math content, so I am not sure where I would start there.

Spitballing here, but I wonder if what we might do is have an actual “pseudocode” tag, to be used in place of program, and where:

- whitespace and newlines are treated literally
- numbering of the lines can be enabled
- math syntax is allowed and properly converted, but expected to be inline, probably?
- perhaps one or two more tags allowed, to allow indicating a keyword (to be bold), variable (italic?) etc?

Rendered in HTML I would expect that to be a non-verbatim block that uses mono-spaced font, treats each line as an isolated item, and has some way to support indenting, possibly by using white-space: pre; on the CSS side. But this is really just throwing ideas out, really.
And I understand that adding a new construct is not a small thing. But I really would not want to see poem used for pseudocode (unless it’s pseudocode that rhymes, of course).

Jacob, out of curiosity, is your pseudocode so math-complex that the simple text form you presented for the sieve would not be applicable? I guess what I am suggesting, at the risk of butting in and without knowing the particulars of what you are working with, is that if I were to be writing pseudocode for a CS course then I would just use the program/verbatim approach, and basically just use “pseudomath” in the pseudocode. But I can see that getting hairy if the math is complex.

Hope I haven’t ruffled too many feathers.

Haris

Charilaos Skiadas
Professor in Mathematics and Computer Science
Hanover College


On Jan 23, 2025, at 5:23 PM, 'JACOB DENSON' via PreTeXt support <pretext...@googlegroups.com> wrote:

Using <ol> as in the example Rob mentioned has two problems in the context I am working in: 

Problem 1: the number of the indented list does not continue from the numbering of the original list (so that 4: in my last post would be listed as 1.)
Problem 2: the numbering of the indented list is also indented, whereas I would prefer the position of the numbering to be consistent between non-indented and indented lines.

The <poem> division seems to provide solutions to these two problems, except it doesn't seem to have line numbering support.
On Thursday, January 23, 2025 at 4:18:53 PM UTC-6 dbrian...@gmail.com wrote:

Rob Beezer

unread,
Jan 24, 2025, 2:42:35 PM1/24/25
to pretext...@googlegroups.com
> Hope I haven’t ruffled too many feathers.

Not at all! To the contrary, you make some great points.

More to say, but it may be a while before I can catch-up.

Rob
>> On Jan 23, 2025, at 5:23 PM, 'JACOB DENSON' via PreTeXt support <pretext-
>> programs.html#algorithm-sieve-eratosthenes <https://
>> pretextbook.org/examples/sample-article/html/section-
>> programs.html#algorithm-sieve-eratosthenes>
>>
>> which would support some math in the steps.
>>
>> To answer your question, a #program will respect indentation, and
>> you can use Unicode math symbols in some sort of code-comment
>> pseudo-syntax, but pity your reader using braille.
>>
>> A #cd can be structured as a #cline, which should respect leading
>> whitespace. See parts of
>>
>> Section 23: Pre-Formatted Text
>> https://pretextbook.org/examples/sample-article/html/section-pre-
>> formatted.html <https://pretextbook.org/examples/sample-article/
>> html/section-pre-formatted.html>
>>
>> Rob
>>
>>
>>
>> On January 23, 2025 10:27:44 AM HST, 'JACOB DENSON' via PreTeXt
>> support <pretext...@googlegroups.com> wrote:
>> >I'm currently editing together some questions for a computing
>> science
>> >course into a PreTeXt document. Some of the questions involve
>> pseudocode.
>> >What is the recommended way to write pseudocode in a PreTeXt
>> document,
>> >especially in regards to pseudocode in which *indentation* is
>> necessary
>> >(i.e. for and while loops). A cd tag is not viable since I would
>> like to
>> >include mathjax in the pseudocode, and the algorithm
>> documentation does not
>> >seem to indicate support for indentation.
>> >
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "PreTeXt support" group.
>> To unsubscribe from this group and stop receiving emails from it, send
>> an email to pretext-suppo...@googlegroups.com.
>> To view this discussion visit https://groups.google.com/d/msgid/
>> pretext-support/f11be9d6-2f4b-4195-b58e-
>> b066a5b903een%40googlegroups.com <https://groups.google.com/d/msgid/
>> pretext-support/f11be9d6-2f4b-4195-b58e-
>> b066a5b903een%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "PreTeXt support" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to pretext-suppo...@googlegroups.com <mailto:pretext-
>> support+u...@googlegroups.com>.
>> To view this discussion visit https://groups.google.com/d/msgid/pretext-
>> support/073ef06c-f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com <https://
>> groups.google.com/d/msgid/pretext-support/073ef06c-
>> f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com?
>> utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com <mailto:pretext-
> support+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/pretext-
> support/3A4C6285-7A81-47F8-BE03-0D7588AD508B%40gmail.com <https://
> groups.google.com/d/msgid/pretext-support/3A4C6285-7A81-47F8-
> BE03-0D7588AD508B%40gmail.com?utm_medium=email&utm_source=footer>.

Rob Beezer

unread,
Jan 24, 2025, 6:08:25 PM1/24/25
to pretext...@googlegroups.com
We'll, the website examples are stale, but maybe the bulk of the CSS was in for the last build. Thanks for checking

On January 23, 2025 12:44:55 PM HST, Andrew Scholer <andrew....@chemeketa.edu> wrote:
>Indentation on poems looks like it is working fine:
>An Irish Airman Foresees His Death
><https://pretextbook.org/examples/humanities/html/yeats.html#yeats-6>
>>>> <https://groups.google.com/d/msgid/pretext-support/f11be9d6-2f4b-4195-b58e-b066a5b903een%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "PreTeXt support" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to pretext-suppo...@googlegroups.com.
>> To view this discussion visit
>> https://groups.google.com/d/msgid/pretext-support/073ef06c-f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com
>> <https://groups.google.com/d/msgid/pretext-support/073ef06c-f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

Rob Beezer

unread,
Jan 24, 2025, 6:17:18 PM1/24/25
to pretext...@googlegroups.com
A #ol is for steps in a procedure. Like a program, an algorithm, or a recipe. (A #ul is for the ingredients in a recipe.). So the numbers in the example algorithm are being used correctly, and the contents of the loop is a separate nested sequence to be followed in order. It is a conscious choice to use numbers for the inner procedure, the default is a, b, c.

So I think that use of a list structure is in very good shape. In particular, those numbers are not to be construed as line numbers.

Rob


On January 23, 2025 12:23:46 PM HST, 'JACOB DENSON' via PreTeXt support <pretext...@googlegroups.com> wrote:
>Using <ol> as in the example Rob mentioned has two problems in the context
>I am working in:
>
>Problem 1: the number of the indented list does not continue from the
>numbering of the original list (so that 4: in my last post would be listed
>as 1.)
>Problem 2: the numbering of the indented list is also indented, whereas I
>would prefer the position of the numbering to be consistent between
>non-indented and indented lines.
>
>The <poem> division seems to provide solutions to these two problems,
>except it doesn't seem to have line numbering support.
>On Thursday, January 23, 2025 at 4:18:53 PM UTC-6 dbrian...@gmail.com wrote:
>
>>> <https://groups.google.com/d/msgid/pretext-support/f11be9d6-2f4b-4195-b58e-b066a5b903een%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>

Rob Beezer

unread,
Jan 24, 2025, 6:32:03 PM1/24/25
to pretext...@googlegroups.com
Yes, a program that rhymes inside a #poem would be too much. But seriously, I hope nobody uses #poem to make this happen.

I'm warming to the idea of a #pseudocode as Haris suggests, in the same family as #program and #console. With the point being to allow #m. There was a suggestion to markup identifiers, keywords, comments, etc. With et cetera being a slippery slope.

New elements are expensive, as Haris also suggests. Mixing verbatim text and math, and mixing monospace and math fonts, are tricky.

Anybody with the requisite skills interested enough to take on a challenge?

Rob
>>>> To view this discussion visit https://groups.google.com/d/msgid/pretext-support/f11be9d6-2f4b-4195-b58e-b066a5b903een%40googlegroups.com <https://groups.google.com/d/msgid/pretext-support/f11be9d6-2f4b-4195-b58e-b066a5b903een%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com <mailto:pretext-suppo...@googlegroups.com>.
>> To view this discussion visit https://groups.google.com/d/msgid/pretext-support/073ef06c-f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com <https://groups.google.com/d/msgid/pretext-support/073ef06c-f6ec-4e1b-9a75-32a31c1b41e1n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>

Alex Jordan

unread,
Jan 24, 2025, 7:30:57 PM1/24/25
to pretext...@googlegroups.com
Of possible interest:
https://www.npmjs.com/package/pseudocode

Is a #program with @language="pseudocode" better than a #pseudocode?


On Fri, Jan 24, 2025 at 3:32 PM 'Rob Beezer' via PreTeXt support
> To view this discussion visit https://groups.google.com/d/msgid/pretext-support/MTAwMDAzNS5iZWV6ZXI.1737761520%40quikprotect.

Rob Beezer

unread,
Jan 24, 2025, 9:41:57 PM1/24/25
to pretext...@googlegroups.com
Yes, that is very interesting. But now we write real latex for latex and we write latex-like for html and then with no js we do a third thing for epub? Seems a bit backward to mimic latex to get html? And we are dependent on another project.

But more consequential. The structure in the example at the pseudocode site suggests a lot of necessary structure in the pretext source. Generally we move from more pretext structure to less in output formats. Which makes this seem more expensive.

program/@language="pseudocode": the motivation here is to sprinkle in #m. #program is sometimes runnable (Runestone, especially). I feel like the separation by element is appropriate, versus a shade-of-gray with an attribute.

Rob

JACOB DENSON

unread,
Mar 20, 2025, 1:39:45 PM3/20/25
to PreTeXt support
Hi all, I just wanted to make a post to revive discussion on this topic. Mitch and I are currently using the <poem> environment to generate pseudocode, but if we were to obtain similar results via a '<pseudocode>' environment this would certainly be better, and also the option to add line numbers on a column to the left of the pseudocode. The code we're using that generates this algorithm is also given below.

algorithm_pseudocode.png
<algorithm>
  <statement>
    <p> Algorithm <m>\textbf{sum}(A,B)</m> where <m>A</m> and <m>B</m> are square 2-dimensional arrays with <m>n</m> rows and <m>n</m> columns, containing elements from <m>\Z</m>. </p>

    <poem>
      <stanza>
        <line indent="1">
          For <m>i = 1</m> to <m>n</m>:
        </line>
        <line indent="2">
          For <m>j = 1</m> to <m>n</m>:
        </line>
        <line indent="3">
          <m>C[i,j] \leftarrow 0</m>
        </line>
        <line indent="1">
          For <m>i = 1</m> to <m>n</m>:
        </line>
        <line indent="2">
          For <m>j = 1</m> to <m>n</m>:
        </line>
        <line indent="3">
          <m>C[i,j] \leftarrow A[i,j] + B[i,j]</m>
        </line>
        <line indent="1">
          Return <m>C</m>
        </line>
      </stanza>
    </poem>
  </statement>
</algorithm>



Reply all
Reply to author
Forward
0 new messages