|
测试一下php脚本,从aops texer把tex转换成png再重定向.
用法:首先将需要的公式转成base64,比如- window.btoa("$x^2+y^2$")
- 输出:JHheMit5XjIk
复制代码 所以$x^2+y^2$的base64是JHheMit5XjIk.然后把图片地址填上- /aops.php?tex=JHheMit5XjIk
复制代码 即可.
重定向以后得到的图片是(每次生成新的链接)
我们可以封装一个简单的js函数
- function aops(str){window.open("/aops.php?tex="+window.btoa(str.replaceAll('\n','')))}
复制代码
示例1:将一段公式经过Base64加密得到的,可以在浏览器调试工具中运行,注意,反斜杠需要转义(当然也可以从html元素中读取,就不用手动转义了):
- aops('$$\\sqrt x=\\frac{\\sqrt y}{\\sqrt z}$$')
复制代码
重定向以后得到的图片是
示例2:将一段Asymptote代码去掉换行符以后经过Base64加密得到的,可以在浏览器调试工具中运行:
- aops(`[asy]import graph;
- import palette;
- import contour;
- size(12cm,IgnoreAspect);
- pair a=(pi/2,0);
- pair b=(3pi/2,2pi);
- real f(real x, real y) {return cos(x)*sin(y);}
- int N=100;
- int Divs=10;
- defaultpen(1bp);
- bounds range=bounds(-1,1);
- real[] Cvals=uniform(range.min,range.max,Divs);
- guide[][] g=contour(f,a,b,Cvals,N,operator --);
- pen[] Palette=quantize(Rainbow(),Divs);
- pen[][] interior=interior(g,extend(Palette,grey,black));
- fill(g,interior);
- draw(g);
- palette("$f(x,y)$",range,point(SE)+(0.5,0),point(NE)+(1,0),Right,Palette,
- PaletteTicks("$%+#0.1f$",N=Divs));[/asy]`)
复制代码
其中的asy标签是aops论坛自定义的.重定向以后得到的图片是
更多的Asymptote示例(均来自官网)
- aops(`[asy]import graph;
- size(140mm,70mm,IgnoreAspect);
- scale(false);
- real[] x={1,3,4,5,6};
- real[] y={1,5,2,0,4};
- marker mark=marker(scale(1mm)*cross(6,false,r=0.35),red,Fill);
- draw(graph(x,y,Hermite),"Hermite Spline",mark);
- xaxis("$x$",Bottom,LeftTicks(x));
- yaxis("$y$",Left,LeftTicks);
- attach(legend(),point(NW),40S+30E,UnFill);
- [/asy]`)
复制代码
- aops(`[asy]import graph;
- size(200,IgnoreAspect);
- real f(real x) {return 1/x;};
- bool3 branch(real x)
- {
- static int lastsign=0;
- if(x == 0) return false;
- int sign=sgn(x);
- bool b=lastsign == 0 || sign == lastsign;
- lastsign=sign;
- return b ? true : default;
- }
- draw(graph(f,-1,1,branch));
- axes("$x$","$y$",red);
- [/asy]`)
复制代码

