Giter Site home page Giter Site logo

garciparedes / ng-katex Goto Github PK

View Code? Open in Web Editor NEW
101.0 5.0 38.0 17.23 MB

Angular module to write beautiful math expressions in TeX syntax boosted by KaTeX library

Home Page: https://garciparedes.me/ng-katex/

License: MIT License

TypeScript 76.46% JavaScript 15.74% HTML 5.90% CSS 0.63% SCSS 1.27%
angular angular-components angular-directives katex latex math ng-katex latex-equations

ng-katex's Introduction

ng-KaTeX

npm Travis npm GitHub stars npm

Description

Angular module to write beautiful math expressions in TeX syntax boosted by KaTeX library. You can see a demo here.

Install

To install the module you can simply type it on your command line:

npm install ng-katex --save

To add the module to your project add the KatexModule to import's field of your parent module:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { KatexModule } from 'ng-katex';

import { AppComponent } from './app/app.component';

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

class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);

Important!

If you're using angular-cli, add the katex css import to your styles.css:

@import '~katex/dist/katex.css';

If you're not using the angular-cli, import the stylesheet to your index.html:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.0/katex.min.css">

Usage

You can write a LaTeX equation as follows:

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

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

Also, you can add options to ng-katex components with:

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

import { KatexOptions } from 'ng-katex';

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

The options object structure is defined here.

If you want to write a paragraph with LaTeX equations, you can do it as follows:

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

@Component({
  selector: 'my-app',
  template: `<ng-katex-paragraph [paragraph]="paragraph"></ng-katex-paragraph>`
})
export class AppComponent {
  paragraph: string = `
    You can write text, that contains expressions like this: $x ^ 2 + 5$ inside them. As you probably know.
    You also can write expressions in display mode as follows: $$\\sum_{i=1}^n(x_i^2 - \\overline{x}^2)$$.
    In first case you will need to use \\$expression\\$ and in the second one \\$\\$expression\\$\\$.
    To scape the \\$ symbol it's mandatory to write as follows: \\\\$
  `;
}

If you want to write HTML with LaTeX equations, you can do it as follows: (Security Note: this bypasses Angular DOM Sanitization)

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

@Component({
  selector: 'my-app',
  template: `<ng-katex-html [html]="html"></ng-katex-html>`
})
export class AppComponent {
  html: string = `
    <div>You can write html, that contains expressions like this: $x ^ 2 + 5$ inside them. As you probably know. You also can write expressions in display mode as follows: $$\\sum_{i=1}^n(x_i^2 - \\overline{x}^2)$$. In first case you will need to use \\$expression\\$ and in the second one \\$\\$expression\\$\\$. To scape the \\$ symbol it's mandatory to write as follows: \\\\$</div><p>: <button>I'm a button</button></p>
  `;
}

Contributors

Changelog

See changelog page to get info about release changes.

Contributing

See CONTRIBUTING.md

License

ng-katex is licensed under MIT license.

ng-katex's People

Contributors

alexis-mrc avatar dependabot[bot] avatar garciparedes avatar joshclaxton avatar rarkins avatar renovate-bot avatar twodcube avatar vberlier avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

ng-katex's Issues

Error with universal

The SSR (universal) build of the application gives me the following error.

