Make beautiful colored code listings in LaTeX with the power of TreeSitter.

Overview

What is this?

This is a CLI tool that consumes TreeSitter's output and transforms it into LaTeX code that will produce syntax-colored code listing.

If you are familiar with LaTeX packages like minted or listings, this could be seen as an alternative.

Example snippets:

Note that the colors and styles can be individually customized in configuration file.

Rust example snippet:
rust example

Julia example snippet:
rust example

How to use it?

External requirements:

  1. TreeSitter installed

    On Linux, it is tree-sitter-cli package.
    On Windows, it is tree-sitter.exe.

    You can check if the installation is successfull by running tree-sitter command. It should print help.

  2. Parsers for languages downloaded

    TreeSitter has parsers for many languages, you will need to download the ones you want and put them in a directory that will be specified in TreeSitter's configuration. More on that later.

  3. LaTeX setup

    In your LaTeX preamble, you will need to add \usepackage{listings} and below that, add \lstset{escapeinside={<@}{@>}}. The listings package provides captions, labels and referencing as well as other formatting (line numbers, frame, ...).

First run:

If you never ran tree-sitter on your machine before, it will want you to initialize it. That is done simply by running tree-sitter init-config and it will create a configuration file and tell you where to find it.

Actual usage of chromacode

You can download pre-build binary chromacode[.exe] from here or you can clone this repo and build it yourself by running cargo build --release (assuming you have Rust and Cargo installed and set up).

Once you have the initial setup done, you can just call chromacode and follow the instructions. Or, if you prefer classic CLI, you can call chromacode -h, read the help page and use it in non-interactive mode (or even in hybrid mode).

The general idea is that you will feed it a file with some source code and it will spit out a .tex file that is ready to be used in your LaTeX code (by \input{} command). Note that the package listings will be required in LaTeX since it provides labels, line numbering, frame and other goodness.

Configuration

For more detailed information, please refer to the official TreeSitter documentation

After running tree-sitter init-config, TreeSitter will create a configuration file config.json.

Paths and parsers

In the config.json file, there is an option parser-directories that determines where will TreeSitter look for the language parsers. When you download some language parser, the directory tree-sitter-[language] should be located in those paths.

Theme (colors and styles)

Second option in the config.json file is theme. TreeSitter will use those values for highlighting.

There will be probably some default values, but I recommend using the example I provided here as an template to build upon. It contains more capture groups and has unified format.

Highlight queries

Highlighting queries are patterns that tell TreeSitter what pieces of code have what syntactic meaning. Most of the language parsers provide some highlighting queries located usually in tree-sitter-language/queries/highlights.scm.

Those queries usually work fine out of the box, but aren't as rich as could be.

I use custom highlighting queries that are available in this repo in examples directory (here). They are pretty much taken from nvim-treesitter repository and modified to function properly without the Neovim abstractions. To make the nvim-treesitter's queries work, following modifications were made:

  1. replace unsupported predicates
    The nvim-treesitter uses some predicates that are not supported by the tree-sitter-cli.
    Most common are:

    • #lua-match? - I replaced it with regular #match?, but don't forget to replace the regex as well, Lua has some additional regex special characters such as %u or %s.
    • #any-of? - I expanded this one into many #match? patterns for each individual case, but there may be some better way to deal with it.
  2. Reorganize the scheme captures
    The nvim-treesitter does something smart when multiple captures are applicable at the same time, but the regular tree-sitter does not. From my experimentation, the first match is returned. Therefore, the more specific captures need to be in the file before the more generic ones. This may require some trial and error, but getting the longer ones as first is a good place to start.

Feel free to use my highlights as an inspiration. Also note that the file needs to be named highlights.scm, the files in this repository are intentionally named differently.

How does chromacode differ from minted?

The end goal is the same: to have pretty colored code listings in LaTeX. But the path is slightly different.

Here are the main differences:

  1. It uses TreeSitter to handle the syntax highlighting. The results are in my opinion better-looking, more rich and precise. See the example snippets above or the full example pdf and form your own opinion.

  2. This isn't a LaTeX package, but rather a tool that will generate LaTeX code. That means it will produce .tex file and it is not necessary to run it on every LaTeX compilation. If that is something positive or negative is up to you.

You might also like...
A Rust command that prettifies the ugly `cargo test` output into a beautiful one.
A Rust command that prettifies the ugly `cargo test` output into a beautiful one.

Cargo Pretty Test ✨ A Rust command-line tool that prettifies the ugly cargo test output into a beautiful output. This crate can be also used as a libr

A beautiful, tiny traceback and logging library supporting #![no_std] rust.

breadcrumbs Breadcrumbs is a beautiful, tiny traceback and logging library for Rust that offers seamless integration with #![no_std], multi-threading

Sample and plot power consumption, average frequency and cpu die temperatures over time.
Sample and plot power consumption, average frequency and cpu die temperatures over time.

sense Sense is a small tool to gather data on cpu temperature, power usage and clock frequency and plot graphs during some load. Dependencies Sense is

A command line tool to control the power state of Valve Base Stations 2.0.

