Renders KaTeX from Ruby.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
lib fix rubocop Layout/EmptyLineAfterGuardClause Dec 3, 2018
spec
vendor/katex
.codeclimate.yml Coverage Jan 17, 2017
.gitignore bundle gem katex Jan 15, 2017
.rspec bundle gem katex Jan 15, 2017
.rubocop.yml Add rubocop Jan 15, 2017
.simplecov Coverage Jan 17, 2017
.travis.yml Coverage Jan 17, 2017
CHANGES.md KaTeX v0.9.0 Apr 16, 2018
CODE_OF_CONDUCT.md bundle gem katex Jan 15, 2017
Gemfile rubocop -a Aug 15, 2017
LICENSE.txt bundle gem katex Jan 15, 2017
README.md
Rakefile rubocop Apr 16, 2018
katex.gemspec rubocop Apr 16, 2018

README.md

KaTeX for Ruby Build Status Test Coverage Code Climate

This rubygem enables you to render TeX math to HTML using KaTeX. It uses ExecJS under the hood.

Installation

Add this line to your application's Gemfile:

gem 'katex', '~> 0.5.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install katex

Usage

Render some math:

Katex.render 'c = \\pm\\sqrt{a^2 + b^2}'
#=> "<span class=\"katex\">..."

If you're on Rails, the result is marked as html_safe.

Any error in the markup is raised by default. To avoid this and render error text instead, pass throw_on_error: false:

Katex.render '\\', throw_on_error: false

Note that this will catch even ParseErrors (unlike native KaTeX).

Learn more about all the available options in the documentation.

Assets

For this rendered math to look nice, you will also need to include KaTeX CSS into the webpage.

I recommend you use the CSS bundled with this gem, to ensure version compatibility.

Automatic registrations

If you use Rails, Sprockets without Rails, or Hanami, the assets are registered automatically.

Simply //= require katex if you use CSS or @import "_katex" if you use Sass.

You can also //= require katex in your JS to access the KaTeX renderer in the browser.

Manual registration

The assets are located in the vendor/assets directory of the gem. The root path to the root directory of the gem is available via Katex.gem_path, e.g.:

File.join(Katex.gem_path, 'vendor', 'assets')

KaTeX version

The version of KaTeX bundled with this gem is available via:

Katex::KATEX_VERSION

Caching

If you cache the output of Katex.render, make sure to use the KaTeX version in the cache key, as the output may change between versions.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/glebm/katex-ruby.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.