- aops(`[asy]import graph;
- real Freq=60.0;
- real margin=5mm;
- pair exp(pair x) {
- return exp(x.x)*(cos(x.y)+I*sin(x.y));
- }
- real Merr(real x, real w) {
- real tau=x/(2*Freq);
- return 20*log(abs((tau*w+tau/(exp(I*2*pi*Freq*tau)-1))*(I*2*pi*Freq)));
- }
- real Aerr(real x, real w) {
- real tau=x/(2*Freq);
- return degrees((tau*w+tau/(exp(I*2*pi*Freq*tau)-1))*(I*2*pi*Freq));
- }
- picture pic1;
- scale(pic1,Log,Linear);
- real Merr1(real x){return Merr(x,1);}
- draw(pic1,graph(pic1,Merr1,1e-4,1),black+1.2);
- ylimits(pic1,-60,20);
- yaxis(pic1,"magnitude (dB)",LeftRight,RightTicks(new
- real[] {-60,-40,-20,0,20}));
- xaxis(pic1,"$f/f_\\mathrm{Ny}$",BottomTop,LeftTicks(N=5));
- yequals(pic1,0,Dotted);
- yequals(pic1,-20,Dotted);
- yequals(pic1,-40,Dotted);
- xequals(pic1,1e-3,Dotted);
- xequals(pic1,1e-2,Dotted);
- xequals(pic1,1e-1,Dotted);
- size(pic1,100,100,point(pic1,SW),point(pic1,NE));
- label(pic1,"$\\theta=1$",point(pic1,N),2N);
- frame f1=pic1.fit();
- add(f1);
- picture pic1p;
- scale(pic1p,Log,Linear);
- real Aerr1(real x){return Aerr(x,1);}
- draw(pic1p,graph(pic1p,Aerr1,1e-4,1),black+1.2);
- ylimits(pic1p,-5,95);
- yaxis(pic1p,"phase (deg)",LeftRight,RightTicks(new real[] {0,45,90}));
- xaxis(pic1p,"$f/f_\\mathrm{Ny}$",BottomTop,LeftTicks(N=5));
- yequals(pic1p,0,Dotted);
- yequals(pic1p,45,Dotted);
- yequals(pic1p,90,Dotted);
- xequals(pic1p,1e-3,Dotted);
- xequals(pic1p,1e-2,Dotted);
- xequals(pic1p,1e-1,Dotted);
- size(pic1p,100,100,point(pic1p,SW),point(pic1p,NE));
- frame f1p=pic1p.fit();
- f1p=shift(0,min(f1).y-max(f1p).y-margin)*f1p;
- add(f1p);
- picture pic2;
- scale(pic2,Log,Linear);
- real Merr2(real x){return Merr(x,0.75);}
- draw(pic2,graph(pic2,Merr2,1e-4,1),black+1.2);
- ylimits(pic2,-60,20);
- yaxis(pic2,"magnitude (dB)",LeftRight,RightTicks(new
- real[] {-60,-40,-20,0,20}));
- xaxis(pic2,"$f/f_\\mathrm{Ny}$",BottomTop,LeftTicks(N=5));
- yequals(pic2,0,Dotted);
- yequals(pic2,-20,Dotted);
- yequals(pic2,-40,Dotted);
- xequals(pic2,1e-3,Dotted);
- xequals(pic2,1e-2,Dotted);
- xequals(pic2,1e-1,Dotted);
- size(pic2,100,100,point(pic2,SW),point(pic2,NE));
- label(pic2,"$\\theta=0.75$",point(pic2,N),2N);
- frame f2=pic2.fit();
- f2=shift(max(f1).x-min(f2).x+margin)*f2;
- add(f2);
- picture pic2p;
- scale(pic2p,Log,Linear);
- real Aerr2(real x){return Aerr(x,0.75);}
- draw(pic2p,graph(pic2p,Aerr2,1e-4,1),black+1.2);
- ylimits(pic2p,-5,95);
- yaxis(pic2p,"phase (deg)",LeftRight,RightTicks(new real[] {0,45.1,90}));
- xaxis(pic2p,"$f/f_\\mathrm{Ny}$",BottomTop,LeftTicks(N=5));
- yequals(pic2p,0,Dotted);
- yequals(pic2p,45,Dotted);
- yequals(pic2p,90,Dotted);
- xequals(pic2p,1e-3,Dotted);
- xequals(pic2p,1e-2,Dotted);
- xequals(pic2p,1e-1,Dotted);
- size(pic2p,100,100,point(pic2p,SW),point(pic2p,NE));
- frame f2p=pic2p.fit();
- f2p=shift(max(f1p).x-min(f2p).x+margin,min(f2).y-max(f2p).y-margin)*f2p;
- add(f2p);
- [/asy]`)
复制代码

