# generating pdf using UseFrontEnd

I run mathematica on a remote server and run it in batch mode using /math -noprompt -script 'scriptname.m' &

Now, I use ssh -X to connect to the server. The server is linux based and my computer is a mac. I use x11 forwarding.

I would like to generate PDFs in the above mentioned 'script.m'. In my code, how should I incorporate UseFrontEnd? should I invoke it first using ConnectToFrontEnd[] ? Should I use UseFrontEnd[Export["blah.pdf",fig]] or is it a one time thing? Also, when using the UseFrontEnd command, is it required that I do ssh -X/Y or is it sufficient if I do ssh?

Thanks.

P.S - I am using mathematica 8

-
this might help: superuser.com/questions/381807/… –  au700 Nov 21 '12 at 18:42
not exactly. How do I use UseFrontEnd together with Export? and should I do that EVERYTIME I export a file to pdf? –  preeti Nov 21 '12 at 18:49
stackoverflow.com/questions/315464/… this seems to help. But still, I don't know if I need to code Needs[Jlink] and then UseFrontEnd[Export[]] everytime I export. –  preeti Nov 21 '12 at 19:00

It is sufficient if you just do ssh - I was able to use the following commands without X11 forwarding.

After you ssh to the machine with Mathematica and invoke MathKernel you can do in the interactive session:

UFE = UsingFrontEnd
(* Out[4]= UsingFrontEnd *)
plot = UFE @ Plot[x^2,{x,-2,2}]
(* Out[5]= -Graphics- *)


And you can see the generated picture:

The UFE = UsingFrontEnd line is to make the input lines shorter. Of course you could write plot = UsingFrontEnd @ Plot[x^2,{x,-2,2}].

After inspecting the output of monitorlm (Mathematica network license manager) I think that the first invocation of UsingFrontEnd starts a SubMathematica process and the same process is used in subsequent UsingFrontEnd commands. The SubMathematica process is terminated when the interactive MathKernel session is closed.

I'm sure things are very similar when using a .m script.

edit I am using Mathematica 7.0.1.0 installed on Ubuntu 11.10 end edit

edit 2 After OP's comment I chcecked that to Plot[] and Export[] you don't need UsingFrontEnd. So the following works fine:

plot = Plot[x^2,{x,-2,2}]
(* Out[1]= -Graphics- *)

The reference says that UsingFrontEnd is necessary for functions like NotebookWrite[], when used from MathKernel. end edit 2
welll it seems you don't need the UsingFrontEnd command to plot = Plot[x^2,{x,-2,2}] and Export["/home/username/output.jpg",plot]. The reference says that UsingFrontEnd is neccesary for NotebookWrite[] and similar functions (I have used it to NotebookPrint[]` some time ago). –  au700 Nov 21 '12 at 19:44