Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Pseudocode Plugin

Allows rendering pseudocode using pseudocode.js. For Tex-based exports, simply the raw contents are exported, surrounded by the \algorithm and \algorithmic environments, and with an attempt to add the dependencies with a \usepackage. This will probably throw a Latex error, since packages may only be imported in the preamble. I fixed handling of this in my fork of the myst-to-tex renderer, but this is not yet implemented upstream. If using an incompatible mystmd version, the correct packages imports should be manually added to the tex preamble by adding \usepackage{algorithmic} and \usepackage{algorithm}.

Examples

Render
Source

Algorithm 1 Quicksort

procedure Quicksort(A,p,rA, p, r)

if p<rp < r then

q=q = Partition(A,p,rA, p, r)

Quicksort(A,p,q−1A, p, q - 1)

Quicksort(A,q+1,rA, q + 1, r)

end if

end procedure

procedure Partition(A,p,rA, p, r)

x=A[r]x = A[r]

i=p−1i = p - 1

for j=p…r−1j = p \dots r - 1 do

if A[j]<xA[j] < x then

i=i+1i = i + 1

exchange A[i]A[i] with A[j]A[j]

end if

exchange A[i]A[i] with A[r]A[r]

end for

end procedure

Refer to Algorithm 2.

Render
Source

Algorithm 2 test Text-style

Require: some preconditions

Ensure: some postconditions

Require: some inputs

Ensure: some outputs

procedure Test-Declarations()

font families: sffamily, ttfamily, normalfont, rmfamily.

font weights: normal weight, bold, medium, lighter.

font shapes: itshape Small-Caps slshape upshape.

font sizes: tiny scriptsize footnotesize small normal large Large Large huge Huge

end procedure

procedure Test-Commands()

textnormal, textrm, textsf, texttt.

textbf, textmd, textlf.

textup, textit, textsc, textsl.

uppercase, LOWERCASE.

end procedure

procedure Test-Colors()

colors: red\color{red}{red}, green\color{green}{green}, blue\color{blue}{blue}

colors: yellow\color{yellow}{yellow}, cyan\color{cyan}{cyan}, magenta\color{magenta}{magenta}

end procedure

Render
Source

Algorithm 3 Test specials

procedure Test-Specials()

Specials: { } $ & # % _

Bools: and or not true false

Carriage return: first line
second line

Text-symbols: \

Quote-symbols: ‘single quotes', ‘‘double quotes''

Math: (Cm)(\mathcal{C}_m), i←i+1i \gets i + 1, E=mc2E=mc^2, xn+yn=zn x^n + y^n = z^n , $\$, $\$

end procedure

Render
Source

Algorithm 4 Test control blocks

procedure Test-If()

if <cond> then

<block>;

else if <cond>; then

<block>;

else

<block>;

end if

end procedure

procedure Test-For(nn)

i←0i \gets 0

for i<ni < n do

print ii

i←i+1i \gets i + 1

end for

end procedure

procedure Test-For-To(nn)

i←0i \gets 0

for i…ni \dots n do

print ii

end for

end procedure

procedure Test-For-All(nn)

for all i∈{0,1,⋯ ,n}i \in \{0, 1, \cdots, n\} do

print ii

end for

end procedure

procedure Test-While(nn)

i←0i \gets 0

while i<ni < n do

print ii

i←i+1i \gets i + 1

end while

end procedure

procedure Test-Repeat(nn)

i←0i \gets 0

repeat

print ii

i←i+1i \gets i + 1

until i>ni>n

end procedure

procedure Test-Break-Continue(nn)

for i=0…2ni = 0 \dots 2n do

if i<n/2i < n/2 then

continue

else if i>ni > n then

break

end if

print ii

end for

end procedure

Render
Source

Algorithm 5 Test comments

procedure Test-Comments() // comment for procedure

a statement // inline comment

// line comment

if some condition then // comment for if

return true // another inline comment

else // comment for else

return false // yet another inline comment

end if

end procedure

Note: the scope-lines and no-end options only work in HTML-based rendering.

Render
Source

Algorithm 6 Classical Euclidean Algorithm

procedure Euclid(a,ba,b)

while a≠ba \neq b do

if a>ba > b then

a←a−ba \gets a - b

else

b←b−ab \gets b - a

end if

end while

return aa

end procedure

Render
Source

Algorithm 7 DBSCAN

Require: A dataset DD, the ε\varepsilon distance threshold, and the minimum number of points minPtsminPts

Ensure: A set of clusters KK

procedure DBSCAN(D,ε,minPtsD, \varepsilon, minPts)

K←∅K \gets \emptyset

for all p∈Dp \in D do

if pp has not been visited then

mark pp as visited

Nε(p)←N_{\varepsilon}(p) \gets RangeQuery(p,ε,D)(p, \varepsilon, D)

if Nε(p)<minPtsN_{\varepsilon}(p) < minPts then

mark pp as noise

else // p is a core object

C←C \gets ExpandCluster(p,Nε(p))(p, N_{\varepsilon}(p))

K←K∪{C}K \gets K \cup \{C\}

return KK