Algo Typst Library

repository·main·Indexed 41 days ago

https://github.com/platformer/typst-algorithms

A Typst library for typesetting algorithms and programming code blocks. It provides the `algo` function for formatted pseudocode with support for indentation via `#i` and `#d`, line numbering, and keyword emphasis, as well as a `code` function for styled source code blocks.

Tokens
1.1K
Snippets
6
Records
6
Agent score
2%

What's inside Algo

  1. Install the algo package

    main

    For Typst v0.6.0+, you can import the package directly from the preview repository. For older versions, download algo.typ and include it in your project.

    Using Typst v0.6.0+:

    #import "@preview/algo:0.3.6": algo, i, d, comment, code

    Using older versions:

    #import "algo.typ": algo, i, d, comment, code
    #import "@preview/algo:0.3.6": algo, i, d, comment, code
  2. Create code blocks with the code function

    main

    The code function creates a styled code block, typically used for actual programming language source code (e.g., Python, C++).

    Usage: Pass a raw block (using triple backticks) as the body.

    #code()[ 
      ```py
      def hello():
          print("world")

    ]

    
    **Key Configuration Options:**
    - `line-numbers`: `boolean`. Enables/disables line numbering.
    - `indent-guides`: `stroke`. Visual guides for indentation.
    - `tab-size`: `integer`. Number of spaces for a tab (automatically detected if not specified).
    - `row-gutter`: `length`. Vertical space between lines.
    - `column-gutter`: `length`. Horizontal space between line numbers and text.
    - `inset`: `length`. Inner padding.
    - `fill`: `color`. Background color.
    - `stroke`: `stroke`. Border style.
    - `main-text-styles`: `dictionary`. Styling for the raw text (supports Typst `text` function parameters).
    - `line-number-styles`: `dictionary`. Styling for line numbers (supports Typst `text` function parameters).
    

    #code(indent-guides: 1pt + gray)[

    def fib(n):
        return n

    ]

  3. Create pseudocode with the algo function

    main

    The algo function is used to create formatted pseudocode elements. It supports titles, parameters, line numbers, and keyword emphasis.

    Basic Usage:

    #algo(
      title: "Fib",
      parameters: ("n",)
    )[
      if $n < 0$:#i\        // use #i to indent
        return null#d\      // use #d to dedent
      return $n$
    ]

    Key Configuration Options:

    • title: string or content. The algorithm name.
    • parameters: array. List of parameters (strings are automatically treated as math mode).
    • line-numbers: boolean. Enables/disables line numbering.
    • strong-keywords: boolean. Enables/disables automatic emphasis for keywords.
    • keywords: array of string. Custom list of terms to emphasize.
    • comment-prefix: content. Prefix for comments (default is "// ").
    • indent-size: length. Size of line indentations.
    • indent-guides: stroke. Visual guides for indentation.
    • main-text-styles: dictionary. Styling for the main text (supports Typst text function parameters).
    • comment-styles: dictionary. Styling for comments (supports Typst text function parameters).
    • line-number-styles: dictionary. Styling for line numbers (supports Typst text function parameters).
    #algo(
      title: "Fib",
      parameters: ("n",)
    )[
      if $n < 0$:#i\
        return null#d\
      return $n$
    ]
  4. Add comments in algo bodies with comment

    main

    The comment function adds a comment to the current line within an algo body.

    Parameters:

    • body: content. The text of the comment.
    • inline: boolean. If true, the comment is displayed in place of the text. If false (default), it is displayed on the right side of the algorithm.

    Note: Inline comments respect both main-text-styles and comment-styles, preferring comment-styles in case of conflict. To bypass keyword emphasis in inline comments, wrap the content in a text function.

    #algo()[
      return $n$#comment[this is a right-aligned comment]
      return $n$#comment(inline: true)[this is inline]
    ]
  5. Prevent keyword emphasis with no-emph

    main

    Use the no-emph function within an algo body to prevent specific content from being automatically emphasized as a keyword. This is useful if a word is both a keyword and intended as normal text.

    #algo()[
      #no-emph[if] is not emphasized here
    ]