简体   繁体   English

如何让Mathjax与Angular2一起使用?

[英]How to get Mathjax working with Angular2?

Has anyone gotten Mathjax working with Angular2 ? 有没有人让Mathjax与Angular2合作?

Plunkr example created :- http://plnkr.co/edit/FLduwmtHfkCN5XPfzMsA?p=preview Plunkr示例创建: - http://plnkr.co/edit/FLduwmtHfkCN5XPfzMsA?p=preview

From some Angular1 examples I saw it looks like a directive is needed to call MathJax.Hub.Queue, but I suspect it will take me quite a while to get the Angular 2 syntax right, so I wondered if anyone has already done it ? 从一些Angular1示例中我看到它看起来需要一个指令来调用MathJax.Hub.Queue,但我怀疑我需要花很长时间才能使Angular 2语法正确,所以我想知道是否有人已经完成了它?

eg the following is an Angular 1 example. 例如,以下是Angular 1示例。 https://github.com/ColCarroll/ngMathJax/blob/master/ng-mathjax.js https://github.com/ColCarroll/ngMathJax/blob/master/ng-mathjax.js

And the mathjax syntax is here :- https://docs.mathjax.org/en/v1.1-latest/typeset.html mathjax语法在这里: - https://docs.mathjax.org/en/v1.1-latest/typeset.html

Trying to do something similar in Angular2. 尝试在Angular2中做类似的事情。

UPDATE - the following works, thanks to Thierry. 更新 - 感谢Thierry,以下工作。

Component:- 零件:-

import {Component, OnInit} from 'angular2/core';
import {MathJaxDirective} from './mathjax.directive';

@Component({
  selector: 'hello-mathjax',
  templateUrl: 'app/hello_mathjax.html',
  directives: [MathJaxDirective]
})
export class HelloMathjax {
  fractionString: string = 'Inside Angular one half = $\\frac 12$';
  index: number = 3;

    ngOnInit() {
        MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathJax"]);
    }

    update () {
      this.fractionString = 'Inside Angular one third = $\\frac 1'+this.index+'$';
      this.index++;
    }

}

Directive:- 指示:-

import {Directive, ElementRef, Input} from 'angular2/core';
@Directive({
    selector: '[MathJax]'
})
export class MathJaxDirective {
    @Input('MathJax') fractionString: string;

    constructor(private el: ElementRef) {
    }

    ngOnChanges() {
      console.log('>> ngOnChanges');
       this.el.nativeElement.style.backgroundColor = 'yellow';
       this.el.nativeElement.innerHTML = this.fractionString;
       MathJax.Hub.Queue(["Typeset",MathJax.Hub, this.el.nativeElement]);
    }
}

Still not sure why I need to queue the re-render in both places. 仍然不确定为什么我需要在两个地方排队重新渲染。

I would implement this way with an input to get the specified expression: 我将使用输入实现这种方式来获取指定的表达式:

import {Directive, ElementRef, Input} from 'angular2/core';
@Directive({
    selector: '[MathJax]'
})
export class MathJaxDirective {
    @Input(' MathJax')
    texExpression:string;

    constructor(private el: ElementRef) {
    }

    ngOnChanges() {
       this.el.nativeElement.innerHTML = this.texExpression;
       MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.el.nativeElement]);
    }
}

And use this way: 并使用这种方式:

<textarea #txt></textarea>
<span [MathJax]="txt.value"></span>

See this plunkr: http://plnkr.co/edit/qBRAIxR27zK3bpo6QipY?p=preview . 请参阅此plunkr: http ://plnkr.co/edit/qBRAIxR27zK3bpo6QipY?p = preview。

Based on this question, I started the development of an open source project to typeset TeX math expressions with Angular. 基于这个问题,我开始开发一个开源项目,用Angular排版TeX数学表达式。 The project is on https://github.com/garciparedes/ng-katex . 该项目位于https://github.com/garciparedes/ng-katex

You can install the module with: 您可以使用以下命令安装模块:

npm install ng-katex --save

To add the module to your proyect add the KatexModule to import 's field of your parent module: 要将模块添加到proyect,请将KatexModule添加到父模块的import字段中:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';

import { KatexModule } from 'ng-katex';

@NgModule({
  imports: [BrowserModule, KatexModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})

class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

And then you can use it as follows: 然后你可以按如下方式使用它:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<ng-katex [equation]="equation">`
})
export class AppComponent {
  equation: string = ''\sum_{i=1}^nx_i'';
}

Used Mathjax in my project in Angular 2 as follows: 在我的Angular 2项目中使用Mathjax如下:

setTimeout(function () { MathJax.Hub.Queue(["Typeset", MathJax.Hub]);}, 5); setTimeout(function(){MathJax.Hub.Queue([“Typeset”,MathJax.Hub]);},5);

as the queue runs asynchronously, the setTimeout ensures the math rendering process happens after a certain point of time. 当队列异步运行时,setTimeout确保数学渲染过程在某个时间点之后发生。

The problem is that template generator of angular 2 looking for end of file char '{' or finds something unexpected inside the html file at the end of file(EOF). 问题是,角度为2的模板生成器正在寻找文件结尾char'{'或在文件末尾的html文件中找到意外的东西(EOF)。

In order to be able to use MathJax with Angular 2 template generator: 为了能够将MathJax与Angular 2模板生成器一起使用:

<h1>example not working = $$ 2^{x + 2 } $$</h1>

<h1>example working = $$ 2^{{ '{' }} x + 2 {{ '}' }} $$</h1>

Just use {{ '{' }} or {{ '}' }} , instead of simple '{' inside the html tags. 只需使用{{'{'}}或{{'}'}},而不是html标记内的简单“{”。

Dont forget to add this to your index.html tags : 别忘了将它添加到index.html标记中:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM