lasem is a Ruby native extension that wraps the
Lasem C library to render mathematical
notation and SVG. It parses MathML, SVG, or Lasem-supported itex/LaTeX input
and emits SVG, PNG, PDF, or PostScript through Lasem’s existing layout
pipeline.
The gem keeps the Ruby layer focused on input validation, ergonomic entry points, and predictable errors. Parsing, layout, and rendering are delegated to Lasem.
lasem is intended for Ruby applications that need an embeddable interface to
Lasem-supported rendering without shelling out to the lasem-render
executable.
The source repository is named lasem-ruby to make the language binding
clear; the gem itself is named lasem because the public Ruby API is already
namespaced as Lasem.
lasem is a native extension. Ruby 3.3 or newer is required, and the upstream
Lasem C library must be available for rendering support.
The released gem does not package or build the upstream Lasem C library. If
Lasem is missing when the gem is installed, installation can still succeed with
a stub extension, but Lasem.render raises Lasem::DependencyError until
Lasem is installed and the extension is rebuilt.
Install your operating system’s Lasem development package when one is available. The package lists below cover the Ruby native extension toolchain, the vendored Lasem source-checkout build, and fonts commonly needed for math rendering.
sudo apt-get install build-essential ruby-dev pkg-config meson ninja-build \
bison flex gettext libglib2.0-dev libgdk-pixbuf-2.0-dev libcairo2-dev \
libpango1.0-dev libxml2-dev fonts-lyxsudo dnf install gcc ruby-devel pkgconf-pkg-config meson ninja-build \
bison flex gettext glib2-devel gdk-pixbuf2-devel cairo-devel pango-devel \
libxml2-devel lyx-fontsRun lasem-doctor --all-warnings after installation if rendering is
unavailable or the gem reports missing native dependencies.
Add to your application’s Gemfile:
gem "lasem"Then:
bundle installOr install directly:
gem install lasemLasem itself is a native C library and is not bundled with the gem. See Prerequisites before installing the gem.
Lasem.render(source, input: :xml, output: :svg, **options) # => StringReturns the rendered output as a binary string in the requested format.
input-
Source language. One of
:xml,:mathml,:svg,:latex,:itex. Default:xml.:xml,:mathml, and:svgare equivalent: all are parsed by Lasem’s XML document parser, which auto-detects MathML vs SVG from the document.:latexand:itexare passed unchanged to Lasem’s itex parser; the gem does not infer or add math delimiters. output-
Target format. One of
:svg,:png,:pdf,:ps. Default:svg. ppi-
Pixels per inch used for rasterized output. Positive float. Default
72.0. zoom-
Render scale factor. Positive float. Default
1.0. width,height-
Optional fixed output dimensions in user units. Must be supplied together. Default
nil(use Lasem’s natural size). offset_x,offset_y-
Optional translation in user units. Default
0.0. At the defaultzoom: 1.0the on-canvas displacement equals the offset; with a non-defaultzoomthe offset is applied in the zoom-scaled coordinate system, so the effective displacement also scales withzoom.
mathml = <<~XML
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>A</mi>
<mo>=</mo>
<mfenced open="[" close="]">
<mtable>
<mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
<mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
<mtr><mtd><mn>7</mn></mtd><mtd><mn>8</mn></mtd><mtd><mn>9</mn></mtd></mtr>
</mtable>
</mfenced>
</mrow>
</math>
XML
Lasem.render(mathml, input: :mathml, output: :png, ppi: 192.0)svg = Lasem.render(mathml, input: :mathml, output: :svg)
File.write("equation.svg", svg)The script that produced the images above is at
docs/images/render_samples.rb.
The released gem does not package or build the upstream Lasem C library. Install Lasem before installing this gem when possible, so the native extension can link against it at install time.
The native build resolves Lasem in this order:
-
A source-checkout vendored Lasem install under
vendor/lasem/install, when present. -
A system Lasem package discovered with
pkg-config. -
A compiled stub extension that raises
Lasem::DependencyErrorwhen called.
Released gems do not include the vendored Lasem source, so installed gems
normally resolve Lasem through the system pkg-config path.
The stub keeps require "lasem" working on machines that do not have Lasem
yet, while making rendering failures explicit.
If the gem was installed before Lasem was available, rebuild the native extension once Lasem is in place:
gem pristine lasem --extensionsWith Bundler:
bundle pristine lasemFor a source checkout:
bundle exec rake clean compileVerify the result:
ruby -rlasem -e 'p Lasem.native_available?'Run the bundled doctor:
bundle exec lasem-doctor --all-warningsIt reports missing executables and missing pkg-config packages, and with
--all-warnings (or --lasem-conflict-warnings) it also reports submodule and
setup state. The line Required dependencies look available. is printed when
the required toolchain is present; any warnings are listed after the status
section. A successful dependency check does not by itself guarantee the native
extension is built — verify that separately with Lasem.native_available?.
The same checks are available through Rake, where the WARNINGS variable maps
to the CLI flags: WARNINGS=all (all warnings), WARNINGS=lasem (Lasem setup
warnings), WARNINGS=deps (dependency warnings):
bundle exec rake lasem:doctor WARNINGS=allIf Lasem.render raises Lasem::DependencyError, the gem is loading the stub
extension. Rebuild after installing Lasem (see Native dependency resolution).
The vendored build is a development helper for source checkouts. It is not part of normal gem installation.
git submodule update --init vendor/lasem/source
bundle exec rake lasem:doctor
bundle exec rake lasem:build
bundle exec rake clean compileThe build task installs Lasem into vendor/lasem/install. The native
extension adds that install’s pkgconfig directory to PKG_CONFIG_PATH and
embeds the vendored library directory as a runtime library path.
The vendored Lasem source is licensed under LGPL-2.1-or-later. Keep Lasem’s license files with the vendored source when updating it.
Ruby 3.3 or newer is required.
bundle install
bundle exec rake # runs RSpec
bundle exec rubocopRendering specs are skipped unless the native extension is built against a
usable Lasem. In a source checkout, build the vendored Lasem first (see
Vendored Lasem (source checkout)) — that is the development
step that requires git submodule update --init vendor/lasem/source.
Environment variables understood by the build:
LASEM_PKG_CONFIG-
Override the pkg-config package name (for example
lasem-0.6). LASEM_SOURCE_DIR-
Override the vendored source directory used by
rake lasem:build. LASEM_BUILD_DIR-
Override the Meson build directory used by
rake lasem:build. LASEM_INSTALL_DIR-
Override the install prefix used by
rake lasem:buildand the native extension.
Copyright Ribose.
lasem (the Ruby gem) is licensed under the BSD 2-Clause license. See
LICENSE.txt for details.
The vendored Lasem C library is licensed under LGPL-2.1-or-later. Its license
files ship with the vendored source under vendor/lasem/source/.