ERROR { Error: Uncaught (in promise): Error: StaticInjectorError[n]: 
  StaticInjectorError[n]: 
    NullInjectorError: No provider for n!

It's weird because universal usually errors out for usages of document at runtime. This is an AOT error, but the ng serve --aot works just fine for me. Seems like metadata or something has not been configured properly. A good place to start is to make sure the lib complies to Angular Package Format. The following two repos help with generating such a library: https://github.com/dherges/ng-packagr and https://github.com/steveblue/angular2-rollup.

Make ng-katex work with Angular 9

Hello !

Once again, I would like to thank you for the quality of your library, it is so helpful for my work.

I upgraded my project to Angular 9 yesterday. Unfortunately ng-katex is not working with this new version of Angular.

A minimal repro could be upgrading ng-katex demo app to Angular 9 :
image

The only way to make it work is to disable Ivy compiler (new and enabled by default in Angular 9).
I tried to look at the issue here : https://github.com/angular/angular/issues?utf8=%E2%9C%93&q=is%3Aissue+ng6002

but unfortunately the issue seems to be highly technical...

By any chance, is there someone that could help make this great library work in Angular 9?

Thanks !

Serge

how to support longdiv macros?

I add options in component:

options: KatexOptions = {
    displayMode: true,
    throwOnError: false,
    macros: {
      '\\longdiv': '\\Large{#1\\overline{\\smash{\\Big)}#2}}'
    }
  };

but still get error: KaTeX parse error: Undefined control sequence: \longdiv at position 2: \̲l̲o̲n̲g̲d̲i̲v̲{1}{2}

KatexModule is not an NgModule

I am getting this error when trying to start the dev Server fresh with ng serve. Oddly enough it works if it's already running and I just add it as a module afterwards.
Version: v0.3.21

So I tried with v0.3.30 and got another error:
ERROR in Error: Metadata version mismatch for module ./node_modules/ng-katex/ng-katex.d.ts, found version 4, expected 3, resolving symbol ...

I am on Angular 4.3.6.

Any idea?

Thanks for the project, looking good!

Error: 'render' is not exported after running `yarn run ngl build`

After trying to build a component which uses angular-librarian for the Angular 2+ scaffolding setup and uses ng-katex as a dependency I receive the following error after running yarn run ngl build.

I made a basic component to reproduce the error, you can find the structure here:

https://github.com/milmazz/kat

D:\kat [master +19 ~0 -0 !]> yarn run ngl build
yarn run v1.5.1
$ D:\kat\node_modules\.bin\ngl build

> [email protected] prebuild D:\kat
> rimraf dist out-tsc


> [email protected] build D:\kat
> node ./tasks/build

Copying `src` files into `build`
> Complete
Inlining resources
> Complete
Compiling code
> Complete
Copying typings + metadata to `dist`
> Complete
Generating bundles via rollup
[Librarian]: > Build failed

{ Error: 'render' is not exported by node_modules\katex\dist\katex.js
    at error (D:\kat\node_modules\rollup\dist\rollup.js:189:14)
    at Module.error (D:\kat\node_modules\rollup\dist\rollup.js:16532:3)
    at Module.trace (D:\kat\node_modules\rollup\dist\rollup.js:16629:10)
    at ModuleScope.findVariable (D:\kat\node_modules\rollup\dist\rollup.js:13971:22)
    at FunctionScope.findVariable (D:\kat\node_modules\rollup\dist\rollup.js:11545:33)
    at Scope.findVariable (D:\kat\node_modules\rollup\dist\rollup.js:11545:33)
    at FunctionScope.findVariable (D:\kat\node_modules\rollup\dist\rollup.js:11545:33)
    at Scope.findVariable (D:\kat\node_modules\rollup\dist\rollup.js:11545:33)
    at Node._bindVariableIfMissing (D:\kat\node_modules\rollup\dist\rollup.js:12628:31)
    at Node.bindNode (D:\kat\node_modules\rollup\dist\rollup.js:12623:8)
  code: 'MISSING_EXPORT',
  url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module',
  pos: 111,
  loc:
   { file: 'D:\\kat\\node_modules\\ng-katex\\ng-katex.esm.js',
     line: 1,
     column: 111 },
  frame: '1: import{Component,EventEmitter,Input,Output,Injectable,Directive,ElementRef,NgModule}from"@angular/core";import{render}from"katex";function __decorate(e,t,o,n){var r,a=arguments.length,i=a<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,o,n);else for(var p=e.length-1;p>=0;p--)(r=e[p])&&(i=(a<3?r(i):a>3?r(t,o,i):r(t,o))||i);return a>3&&i&&Object.defineProperty(t,o,i),i}function __metadata(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}var KatexComponent=function(){function e(){this.onError=new EventEmitter}return e.prototype.hasError=function(e){this.onError.emit(e)},__decorate([Input(),__metadata("design:type",String)],e.prototype,"equation",void 0),__decorate([Input(),__metadata("design:type",Object)],e.prototype,"options",void 0),__decorate([Output(),__metadata("design:type",Object)],e.prototype,"onError",void 0),e=__decorate([Component({selector:"ng-katex",template:\'\\n    <span [katex]="equation"\\n          [katex-options]="options"\\n          (someEvent)="hasError($event)">\\n    </span>\\n  \'})],e)}(),KatexService=function(){function e(){}return e.prototype.render=function(e,t,o){return render(e,t.nativeElement,o)},e=__decorate([Injectable()],e)}(),KatexDirective=function(){function e(e,t){this.el=e,this.katexService=t,this.onError=new EventEmitter}return e.prototype.ngOnChanges=function(){try{this.katexService.render(this.equation,this.el,this.options)}catch(e){this.onError.emit(e)}},__decorate([Input("katex"),__metadata("design:type",String)],e.prototype,"equation",void 0),__decorate([Input("katex-options"),__metadata("design:type",Object)],e.prototype,"options",void 0),__decorate([Output(),__metadata("design:type",Object)],e.prototype,"onError",void 0),e=__decorate([Directive({selector:"[katex]"}),__metadata("design:paramtypes",[ElementRef,KatexService])],e)}(),KatexModule=function(){function e(){}return e=__decorate([NgModule({imports:[],providers:[KatexService],declarations:[KatexDirective,KatexComponent],exports:[KatexComponent]})],e)}();export{KatexComponent,KatexModule};\n                                                                                                                  ^' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `node ./tasks/build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

Done in 17.49s.

The error message points to this rollup Troubleshooting guide:

https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module

Which states the following:

Import declarations must have corresponding export declarations in the imported module. For example, if you have import a from './a.js' in a module, and a.js doesn't have an export default declaration, or import {foo} from './b.js', and b.js doesn't export foo, Rollup cannot bundle the code.

But, this seems under the ng-katex control, is that right?

How can I fix this error? Any help is greatly appreciated it.

As a side note, everything seems to works fine when I run yarn run ngl serve, but I really want to build and pack my library.

Automate building

  • Change path from node_modules/katex to ../katex
  • Remove *.ngfactory.ts files

Some equation can not been shown in right way with ionic framework.

Hi,
When I use ng-katex in ionic framework some equation can not been displayed in the right way.
For example, x = {-b \pm \sqrt{b^2-4ac} \over 2a} .
It can be shown in Angular project normally ,but in ionic project the position of b^2 is out of sqrt mark.
Can ng-katex be used in ionic project?

Any advice will be appreciate .
Thanks a lot .

IE11 Support?

KaTeX supports IE9-11 but either ng-katex doesn't or I forgot something? Polyfills?
ERROR TypeError: The object doesn't support the property or method "createShadowRoot (roughly translated).
Btw the demo doesn't start at all on IE11. (different error though)

Not equal \\ne do not render (= and blank square instead)

HTML:
<ng-katex [equation]="exp"></ng-katex>

TS:
exp = '\\ne';

Result:
Zrzut ekranu 2019-03-25 o 22 13 25

I'm using Google Chrome and Safari on macOS.
Typing 'not equal' unicode's char in ng-katex equation will cause the same result.
Unicode char in HTML is rendering properly.

Changing Delimiters

As far I'm aware from reading the docs, there not really a good way to change the delimiters from the component.ts files. There's a way to do from the index.html, but I haven't found a way to get rid of the '$' '$' delimiters. An easy way to change the delimiters in the component.ts (passing in the options object) would be a good idea

New release version 1.1.0 - Can't find module 'ng-katex'

New release doesn't work like ^1.0.3 as written in readme for demo app and my project as well.
Installing or updating the version to 1.1.0 throws an error Can't find module 'ng-katex'.
Also, the structure of ng-katex under node modules is different.

Can someone tell me how to work with new version?

Thanks!

Implement "auto-parser" paragraph

The new component should receive an string (as parameter or by <ng-content>) and then, it should render all expressions delimited by $ symbol, like in Tex language

Matrix brackets

Almost all matrices have wrong brackets

"c = \begin{pmatrix} a & b \\ c & d\\ c & d\end{pmatrix}"
image
"c = \begin{bmatrix} a & b \\ c & d\\ c & d\end{bmatrix}"
image

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

[options] in ng-katex-paragraph

Hello !

I'm using ng-katex in place of MathJax, thanks for the good work!

I'd have a suggestion : in ng-katex-paragraph directive, would it be possible to pass in [options], just like in ng-katex?

Thanks :)

Serge

For some cases it doesn't support latex inputs which katex supports.

take the example of this latex-
'{\text{ In a solid }AB\text{ having the NaCl structure, }A\text{ atoms occupy the }}\text{ corners of the cubic unit cell. If all the face centred atoms along }\text{ one of the axes are removed, then the resultant stoichiometr. }\text{ of the solid is }{\text{ (a) }AB_2\text{ (b) }A_2B\quad\text{ (c) }A_4B_3\text{ (d) }A_3B_4}'

It is getting rendered on katex demo page( https://katex.org/ ) but not with ng-katex.

How to get ng-katex to work in stackblitz

I attempted to get a sample stackblitz up and running to play around with ng-katex but it does not render some of the equations properly. The same equations work fine on https://garciparedes.me/ng-katex/

I suspect it is a CSS import issue. Maybe something that the docs haven't mentioned from this perspective yet? Would you mind taking a look and letting me know if there's a bug that was exposed or a configuration issue: https://stackblitz.com/edit/try-ng-katex

not showing Fraction line

I tried to display fractions but it shows like this.

screenshot-localhost-4200-2018 03 08-07-56-50

The line between fractions isn't displayed.
Please give some advice for this.

Warning at npm install

The warnings is as follows:

npm WARN enoent ENOENT: no such file or directory, open 'ng-katex/dist/node_modules/@angular/core/package.json'
npm WARN enoent ENOENT: no such file or directory, open 'ng-katex/dist/node_modules/rxjs/package.json'

ng-katex has problem with angular ivy

Describe the bug
A clear and concise description of what the bug is.

ng build succeed with ivy disabled but got following error when ivy is enabled.

Compiling @angular/flex-layout/grid : es2015 as esm2015
Compiling @angular/flex-layout : es2015 as esm2015
Compiling ng-katex : module as esm5

ERROR in node_modules/ng-katex/src/ng-katex.module.d.ts:1:22 - error TS-996002: Appears in the NgModule.imports of CalculateAreaModule, but could not be resolved to an NgModule class

I have refresh installed all packages (by delete the node-modules folder) but still cannot make it compile with ivy.

my system:
Angular CLI: 8.3.17
Node: 12.13.0
OS: win32 x64
Angular: 8.2.13

Please take a looks at this issue. Thank you !

Fonts issue

When trying to use this as documentation suggests, I'm getting errors about missing fonts.

image

It's unclear how to proceed.

Error: katex importing browsermodule

Describe the bug
ng-katex giving error when importing it to NgModule

Steps to reproduce the error

  • npm install ng-katet
  • import into any module, (lazily loaded in my case)
  • compile or build

Error produced

ERROR Error: Uncaught (in promise): Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.
Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.

Export the KatexService as part of the Public API Surface of ng-katex

Is your feature request related to a problem? Please describe.
Certain requirements needs the renderToString() and render() function currently in the KatexService

for instance a requirement to include katex in a text-editor will need the renderToString() function in the KatexService,
The current implementation hides the KatexService which prevents the use of the renderToString().

Describe the solution you'd like
Export the KatexService as part of the Public API Surface of ng-katex

Describe alternatives you've considered
I considered including the ng-katex project as a library in my current project and make the update from my local copy of the ng-katex

Options: use the new 'output' option of Katex (>= v0.11.0)

Hello,

I'm using ng-katex on a little project I'm doing for fun. Very useful.

I realized that the options of ng-katex does not implement the 'output' options, newly introduced since Katex v.0.11.0. By default, Katex is rendering both in the same string.

I think it would be nice to have it, and avoid having the MathML and the HTML version printed every time.

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.