- aops(`[asy]import graph;
- import palette;
- import contour;
- size(10cm,10cm,IgnoreAspect);
- pair a=(0,0);
- pair b=(2pi,2pi);
- real f(real x, real y) {return cos(x)*sin(y);}
- int N=200;
- int Divs=10;
- int divs=2;
- defaultpen(1bp);
- pen Tickpen=black;
- pen tickpen=gray+0.5*linewidth(currentpen);
- pen[] Palette=BWRainbow();
- bounds range=image(f,Automatic,a,b,N,Palette);
- // Major contours
- real[] Cvals=uniform(range.min,range.max,Divs);
- draw(contour(f,a,b,Cvals,N,operator --),Tickpen);
- // Minor contours
- real[] cvals;
- for(int i=0; i < Cvals.length-1; ++i)
- cvals.append(uniform(Cvals[i],Cvals[i+1],divs)[1:divs]);
- draw(contour(f,a,b,cvals,N,operator --),tickpen);
- xaxis("$x$",BottomTop,LeftTicks,above=true);
- yaxis("$y$",LeftRight,RightTicks,above=true);
- palette("$f(x,y)$",range,point(NW)+(0,0.5),point(NE)+(0,1),Top,Palette,
- PaletteTicks(N=Divs,n=divs,Tickpen,tickpen));
- [/asy]`)
复制代码

- aops(`[asy]import graph;
- import stats;
- size(400,200,IgnoreAspect);
- int n=10000;
- real[] a=new real[n];
- for(int i=0; i < n; ++i) a[i]=Gaussrand();
- draw(graph(Gaussian,min(a),max(a)),blue);
- // Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto.
- int N=bins(a);
- histogram(a,min(a),max(a),N,normalize=true,low=0,lightred,black,bars=false);
- xaxis("$x$",BottomTop,LeftTicks);
- yaxis("$dP/dx$",LeftRight,RightTicks(trailingzero));[/asy]`)
复制代码
- aops(`[asy]import graph;
- size(0,100);
- path g=ellipse((0,0),1,2);
- scale(true);
- axis(Label("C",align=10W),g,LeftTicks(endlabel=false,8,end=false),
- ticklocate(0,360,new real(real v) {
- path h=(0,0)--max(abs(max(g)),abs(min(g)))*dir(v);
- return intersect(g,h)[0];}));[/asy]`)
复制代码

- aops(`[asy]import graph;
- import palette;
- int n=256;
- pen[] Palette=BWRainbow();
- real w(real w0, real z0, real z) {return w0*sqrt(1+(z/z0)^2);}
- real pot(real lambda, real w0, real r, real z)
- {
- real z0=pi*w0^2/lambda, kappa=2pi/lambda;
- return exp(-2*(r/w(w0,z0,z))^2)*cos(kappa*z)^2;
- }
- picture make_field(real lambda, real w0)
- {
- real[][] v=new real[n][n];
- for(int i=0; i < n; ++i)
- for(int j=0; j < n; ++j)
- v[i][j]=pot(lambda,w0,i-n/2,abs(j-n/2));
- picture p=new picture;
- size(p,250,250,IgnoreAspect);
- real xm=-n/lambda, ym=-n/(2*w0), xx=n/lambda, yx=n/(2*w0);
- image(p,v,(xm,ym),(xx,yx),Palette);
- xlimits(p,xm,xx);
- ylimits(p,ym,yx);
- xaxis(p,"{\\Large $z/\\frac{\\lambda}{2}$}",BottomTop,LeftTicks);
- yaxis(p,"{\\Large $r/w_0$}",LeftRight,RightTicks);
- label(p,format("{\\LARGE $w_0/\\lambda=%.2f$}",w0/lambda),point(p,NW),5N);
- return p;
- }
- picture p=make_field(160,80);
- picture q=make_field(80,80);
- picture r=make_field(16,80);
- picture s=make_field(2,80);
- real margin=1cm;
- add(p.fit(),(0,0),margin*NW);
- add(q.fit(),(0,0),margin*NE);
- add(r.fit(),(0,0),margin*SW);
- add(s.fit(),(0,0),margin*SE);[/asy]`)
复制代码