lighthousectl A command line tool to control the power state of Valve Base Stations 2.0. Usage Scan All Base Stations It scans endlessly. You can stop

Move multiple files, with the power of your editor

NOTE FOR USERS THAT WANT TO TRY THIS OUT! The current code on the master branch doesn’t work as documented, as a newer more powerful interface is bein

Maccha is an extremely extensible and themable power menu for Windows, macOS, and Linux.

Maccha I hate coffee. Maccha is an extremely extensible and themable power menu for Windows, macOS, and Linux. Plugins Plugins are written in Rust (ot

Shaping, Processing, and Transforming Data with the Power of Sulfur with Rust
Shaping, Processing, and Transforming Data with the Power of Sulfur with Rust

Sulfur WIP https://www.youtube.com/watch?v=PAAvNmoqDq0 "Shaping, Processing, and Transforming Data with the Power of Sulfur" Welcome to the Sulfur pro

🚀 JavaScript driver for ScyllaDB, harnessing Rust's power through napi-rs for top performance. Pre-release stage. 🧪🔧
🚀 JavaScript driver for ScyllaDB, harnessing Rust's power through napi-rs for top performance. Pre-release stage. 🧪🔧

🚀 JavaScript driver for ScyllaDB. Pre-release stage. 🧪🔧 ⚠️ Disclaimer ⚠️ This repository and the associated npm package are currently in a 🐣 pre-r

RustGPT is a ChatGPT UI built with Rust + HTMX: the power of Rust coupled with the simplicity of HTMX 💚

RustGPT 🦀✨ RustGPT.Blog.Post.mp4 Welcome to the RustGPT repository! Here, you'll find a web ChatGPT clone entirely crafted using Rust and HTMX, where

Comments
  • libc version mismatch

    libc version mismatch

    Hello,

    I'd like to give chroma_code a try, but I seem to be missing some library files. I am running on ubuntu version 20.04.5. When I try to do ./chromacode -h I get the following error.

    ./chromacode: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.33' not found (required by ./chromacode)
    ./chromacode: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by ./chromacode)
    ./chromacode: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ./chromacode)
    

    I ran apt policy libc6 and got the following.

    libc6:
      Installed: 2.31-0ubuntu9.9
      Candidate: 2.31-0ubuntu9.9
    

    It seems I have an older version of libc6. Any idea how this could be resolved?

    opened by NuclearPowerNerd 8
Releases(1.0.0)
Owner
Tomáš Lebeda
Tomáš Lebeda
🏭 Convert Markdown documents into themed HTML pages with support for code syntax highlighting, LaTeX and Mermaid diagrams.

Marky Markdown Magician ?? Features Hot reload previewing ?? Conversion to HTML / PDF ?? Themes! ✨ Extensions - Math, diagrams, syntax-highlighting ??

Vadim 12 Feb 19, 2023
hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes

hexyl is a simple hex viewer for the terminal. It uses a colored output to distinguish different categories of bytes (NULL bytes, printable ASCII characters, ASCII whitespace characters, other ASCII characters and non-ASCII).

David Peter 7.3k Dec 29, 2022
Simple macros to write colored and formatted text to a terminal. Based on `termcolor`, thus also cross-platform.

Bunt: simple macro-based terminal colors and styles bunt offers macros to easily print colored and formatted text to a terminal. It is just a convenie

Lukas Kalbertodt 202 Dec 22, 2022
Latex template handler using Rust

Latex template handler using Rust All the templates used by this program are stored in another GitHub repository. Dependencies rust & cargo How to use

Johan Rodríguez 2 Oct 19, 2022
A transpiled superset of TeX for writing LaTeX.

LiA A transpiled superset of TeX for writing LaTeX. This is more for my personal use however you're welcome to use it or contribute. These added featu

Jasper 62 Jan 5, 2023
LaTeX support for Typst, powered by Rust and WASM.

MiTeX LaTeX support for Typst, powered by Rust and WASM. MiTeX processes LaTeX code into an abstract syntax tree (AST). Then it transforms the AST int

null 58 Jan 1, 2024
:large_orange_diamond: Build beautiful terminal tables with automatic content wrapping

Comfy-table Comfy-table tries to provide utility for building beautiful tables, while being easy to use. Features: Dynamic arrangement of content to a

Arne Beer 525 Jan 8, 2023
More beautiful HTML reports for llvm-cov/cargo-llvm-cov

?? llvm-cov-pretty More beautiful HTML reports for llvm-cov (cargo-llvm-cov specifically). Dark theme support (switches automatically based on your br

Dominik Nakamura 13 Jun 26, 2023
Beautiful, minimal, opinionated CLI prompts inspired by the Clack NPM package

Effortlessly build beautiful command-line apps with Rust ?? ✨ Beautiful, minimal, opinionated CLI prompts inspired by the @clack/prompts npm package.

Alexander Fadeev 7 Jul 23, 2023
A beautiful and feature-packed Apple Music CLI

am A beautiful and feature-packed Apple Music CLI! Written in Rust. Installation Nix (recommended) This GitHub repository contains a flake. Add github

Ryan Cao 5 Sep 21, 2023