// the message asLaTeX returns a string of LaTeX code that can be converted to pdf
// pdflatex - this needs to be installed first (see LaTex installers online).

(
a = SinOsc.ar([71, LFPulse.kr(2).range(34, 45), 45, 34]).abs;
a = a / 2;
a = a.sqrt;
a.asLaTeX;
)

(
a = (Pulse.ar ** Pulse.ar);
a.asLaTeX;
)

(
a = Control.names([\freq, \amp]).kr([300, 0.1, 550, 0.3])
a.asLaTeX;
);

(SinOsc.ar(Control.names(\freq).kr(440), 0.5pi, Control.names(\amps).kr(0.2.dup)) * 0.125).asLaTeX;

0.5pi.asLaTeX
(SinOsc.ar(203) ** (1/3)).asLaTeX

(
a = 0;
3.do { a = Blip.ar(a * 30.rand + 30.rand) };
a = SinOsc.ar(a) * 0.1;
a.asLaTeX;
)

(
a = (SinOsc.ar(Control.names(\freq).kr([1440, 550]), 0.5pi, Control.names(\amps).kr(0.2.dup)) * 0.135);
x = a.asLaTexNodeDoc.postln;
f = File("~/textex_test".standardizePath, "w");
f.write(x);
f.close;
//unixCmd("rm" + "~/textex_test.*".standardizePath); // remove all older latex files
runInTerminal("pdflatex" + "~/textex_test".standardizePath);
)

unixCmd("open -a safari.app" +  "~/textex_test.pdf".standardizePath);

exp(SinOsc.ar(log(SinOsc.ar), 0.5pi)).asLaTexNodeDoc

(
fork {
	var path;
	path = "~/textex_test.pdf".standardizePath;
	loop {
		5.wait;
		(
a = (SinOsc.ar(Control.names(\freq).kr([1440, 550].rand), 0.5pi, Control.names(\amps).kr(0.2.dup)) * 0.135);
x = a.asLaTexNodeDoc.postln;
f = File(path, "w");
f.write(x);
f.close;
//unixCmd("rm" + "~/textex_test.*".standardizePath); // remove all older latex files
runInTerminal("pdflatex" + path + "; open" + path);
);
}
}
)

// works only with TeXniscope (Preview has an updating bug)
(
fork {
	var path, latexpath, a, x, f;
	path = "textex_test".standardizePath;
	latexpath = "/usr/texbin/pdflatex";
	inf.do {
		
			a = SinOsc.ar({300.rand} ! 5) * 0.135 * pi;
			x = a.asLaTexNodeDoc.postln;
			f = File(path, "w");
			f.write(x);
			f.close;
			//unixCmd(latexpath + path);
			unixCmd(latexpath + path + "; open -a TeXniscope" + path ++ ".pdf");
			3.wait;
}
}
)

// works only with TeXniscope (Preview has an updating bug)
(
Tdef(\x) {
	var path, latexpath, a, x, f;
	path = "textex_test".standardizePath;
	latexpath = "/usr/texbin/pdflatex";
	inf.do { |i|
		
			a = Pulse.ar({300.rand} ! 5) * 0.135 * pi;
			a = a.exp;
			x = a.asLaTexNodeDoc;
			f = File(path, "w");
			f.write(x);
			f.close;
			0.4.wait;
			
			//unixCmd(latexpath + path + "; open -a TeXniscope" + path ++ ".pdf");
			if( i == 0) { unixCmd(latexpath + path + "; open -a TeXniscope" + path ++ ".pdf") } {
				unixCmd(latexpath + path, nil, false);
			};
			3.wait;
}
}.play;
)

// use sc view
(
Tdef(\x) {
	var path, latexpath, a, x, f, w, m, bounds;
	path = "textex_test".standardizePath;
	latexpath = "/usr/texbin/pdflatex";
	bounds = Rect(0, 0, 500, 500 * 2.sqrt);
	defer {
		w = SCWindow("mov", bounds).front;
		m = SCMovieView(w, bounds);
		m.path = path ++ ".pdf";
		m.showControllerAndAdjustSize(false, 1);
	};
	inf.do { |i|
		
			a = Pulse.ar({300.rand} ! 5) * 0.135 * pi;
			x = a.asLaTexNodeDoc;
			f = File(path, "w");
			f.write(x);
			f.close;
			unixCmd(latexpath + path);
			AppClock.sched(1, {
				//m.path = "textex_test2.pdf".standardizePath; // dummy.
				m.path = path ++ ".pdf";
				
				m.resizeWithMagnification(1 + 0.01.rand);
				nil
			});
			3.wait;
}
}.play;
)