1ère Générale NSI

 

Term. Générale NSI

 

Terminale STI2D SIN

Bts Ccst

Technico-commercial 3.0

import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class TlatexService { //recupere le head du doc head : string; //recupere le body du doc body : string; //position de la recherche posi:number; //Posi retour ligne latex posiReLigne :number; paragraph=false; //recupere le charactere retour ligne latex charReLigne : string; constructor() { } /********************************* * traiter latex *********************************/ traiterLatex(data:string):string { // let lg = this.longChaine("\\begin{document}"); //recherche le debut du document let debTexte = data.indexOf("\\begin{document}"); //recupere le head du doc this.head = data.slice(0,debTexte); //recupere le body du doc this.body = data.slice(debTexte+lg); //position de la recherche this.posi = 0; //recupere le charactere retour ligne latex this.posiReLigne = this.head.indexOf('}')+1; this.charReLigne = this.head.charAt(this.posiReLigne); //recupere les instruction head this.traiterInstHead(); this.nettoyerBody(); // this.chargescript(); let re = new RegExp(this.charReLigne, 'g'); this.head = this.head.replace(re,'
'); return this.body;//body; } /************************************ * traite les instructions du head ***********************************/ traiterInstHead(){ let nwc = "\\newcommand"; let cnt = "\\newcounter"; let lgnwc = nwc.length; let lgcnt = cnt.length; let posinwc = 1; let posicnt = 1; let boucle=12; posinwc = this.head.indexOf(nwc,this.posi+1); this.posi = posinwc+lgnwc; while (posinwc>0) { this.newcommand(); posinwc = this.head.indexOf(nwc,this.posi+1); this.posi = posinwc+lgnwc; }//fin while }//fin traiter /******************************************* * newcommand ****************************************/ newcommand() { let tagchercher = this.entreacco(); let tagnouveau = this.entreacco(); if(tagnouveau!="0") { this.head = this.remplace(this.head,tagchercher+"{",tagnouveau+"{",this.posi); if(this.paragraph==false) { this.body = this.remplace(this.body,tagchercher,tagnouveau,0); } else { this.paragraph=false; this.body = this.remplace(this.body,tagchercher,"

"+tagnouveau+"

",0); } this.posi--; //alert(this.posi+' '+tagchercher+' '+tagnouveau); } }//fin newcommand /******************************************* * recherche et remplace tag dans texte ****************************************/ remplace(texte:string,tagchercher:string,tagnouveau:string,positag=0):string{ positag = texte.indexOf(tagchercher,positag); while (positag>0) { texte = this.insert(texte,tagnouveau,positag,tagchercher.length); positag = texte.indexOf(tagchercher,positag+1); } return texte; } /******************************************* * renvoie la chaine entre les accolades ****************************************/ entreacco() { //let accoo = 0; //let accof = 0; let caract =""; let numacco = 0; let nonslash = true; let tag =""; let nbmax=50; do { caract = this.head[this.posi]; console.log(caract); if(caract=="\\") { nonslash = false; tag+=caract; } else if(caract=='{') { if(nonslash) { numacco++; if(numacco!=1) tag+=caract; } else { tag+=caract; nonslash=true; } } else if(caract=='}') { if(nonslash) {numacco--; if(numacco!=0) tag+=caract; } else { nonslash=true; tag+=caract; } } else if(caract==this.charReLigne) { numacco=-1; } else if(caract== '[') { numacco=-2; } else { tag+=caract; nonslash=true; } this.posi++; nbmax--; } while((numacco>0)&&(nbmax>0)); if(numacco==-2) { //recherche la balise paragraph this.paragraph=true; let balise = "\\paragraph{"; let posipara = this.head.indexOf(balise,this.posi); if (posipara>0) { let accof = this.head.indexOf('}',posipara+balise.length); tag = this.head.slice(posipara+balise.length,accof); numacco=0; //alert(tag); } } if(numacco==0) { return tag; } else { return "0"; } }//fin entreacco /************************************** * Remplacer les tag du body ******************************************/ nettoyerBody() { this.body = this.body.replace(/\\hfill\{([^\}]+)\}/g,'

$1

'); this.body = this.body.replace(/\\LARGE/g,''); this.body = this.body.replace(/\\medskip/g,'
  '); this.body = this.body.replace(/\\noindent/g,'
  '); this.body = this.body.replace(/\\bf/g,''); this.body = this.body.replace(/\\section\{([^\}]+)\}/g,'

$1

'); this.body = this.body.replace(/\\subsection\{([^\}]+)\}/g,'

$1

'); this.body = this.body.replace(/%[^\n]*(?=\n)/g,""); // remove LaTeX comments this.body = this.body.replace(/\\vspace[^\n]*(?=\n)/g,""); //remove comments this.body = this.body.replace(/\\hfill([^\}]+)\n/g,'

$1

'); this.body = this.body.replace(/\\item\[(\w+)\]/g,"
  • $1"); //begin this.body = this.body.replace(/\\item/g,"
  • "); //this.body = this.body.replace(/\\begin\{EX\}(\S+?)\\end\{EX\}/g,''); this.body = this.body.replace(/\\begin\{EX\}/g,'
    Ex
    '); this.body = this.body.replace(/\\end\{EX\}/g,'
    fin Ex

    '); //this.effaceEntre2tag("\\begin{EX}","\\end{EX}"); //item ul ol //this.tagbeginend('\\begin{itemize}','

      ','\\end{itemize}','

    '); //this.tagbeginend('\\begin{tabular}','

      ','\\end{tabular}','

    '); this.body = this.body.replace(/\\begin\{itemize\}/g,''); this.body = this.body.replace(/\\end\{itemize\}/g,'

    '); //tableau tabular //this.body = this.body.replace(/^\[[^\[]+\]/g,''); //this.tagbeginend('\\begin{tabular}','\n\n
    ' , '\\end{enumerate}' , '
    '); //this.body = this.body.replace(/&/g,''); //this.body = this.body.replace(/\\\\\s/g,"\n"); this.body = this.body.replace(/\\caption\{([^\}]+)\}/g,'
    $1
    '); //let regex2 = /\$(\S+?)\$/g; // you need ()-brackets to get $1 //this.body = this.body.replace(regex2, '\\\($1\\\)'); // $1 = first () = highlight let posibody=this.body.indexOf('$');; let ouvaco = true; while(posibody>0) { if(ouvaco) { this.body = this.insert(this.body,"\\(",posibody,1); ouvaco = false; } else { this.body = this.insert(this.body,"\\)",posibody,1); ouvaco = true; } posibody = this.body.indexOf('$',posibody+1); } }//fin nettoyerBody /********************************************************** *gestion des tags begin end *************************************/ tagbeginend(tag,taghtml,tagf,taghtml1) { let lgtag = tag.length; let lgtagf = tagf.length; //gere si 2 retour ligne ou tag end let positagf1=0; let positagf2=0; let posibody=this.body.indexOf(tag); while(posibody>0) { this.body = this.insert(this.body,taghtml,posibody,lgtag); positagf1 = this.body.indexOf(tagf,posibody+lgtag); positagf2 = this.body.indexOf(this.charReLigne+this.charReLigne,posibody+lgtag); if(positagf1==-1||positagf20) { let posifin = this.body.indexOf(tagfin)+tagfin.length ; this.body= this.body.substring(0,posideb)+"
    aaaaa
    "+this.body.slice(posifin); posideb = this.body.indexOf(tagdeb); } } longChaine(texte : string):number { return texte.length; } chargescript(){ var head = document.getElementsByTagName("head")[0], script; script = document.createElement("script"); script.type = "text/x-mathjax-config"; script[(window? "innerHTML" : "text")] = "MathJax.Hub.Config({\n" + " tex2jax: { inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true }\n" + ", TeX: { extensions: ['color.js'], equationNumbers: { autoNumber: 'AMS' } }\n" + "});" head.appendChild(script); script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; //TeX-AMS_CHTML or edit previous line for any other cdn or local mathjax installation head.appendChild(script); } }
  • En poursuivant votre navigation sur mon site, vous acceptez l’utilisation des Cookies et autres traceurs  pour réaliser des statistiques de visites et enregistrer sur votre machine vos activités pédagogiques. En savoir plus.