Ploting Fuctions for use with TeX

©2004-2013 by Gerald Teschl  <Gerald.Teschl@univie.ac.at>, <http://www.mat.univie.ac.at/~gerald/>

TEXPlot takes a function and outputs a latex picture object which can be typeset using:

\documentclass{article}
\usepackage{curves}
\unitlength1cm

\begin{document}
	<Insert picture>
\end{document}

TeXPlot Code

Needs["NumericalCalculus`"]

Off[General::spell,General::spell1]

MyChop[x_]:=Round[10^3*x]/10.^3;
SetAttributes[MyChop,Listable];

GetValue[meth_,rest__]:=If[meth==="Bezier",GetValueB[rest],GetValueC[rest]];GetValueB[f_,x_,x0_]:=Block[{},
fp=N[D[f,x] /. x->x0];
If[!NumberQ[fp],fp=ND[f,x,x0]];
{N[f/. x-> x0],fp}
];
GetValueC[f_,x_,x0_]:=Block[{},
{N[f/. x-> x0],0}
];

PlotOne[meth_,rest__]:=If[meth==="Bezier",PlotOneB[rest],PlotOneC[rest]];PlotOneB[t_,xscale_,yshift_,yscale_,len_,nmax_]:=Block[{x1,x2,x3,f1,f2,f3,fp1,fp3,out=""},
Table[
{x1,x3}=(len/nmax){n-1,n};{f1,f3}=({t[[n]][[1]],t[[n+1]][[1]]}-yshift)yscale;
{fp1,fp3}={t[[n]][[2]],t[[n+1]][[2]]} (yscale/xscale);
x2=If[fp1≠fp3,((-f1+f3+fp1 x1-fp3 x3)/(fp1-fp3)),((x1+x3)/2)];
f2=f1+fp1(x2-x1);
out=StringJoin[out,StringReplace[ToString[MyChop[{{x1,f1},{x2,f2},{x3,f3}}]],{" "->"","}, {"->"),(","{{"->"\\qbezier(","}}"->")\n"}]];,{n,nmax}];
out
]
PlotOneC[t_,xscale_,yshift_,yscale_,len_,nmax_]:=Block[{x1,f1,out="\\curve("},
Table[
x1=(len/nmax)n;
f1=(t[[n+1]][[1]]-yshift)yscale;
out=StringJoin[out,StringReplace[ToString[MyChop[{x1,f1}]],{"{"->"",", "->",","}"->", "}]];,{n,0,nmax}];
StringJoin[StringDrop[out,-2],")"]
]

TeXPlot[f_,{x_,xmin_,xmax_},opts___]:=TeXPlot[{f},{x,xmin,xmax},opts];TeXPlot[f_List,{x_,xmin_,xmax_},opts___]:=Block[{nmax,ymax,ymin,t={},d,tt,axes,len,xpad,ypad,arat,meth,xticks,yticks,xscale,yshift,yscale,out},
{nmax,axes,len,arat,meth,xticks,ypad}={PlotPoints,Axes, Length,AspectRatio,Method,Ticks,Padding}/. {opts,PlotPoints->10,Axes->True,Length->10,AspectRatio->1/GoldenRatio,Method->"Curves",Ticks->{},Padding->1.05};
meth=ToString[meth];
If[Length[ypad]>0,xpad=ypad[[1]];ypad=ypad[[2]],xpad=1.08];
If[Length[xticks]>0 && Length[xticks[[1]]]>0,yticks=xticks[[2]];xticks=xticks[[1]]];
d=((xmax-xmin)/nmax);
Do[
t=Append[t,
Table[GetValue[meth,f[[i]],x, xmin+n d],{n,0,nmax}]],
{i,Length[f]}];
tt=Append[Transpose[Flatten[t,1]][[1]],0];
{ymin,ymax}={Min[tt],Max[tt]};
If[ymax==ymin,ymax=ymax+0.01;ymin=ymin-0.01];
yshift=ymin-((ypad-1)/2)(ymax-ymin);yscale=((len*arat)/(ymax-ymin));
xscale=N[(len/(xmax-xmin))];
out=StringJoin["\\begin{picture}(",ToString[MyChop[xpad*len]],",",ToString[MyChop[ypad*len*arat]],")\n\n"];
If[axes,
If[xmin*xmax<=0,out=StringJoin[out,"\\put(0,",ToString[MyChop[-yshift*yscale]],"){\\vector(1,0){",ToString[MyChop[len*xpad]],"}}\n"];
Do[
out=StringJoin[out,"\\put(",ToString[MyChop[xticks[[i]]*xscale]],",",ToString[MyChop[-yshift*yscale-0.1]],"){\\line(0,1){0.2}}\n"];
out=StringJoin[out,"\\put(",ToString[MyChop[xticks[[i]]*xscale-0.1]],",",ToString[MyChop[-yshift*yscale-0.4]],"){$",ToString[TeXForm[xticks[[i]]]],"$}\n"],{i,Length[xticks]}];
];
If[ymin*ymax<=0,out=StringJoin[out,"\\put(",ToString[MyChop[-xmin*xscale]],",0){\\vector(0,1){",ToString[MyChop[len*ypad*arat]],"}}\n"];
Do[
out=StringJoin[out,"\\put(",ToString[MyChop[-xmin*xscale-0.1]],",",ToString[MyChop[(yticks[[i]]-yshift)*yscale]],"){\\line(1,0){0.2}}\n"];
out=StringJoin[out,"\\put(",ToString[MyChop[-xmin*xscale+0.1]],",",ToString[MyChop[(yticks[[i]]-yshift)*yscale-0.1]],"){$",ToString[TeXForm[yticks[[i]]]],"$}\n"],{i,Length[yticks]}];];
];
Do[
out=StringJoin[out,"\n",PlotOne[meth,t[[i]],xscale,yshift,yscale,len,nmax]]
,{i,Length[f]}];
Print["Scaling factors: x=",xscale," y=",yscale];
StringJoin[out,"\n\\end{picture}"]
]

TeXListPlot[l_List]:=Block[{out="\\curve("},
Do[
out=StringJoin[out,StringReplace[ToString[MyChop[l[[n]]]],{"{"->"",", "->",","}"->", "}]];,{n,1,Length[l]}];
StringJoin[StringDrop[out,-2],")"]
];

On[General::spell1,General::spell]

Example

TeXPlot[Sin[x],{x,0,2π},Length->3,Ticks->{{Pi,2Pi},{-1,0,1}},Padding->1.5]

TeXPlot[{Sin[x],Cos[x]},{x,0,2π},Length->3,Axes->False,PlotPoints->7,Method->Bezier]