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.
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:
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:
Loops:
\begin{algorithmic}
\For{$i \gets 1$ \KwTo $n$}
\State $S \gets S + a_i$
\EndFor
\end{algorithmic}
Result:
\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:
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:
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:
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.
Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.