- aops(`[asy]import graph;
- import palette;
- size(10cm,10cm,IgnoreAspect);
- real f(real x, real y) {
- return 0.9*pow10(2*sin(x/5+2*y^0.25)) + 0.1*(1+cos(10*log(y)));
- }
- scale(Linear,Log,Log);
- pen[] Palette=BWRainbow();
- bounds range=image(f,Automatic,(0,1),(100,100),nx=200,Palette);
- xaxis("$x$",BottomTop,LeftTicks,above=true);
- yaxis("$y$",LeftRight,RightTicks,above=true);
- palette("$f(x,y)$",range,(0,200),(100,250),Top,Palette,
- PaletteTicks(ptick=linewidth(0.5*linewidth())));
- [/asy]`)
复制代码

- aops(`[asy]import graph;
- size(9cm,8cm,IgnoreAspect);
- string data="westnile.csv";
- file in=input(data).line().csv();
- string[] columnlabel=in;
- real[][] A=in;
- A=transpose(A);
- real[] number=A[0], survival=A[1];
- path g=graph(number,survival);
- draw(g);
- scale(true);
- xaxis("Initial no.\\ of mosquitoes per bird ($S_{M_0}/N_{B_0}$)",
- Bottom,LeftTicks);
- xaxis(Top);
- yaxis("Susceptible bird survival",Left,RightTicks(trailingzero));
- yaxis(Right);
- real a=number[0];
- real b=number[number.length-1];
- real S1=0.475;
- path h1=(a,S1)--(b,S1);
- real M1=interp(a,b,intersect(h1,g)[0]);
- real S2=0.9;
- path h2=(a,S2)--(b,S2);
- real M2=interp(a,b,intersect(h2,g)[0]);
- labelx("$M_1$",M1);
- labelx("$M_2$",M2);
- draw((a,S2)--(M2,S2)--(M2,0),Dotted);
- draw((a,S1)--(M1,S1)--(M1,0),dashed);
- pen p=fontsize(10pt);
- real y3=0.043;
- path reduction=(M1,y3)--(M2,y3);
- draw(reduction,Arrow,TrueMargin(0,0.5*(linewidth(Dotted)+linewidth())));
- arrow(shift(-20,5)*Label(minipage("\\flushleft{\\begin{itemize}\\item[1.]
- Estimate proportion of birds surviving at end of season\\end{itemize}}",100),
- align=NNE),
- (M1,S1),NNE,1cm,p,Arrow(NoFill));
- arrow(shift(-24,5)*Label(minipage("\\flushleft{\\begin{itemize}\\item[2.]
- Read off initial mosquito abundance\\end{itemize}}",80),align=NNE),
- (M1,0),NE,2cm,p,Arrow(NoFill));
- arrow(shift(20,0)*Label(minipage("\\flushleft{\\begin{itemize}\\item[3.]
- Determine desired bird survival for next season\\end{itemize}}",90),align=SW),
- (M2,S2),SW,arrowlength,p,Arrow(NoFill));
- arrow(shift(8,-15)*Label(minipage("\\flushleft{\\begin{itemize}\\item[4.]
- Calculate required proportional reduction in mosquitoes\\end{itemize}}",90),
- align=NW),
- point(reduction,0.5),NW,1.5cm,p,Arrow(NoFill));[/asy]`)
复制代码

- aops(`[asy]import graph;
- size(100);
- pair a=(0,0);
- pair b=(2pi,2pi);
- path vector(pair z) {return (0,0)--(sin(z.x),cos(z.y));}
- add(vectorfield(vector,a,b));[/asy]`)
复制代码
|
|