Folioby Interconnected
Log InSign Up

Code Blocks and Algorithms — lstlisting, minted, algorithmic

A complete guide to code display on Folio. Covers lstlisting, minted, and verbatim code blocks with supported languages, plus algorithm/algorithmic environments for pseudocode.

FO
Folio Official
March 3, 2026

1. Code Blocks

Folio supports three code display environments. All render with syntax highlighting.

1.1. lstlisting Environment

Specify the language with [language=...]:

def fibonacci(n: int) -> int:
    """Return the nth Fibonacci number"""
    if n <= 1:
        return n
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

# Usage
for i in range(10):
    print(f"F({i}) = {fibonacci(i)}")

Another language:

interface Article {
  id: string;
  title: string;
  content: string;
  tags: string[];
  createdAt: Date;
}

async function fetchArticle(id: string): Promise<Article> {
  const response = await fetch('/api/articles/' + id);
  if (!response.ok) {
    throw new Error('Article not found: ' + id);
  }
  return response.json();
}

1.2. minted Environment

minted specifies the language in curly braces:

public class BinarySearch {
    public static int search(int[] arr, int target) {
        int left = 0, right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) return mid;
            else if (arr[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return -1;
    }
}

1.3. Supported Languages

Languages with syntax highlighting support:

Language Key Language
Python Python
JavaScript / javascript JavaScript
TypeScript / typescript TypeScript
Java / java Java
C / c C
C++ / cpp C++
Bash / bash Bash / Shell
JSON / json JSON
LaTeX / latex LaTeX
Markdown / markdown Markdown

1.4. verbatim Environment

Use verbatim to display content as-is without highlighting:

This is displayed as-is.
  Indentation is preserved.
    Special characters $ % & # are not converted.

2. Algorithm Pseudocode

2.1. algorithm + algorithmic Environments

algorithm is a captioned container; algorithmic is the pseudocode body:

\begin{algorithm}
\caption{Euclidean Algorithm}
\begin{algorithmic}
\Function{GCD}{$a, b$}
  \While{$b \neq 0$}
    \State $t \gets b$
    \State $b \gets a \mod b$
    \State $a \gets t$
  \EndWhile
  \Return $a$
\EndFunction
\end{algorithmic}
\end{algorithm}

Result:

Algorithm 1: Euclidean Algorithm
function GCD a,b
while b=0
t←b 
b←amodb 
a←t 
end while
return a
end function

2.2. Control Structures

Control structures available in algorithmic:

Conditionals:

\begin{algorithmic}
\If{$x > 0$}
  \State $\text{sign} \gets +1$
\ElsIf{$x < 0$}
  \State $\text{sign} \gets -1$
\Else
  \State $\text{sign} \gets 0$
\EndIf
\end{algorithmic}

Result:

if x>0
sign←+1 
else if x<0
sign←−1 
else
sign←0 
end if

Loops:

\begin{algorithmic}
\For{$i \gets 1$ \KwTo $n$}
  \State $S \gets S + a_i$
\EndFor
\end{algorithmic}

Result:

for i←1 \KwTo n
S←S+ai​ 
end for
\begin{algorithmic}
\While{queue is not empty}
  \State $v \gets$ \Call{Dequeue}{}
  \For{$u \in \text{Adj}(v)$}
    \If{$u$ is unvisited}
      \State Mark $u$ as visited
      \State \Call{Enqueue}{$u$}
    \EndIf
  \EndFor
\EndWhile
\end{algorithmic}

Result:

while queue is not empty
v←Dequeue() 
for u∈Adj(v)
if u is unvisited
 Mark u as visited 
Enqueue(u) 
end if
end for
end while

2.3. Input/Output and Preconditions

\begin{algorithmic}
\Require $n \geq 0$ (non-negative integer)
\Ensure Returns $n!$
\Function{Factorial}{$n$}
  \If{$n = 0$}
    \Return $1$
  \EndIf
  \Return $n \times$ \Call{Factorial}{$n - 1$}
\EndFunction
\end{algorithmic}

Result:

Require
n≥0 (non-negative integer)
Ensure
Returns n!
function Factorial n
if n=0
return 1
end if
return n×
Factorial(n−1)
end function

2.4. A More Complex Example: Merge Sort

\begin{algorithm}
\caption{Merge Sort}
\begin{algorithmic}
\Function{MergeSort}{$A, l, r$}
  \If{$l < r$}
    \State $m \gets \lfloor (l + r) / 2 \rfloor$
    \State \Call{MergeSort}{$A, l, m$}
    \State \Call{MergeSort}{$A, m+1, r$}
    \State \Call{Merge}{$A, l, m, r$}
  \EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}

Result:

Algorithm 2: Merge Sort
function MergeSort A,l,r
if l<r
m←⌊(l+r)/2⌋ 
MergeSort(A,l,m) 
MergeSort(A,m+1,r) 
Merge(A,l,m,r) 
end if
end function

function Merge A,l,m,r
L←A[l…m], R←A[m+1…r] 
i←0, j←0, k←l 
while i<∣L∣ and j<∣R∣
if L[i]≤R[j]
A[k]←L[i], i←i+1 
else
A[k]←R[j], j←j+1 
end if
k←k+1 
end while
 Copy remaining elements 
end function

3. Summary

On Folio, both code display (lstlisting, minted) and pseudocode (algorithmic) work with no additional setup. Next, we'll look at diagrams and tables.

LaTeXFolioTutorialCode BlocksAlgorithmsalgorithmic
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Folio BasicsPart 5 of 8
Previous
Mastering Theorem Environments — theorem, definition, proof, and Friends
Next
Diagrams and Tables — mermaid, tikzgraph, tikzcd, tabular

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 3, 2026

Mastering Theorem Environments — theorem, definition, proof, and Friends

A complete guide to Folio's 18+ built-in theorem environments. Covers theorem, definition, proof, lemma, corollary, example, remark, and more — usage, numbering, named arguments, and italic/roman styles explained.

LaTeXFolioTutorial
1
Folio Official·March 3, 2026

Writing Math — Every Notation Available in KaTeX

A comprehensive guide to Folio's math environments. Covers inline math, equation, align, gather, cases, matrix, and \newcommand — every KaTeX-based math notation explained.

LaTeXFolioTutorial
Folio Official·March 3, 2026

Diagrams and Tables — mermaid, tikzgraph, tikzcd, tabular

A complete guide to diagrams and tables on Folio. Covers mermaid flowcharts and sequence diagrams, tikzgraph for graph drawing, tikzcd for commutative diagrams, tabular for tables, figure/table environments, and includegraphics.

LaTeXFolioTutorial
Folio Official·March 3, 2026

Folio LaTeX Reference: A Complete Guide to Commands, Environments, and Syntax

A single-page overview of Folio's LaTeX syntax. A quick reference covering text formatting, math, theorem environments, code blocks, diagrams, and cross-references, with support status shown as ✅⚠️❌.

LaTeXFolioTutorial