Coder Social home page Coder Social logo

ngx-mathjax's Introduction

ngx-mathjax CircleCI

Integrate MathJax with Angular.

NOTE: The MathJax v3 API is not fully componentized and some API features rely on the global library state. I found it is very difficult to integrate the v3 API with Angular. Therefore, the plan to adopt to the new API is on hold.

Feature

  • Dynamically load MathJax library to your web application.
  • Simple typesetting using Angular directive.
  • Dynamic typesetting using expressions.

Install

npm install ngx-mathjax

Configure the module

Load the module in the @NgModule class of the application. You need to pass a ModuleConfiguration instance to the config method to configure the module.

Example

When importing in the root module, the module should be configured with .forRoot method.

import {NgModule} from '@angular/core';
import {AppComponent} from './src/app/app.component';
import {MathJaxModule} from './src/app/math-jax/math-jax.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    MathJaxModule.forRoot({
      version: '2.7.5',
      config: 'TeX-AMS_HTML',
      hostname: 'cdnjs.cloudflare.com',
      mathjaxconfigobject : {
          'HTML-CSS': {
            styles: {
              '.MathJax_Display': {
                'background-color': 'yellow',
              },
            },
          },
        },
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • version is the MathJax release version.
  • config is the MathJax predefined configuration name.
  • hostname is the MathJax CDN hostname.
  • mathjaxconfigobject is the MathJax Configuration Object allowing you to directly influence the Typesetting and other options (which you can read more about at 6).

When importing in a child module, the module must be configured to re-use the same module instance as the root module. So simply configure the module with the .forChild method.

import {MathJaxModule} from './src/app/math-jax/math-jax.module';

...
imports: [
  MathJaxModule.forChild()
]
...

Typeset an element

Add the mathjax directive to elements which you want to apply MathJax typesetting on load.

<div>normal text</div>

<div mathjax>mathjax typesetting
$$
x = 1
$$

\( y = 2 \)
</div>

Typesetting using expression

The Jax elements will be updated when the corresponding expression value is changed. The correspondence principle between the expression and the Jax element is by order.

<div [mathjax]="[exp1, exp2]">

MathJax Expression 1: \( {{ '{}' }} \)
MathJax Expression 2: \( {{ '{}' }} \)
</div>

Insert the {} to the place you want, then surround it with a pair of MathJax delimiter.

You need to escape it in Angular template.

Manually trigger MathJax typesetting

You can use the MathJaxDirective.MathJaxTypeset() method to trigger the typesetting when you want. The steps are:

  • Use ViewChild and its read property to get a reference to the MathJaxDirective instance
  • Call instance.MathJaxTypeset()

Adding a callback

You can specify a callback function to be called when the rendering is finished

component.ts

export class yourComponent implements OnInit {
  callback = function () {
      console.log("Callback function called!")
    }
}

template or html

<div mathjax (mathjax-callback)="callback()">
    \\( E = mc^2 \\)
</div>

TODO

empty

ngx-mathjax's People

Contributors

davidshen84 avatar dependabot[bot] avatar jayandbee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ngx-mathjax's Issues

'mathjaxconfigobject' does not exist in type 'ModuleConfiguration'

Argument of type '{ version: string; config: string; hostname: string; mathjaxconfigobject: {}; }' is not assignable to parameter of type 'ModuleConfiguration'.
  Object literal may only specify known properties, and 'mathjaxconfigobject' does not exist in type 'ModuleConfiguration'.ts(2345)

As seen in types

export declare class ModuleConfiguration {
    /**
     * The version of the MathJax library.
     */
    version: string;
    /**
     * The config name of the MathJax library.
     * Please check the MathJax [documentation](https://docs.mathjax.org/en/latest/config-files.html) for all the configuration names.
     */
    config: string;
    /**
     * MathJax CDN hostname. Please check [here](https://docs.mathjax.org/en/latest/start.html#mathjax-cdn) for available CDN servers.
     */
    hostname: string;
}

need to add definition for mathjaxconfigobject.

EDIT:

I cloned your repo, and mathjaxconfigobject is defined in ModuleConfiguration. I think you just need to publish.

EDIT2:
It's already in this merge.
bd884dd

Allow manually trigger MathJax typeset

When used in conjunction with other components that would modify their content, e.g. a markdown typesetting component, MathJax typesetting should happen the last.

MathMl format doesn't work

I can't make this to work

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mfrac>
          <mn>1</mn><mn>2</mn>
   </mfrac>
</math>

even when I setup this configuration

  MathJaxModule.forRoot({
      version: '2.7.7',
      config: 'TeX-MML-AM_HTMLorMML',
      hostname: 'cdnjs.cloudflare.com'
    }),

Manually trigger MathJax typesetting

I am trying to load mathjax like this

<div mathjax [innerHTML]="mmlText | safeHtml: 'html'"></div>

It renders fine for the first load but when mmlText values changes then it does not render.

I have tried trigger MathJax typesetting after changing the variable value but it does not work.
@ViewChild(MathJaxDirective, { static: false }) private questionStatement: MathJaxDirective; ... changeVar(){ this.mmlText = 'new mml tags here'; this.questionStatement.MathJaxTypeset(); }

How to add config data

Hi,

great software, works really nice in my webapp. Just one thing: do you know how I can add some config options to the mathjax call? I would like to set CSS output options of the css class "MathJax_Display:" to "margin: 0em".

Best.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.