sashaafm

sashaafm OP

Hi guys! Has anyone found (or has a way) to insert good Elixir code listing and snippets in LaTeX? Something with automatic color highlights. I’m using the listings package and while it’s got support for a ton of languages (including Erlang) it does not have for Elixir :frowning2:

I guess I could always insert them as images although I don’t find that to be very correct for a formal document.

First 10 of 10 Posts Switch mode

Qqwy

Qqwy

TypeCheck Core Team

I do not believe there exists a library for LaTeX that does Elixir-highlighting right now. However, as Elixir’s syntax is actually quite regular (i.e. there are not a lot of edge-cases), creating such a library should not be extremely difficult (but, you know, still difficult). There do exist syntax highlighting packages for Sublime, Atom, Emacs and the likes, so maybe some of their logic could be ported to a LaTeX library.

sashaafm

sashaafm OP

Yeah I guessed there wouldn’t be a package for it. Although I’ve been experimenting and I think the Ruby highlight should be okayish!

NobbZ

NobbZ

If you do not fear \write18 and have a recent version of pygments installed, you can use minted package.

vidalraphael

vidalraphael

I’m actually writing a paper on concurrency and I’m using tons of Elixir code. :smile:

I’m using the listings package and created a definition for the Elixir lang.

Some things like atoms and module attributes don’t work quite like I would prefer, but it is better than creating images and pasting then all the time :fearful:

Here’s the definition:

\lstdefinelanguage{elixir}{
	morekeywords={case,catch,def,do,else,false,%
		use,alias,receive,timeout,defmacro,defp,%
		for,if,import,defmodule,defprotocol,%
		nil,defmacrop,defoverridable,defimpl,%
		super,fn,raise,true,try,end,with,%
		unless},
	otherkeywords={<-,->},
	sensitive=true,
	morecomment=[l]{\#},
	morecomment=[n]{/*}{*/},
	morestring=[b]",
	morestring=[b]',
	morestring=[b]"""
}

And here’s the rendered result:

m1dnight

m1dnight

I made some minor changes to this, and ended up with highlighted keywords. The only downside is that a keyword needs to be followed by a space to make the parser happy.

\lstdefinelanguage{elixir}{
    morekeywords={case,catch,def,do,else,false,%
        use,alias,receive,timeout,defmacro,defp,%
        for,if,import,defmodule,defprotocol,%
        nil,defmacrop,defoverridable,defimpl,%
        super,fn,raise,true,try,end,with,%
        unless},
    otherkeywords={<-,->, |>, \%\{, \}, \{, \, (, )},
    sensitive=true,
    morecomment=[l]{\#},
    morecomment=[n]{/*}{*/},
    morecomment=[s][\color{purple}]{:}{\ },
    morestring=[s][\color{orange}]"",
    commentstyle=\color{commentgreen},
    keywordstyle=\color{eminence},
    stringstyle=\color{red},
	showstringspaces=false,
}

I have put an entire minimal example online here: Elixir Listings Latex · GitHub

The rendered output looks as follows:

OvermindDL1

OvermindDL1

You could always write a latex macro to call out to @tmbb’s syntax highlighter or so too, I think you could plug in a new backend to generate latex out pretty easily?

tmbb

tmbb

Sure, and that would allow you to be much more precise in complex bits of syntax, such as sigils and string interpolation. It woukd require packaging Makeup’s highlighter into an escript that highlights the input given by stdin. That’s a little beyond my elixir skills right now (I don’t use Elixir for scripting), but it can be done.

Sure. For someone who knows tex and latex well it can be done in an afternoon. The token format returned by the makeup lexers is stable (although not documented outside the source code, because I can’t think of a way to document them better) and is considered part of the public API, so writing a LaTeX formatter would be easy (again, assuming you’re a TeX pro, which i am not).

Your best bet, if you can read and write Python, is to translate the formatter from Pygments xD (most of Makeup is a blatant rippoff of Pygments anyway)

m1dnight

m1dnight

If you really want proper highlighting, there is always the minted[1] package, which works great. However, I had a hard constraint on lstlistings, so I could not use it.

I think that if we were to improve the highlighting, we should improve no pygments, the backend that is used by minted in latex.

[1] Code Highlighting with minted - Overleaf, Online LaTeX Editor

tmbb

tmbb

Indeed, but Makeup is a much better highlighter for elixir. You can replicate Makeup’s pexer in Pygments, but it’s hard because of Pygment’s “weird” architecture.

s417-lama

s417-lama

There is another option to do it: a vim plugin to generate listings with colorscheme of vim. It does not depend on syntax highlighting of listings and we can utilize syntax highlighting in vim for LaTeX:) The output looks quite beautiful.

https://github.com/s417-lama/carbonpaper.vim

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement