{"id":13524790,"url":"https://github.com/platformer/typst-algorithms","last_synced_at":"2025-04-01T03:32:41.471Z","repository":{"id":152464659,"uuid":"625715378","full_name":"platformer/typst-algorithms","owner":"platformer","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-19T01:23:41.000Z","size":1491,"stargazers_count":128,"open_issues_count":8,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-02T06:17:07.942Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Typst","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/platformer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-04-10T00:13:50.000Z","updated_at":"2024-07-24T01:02:02.000Z","dependencies_parsed_at":"2023-12-07T02:27:07.498Z","dependency_job_id":"d6322dc3-ffbd-47b2-ae19-543a28db866a","html_url":"https://github.com/platformer/typst-algorithms","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformer%2Ftypst-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformer%2Ftypst-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformer%2Ftypst-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platformer%2Ftypst-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/platformer","download_url":"https://codeload.github.com/platformer/typst-algorithms/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222698187,"owners_count":17024877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:13.479Z","updated_at":"2025-04-01T03:32:41.464Z","avatar_url":"https://github.com/platformer.png","language":"Typst","funding_links":[],"categories":["Typst","库和工具类","Templates \u0026 Libraries"],"sub_categories":["杂项","Science, Technology, Engineering \u0026 Math (STEM)"],"readme":"# Algo\n\nA Typst library for writing algorithms. On Typst v0.6.0+ you can import the `algo` package:\n\n```typst\n#import \"@preview/algo:0.3.6\": algo, i, d, comment, code\n```\n\nOtherwise, add the `algo.typ` file to your project and import it as normal:\n\n```typst\n#import \"algo.typ\": algo, i, d, comment, code\n```\n\nUse the `algo` function for writing pseudocode and the `code` function for writing code blocks with line numbers. Check out the [examples](#examples) below for a quick overview. See the [usage](#usage) section to read about all the options each function has.\n\n## Examples\n\nHere's a basic use of `algo`:\n\n```typst\n#algo(\n  title: \"Fib\",\n  parameters: (\"n\",)\n)[\n  if $n \u003c 0$:#i\\        // use #i to indent the following lines\n    return null#d\\      // use #d to dedent the following lines\n  if $n = 0$ or $n = 1$:#i #comment[you can also]\\\n    return $n$#d #comment[add comments!]\\\n  return #smallcaps(\"Fib\")$(n-1) +$ #smallcaps(\"Fib\")$(n-2)$\n]\n```\n\n\u003cimg src=\"https://user-images.githubusercontent.com/40146328/235323240-e59ed7e2-ebb6-4b80-8742-eb171dd3721e.png\" width=\"400px\" /\u003e\n\n\u003cbr /\u003e\n\nHere's a use of `algo` without a title, parameters, line numbers, or syntax highlighting:\n\n```typst\n#algo(\n  line-numbers: false,\n  strong-keywords: false\n)[\n  if $n \u003c 0$:#i\\\n    return null#d\\\n  if $n = 0$ or $n = 1$:#i\\\n    return $n$#d\\\n  \\\n  let $x \u003c- 0$\\\n  let $y \u003c- 1$\\\n  for $i \u003c- 2$ to $n-1$:#i #comment[so dynamic!]\\\n    let $z \u003c- x+y$\\\n    $x \u003c- y$\\\n    $y \u003c- z$#d\\\n    \\\n  return $x+y$\n]\n```\n\n\u003cimg src=\"https://user-images.githubusercontent.com/40146328/235323261-d6e7a42c-ffb7-4c3a-bd2a-4c8fc2df5f36.png\" width=\"300px\" /\u003e\n\n\u003cbr /\u003e\n\nAnd here's `algo` with more styling options:\n\n```typst\n#algo(\n  title: [                    // note that title and parameters\n    #set text(size: 15pt)     // can be content\n    #emph(smallcaps(\"Fib\"))\n  ],\n  parameters: ([#math.italic(\"n\")],),\n  comment-prefix: [#sym.triangle.stroked.r ],\n  comment-styles: (fill: rgb(100%, 0%, 0%)),\n  indent-size: 15pt,\n  indent-guides: 1pt + gray,\n  row-gutter: 5pt,\n  column-gutter: 5pt,\n  inset: 5pt,\n  stroke: 2pt + black,\n  fill: none,\n)[\n  if $n \u003c 0$:#i\\\n    return null#d\\\n  if $n = 0$ or $n = 1$:#i\\\n    return $n$#d\\\n  \\\n  let $x \u003c- 0$\\\n  let $y \u003c- 1$\\\n  for $i \u003c- 2$ to $n-1$:#i #comment[so dynamic!]\\\n    let $z \u003c- x+y$\\\n    $x \u003c- y$\\\n    $y \u003c- z$#d\\\n    \\\n  return $x+y$\n]\n```\n\n\u003cimg src=\"https://github.com/platformer/typst-algorithms/assets/40146328/89f80b5d-bdb2-420a-935d-24f43ca597d8\" width=\"300px\" /\u003e\n\n\u0026nbsp;\n\nHere's a basic use of `code`:\n\n````typst\n#code()[\n  ```py\n  def fib(n):\n    if n \u003c 0:\n      return None\n    if n == 0 or n == 1:        # this comment is\n      return n                  # normal raw text\n    return fib(n-1) + fib(n-2)\n  ```\n]\n````\n\n\u003cimg src=\"https://user-images.githubusercontent.com/40146328/235324088-a3596e0b-af90-4da3-b326-2de11158baac.png\" width=\"400px\"/\u003e\n\n\u003cbr /\u003e\n\nAnd here's `code` with some styling options:\n\n````typst\n#code(\n  indent-guides: 1pt + gray,\n  row-gutter: 5pt,\n  column-gutter: 5pt,\n  inset: 5pt,\n  stroke: 2pt + black,\n  fill: none,\n)[\n  ```py\n  def fib(n):\n      if n \u003c 0:\n          return None\n      if n == 0 or n == 1:        # this comment is\n          return n                # normal raw text\n      return fib(n-1) + fib(n-2)\n  ```\n]\n````\n\n\u003cimg src=\"https://github.com/platformer/typst-algorithms/assets/40146328/c091ac43-6861-40bc-8046-03ea285712c3\" width=\"400px\"/\u003e\n\n## Usage\n\n### `algo`\n\nMakes a pseudocode element.\n\n```typst\nalgo(\n  body,\n  header: none,\n  title: none,\n  parameters: (),\n  line-numbers: true,\n  strong-keywords: true,\n  keywords: _algo-default-keywords, // see below\n  comment-prefix: \"// \",\n  indent-size: 20pt,\n  indent-guides: none,\n  indent-guides-offset: 0pt,\n  row-gutter: 10pt,\n  column-gutter: 10pt,\n  inset: 10pt,\n  fill: rgb(98%, 98%, 98%),\n  stroke: 1pt + rgb(50%, 50%, 50%),\n  radius: 0pt,\n  breakable: false,\n  block-align: center,\n  main-text-styles: (:),\n  comment-styles: (fill: rgb(45%, 45%, 45%)),\n  line-number-styles: (:)\n)\n```\n\n**Parameters:**\n\n*   `body`: `content` \u0026mdash; Main algorithm content.\n\n*   `header`: `content` \u0026mdash; Algorithm header. If specified, `title` and `parameters` are ignored.\n\n*   `title`: `string` or `content` \u0026mdash; Algorithm title. Ignored if `header` is specified.\n\n*   `Parameters`: `array` \u0026mdash; List of algorithm parameters. Elements can be `string` or `content` values. `string` values will automatically be displayed in math mode. Ignored if `header` is specified.\n\n*   `line-numbers`: `boolean` \u0026mdash; Whether to display line numbers.\n\n*   `strong-keywords`: `boolean` \u0026mdash; Whether to strongly emphasize keywords.\n\n*   `keywords`: `array` \u0026mdash; List of terms to receive strong emphasis. Elements must be `string` values. Ignored if `strong-keywords` is `false`.\n\n    The default list of keywords is stored in `_algo-default-keywords`. This list contains the following terms:\n\n    ```\n    (\"if\", \"else\", \"then\", \"while\", \"for\",\n    \"repeat\", \"do\", \"until\", \":\", \"end\",\n    \"and\", \"or\", \"not\", \"in\", \"to\",\n    \"down\", \"let\", \"return\", \"goto\")\n    ```\n\n    Note that for each of the above terms, `_algo-default-keywords` also contains the uppercase form of the term (e.g. \"for\" and \"For\").\n\n*   `comment-prefix`: `content` \u0026mdash; What to prepend comments with.\n\n*   `indent-size`: `length` \u0026mdash; Size of line indentations.\n\n*   `indent-guides`: `stroke` \u0026mdash; Stroke for indent guides.\n\n*   `indent-guides-offset`: `length` \u0026mdash; Horizontal offset of indent guides.\n\n*   `row-gutter`: `length` \u0026mdash; Space between lines.\n\n*   `column-gutter`: `length` \u0026mdash; Space between line numbers, text, and comments.\n\n*   `inset`: `length` \u0026mdash; Size of inner padding.\n\n*   `fill`: `color` \u0026mdash; Fill color.\n\n*   `stroke`: `stroke` \u0026mdash; Stroke for the element's border.\n\n*   `radius`: `length` \u0026mdash; Corner radius.\n\n*   `breakable`: `boolean` \u0026mdash; Whether the element can break across pages. WARNING: indent guides may look off when broken across pages.\n\n*   `block-align`: `none` or `alignment` or `2d alignment` \u0026mdash; Alignment of the `algo` on the page. Using `none` will cause the internal `block` element to be returned as-is.\n\n*   `main-text-styles`: `dictionary` \u0026mdash; Styling options for the main algorithm text. Supports all parameters in Typst's native `text` function.\n\n*   `comment-styles`: `dictionary` \u0026mdash; Styling options for comment text. Supports all parameters in Typst's native `text` function.\n\n*   `line-number-styles`: `dictionary` \u0026mdash; Styling options for line numbers. Supports all parameters in Typst's native `text` function.\n\n### `i` and `d`\n\nFor use in an `algo` body. `#i` indents all following lines and `#d` dedents all following lines.\n\n### `comment`\n\nFor use in an `algo` body. Adds a comment to the line in which it's placed.\n\n```typst\ncomment(\n  body,\n  inline: false\n)\n```\n\n**Parameters:**\n\n*   `body`: `content` \u0026mdash; Comment content.\n\n*   `inline`: `boolean` \u0026mdash; If true, the comment is displayed in place rather than on the right side.\n\n    NOTE: inline comments will respect both `main-text-styles` and `comment-styles`, preferring `comment-styles` when the two conflict.\n\n    NOTE: to make inline comments insensitive to `strong-keywords`, strong emphasis is disabled within them. This can be circumvented via the `text` function:\n\n    ```typst\n    #comment(inline: true)[#text(weight: 700)[...]]\n    ```\n\n### `no-emph`\n\nFor use in an `algo` body. Prevents the passed content from being strongly emphasized. If a word appears in your algorithm both as a keyword and as normal text, you may escape the non-keyword usages via this function.\n\n```typst\nno-emph(\n  body\n)\n```\n\n**Parameters:**\n\n*   `body`: `content` \u0026mdash; Content to display without emphasis.\n\n### `code`\n\nMakes a code block element.\n\n```typst\ncode(\n  body,\n  line-numbers: true,\n  indent-guides: none,\n  indent-guides-offset: 0pt,\n  tab-size: auto,\n  row-gutter: 10pt,\n  column-gutter: 10pt,\n  inset: 10pt,\n  fill: rgb(98%, 98%, 98%),\n  stroke: 1pt + rgb(50%, 50%, 50%),\n  radius: 0pt,\n  breakable: false,\n  block-align: center,\n  main-text-styles: (:),\n  line-number-styles: (:)\n)\n```\n\n**Parameters:**\n\n*   `body`: `content` \u0026mdash; Main content. Expects `raw` text.\n\n*   `line-numbers`: `boolean` \u0026mdash; Whether to display line numbers.\n\n*   `indent-guides`: `stroke` \u0026mdash; Stroke for indent guides.\n\n*   `indent-guides-offset`: `length` \u0026mdash; Horizontal offset of indent guides.\n\n*   `tab-size`: `integer` \u0026mdash; Amount of spaces that should be considered an indent. If unspecified, the tab size is determined automatically from the first instance of starting whitespace.\n\n*   `row-gutter`: `length` \u0026mdash; Space between lines.\n\n*   `column-gutter`: `length` \u0026mdash; Space between line numbers and text.\n\n*   `inset`: `length` \u0026mdash; Size of inner padding.\n\n*   `fill`: `color` \u0026mdash; Fill color.\n\n*   `stroke`: `stroke` \u0026mdash; Stroke for the element's border.\n\n*   `radius`: `length` \u0026mdash; Corner radius.\n\n*   `breakable`: `boolean` \u0026mdash; Whether the element can break across pages. WARNING: indent guides may look off when broken across pages.\n\n*   `block-align`: `none` or `alignment` or `2d alignment` \u0026mdash; Alignment of the `code` on the page. Using `none` will cause the internal `block` element to be returned as-is.\n\n*   `main-text-styles`: `dictionary` \u0026mdash; Styling options for the main raw text. Supports all parameters in Typst's native `text` function.\n\n*   `line-number-styles`: `dictionary` \u0026mdash; Styling options for line numbers. Supports all parameters in Typst's native `text` function.\n\n## Contributing\n\nPRs are welcome! And if you encounter any bugs or have any requests/ideas, feel free to open an issue.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatformer%2Ftypst-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplatformer%2Ftypst-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatformer%2Ftypst-algorithms/lists"}