Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

I greatly appreciate your help.

Thanks.

P.S - I am using mathematica 8

share|improve this question
1  
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
add comment

1 Answer 1

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- *)
UFE @ Export["/home/username/picture.jpg",plot]
(* Out[6]= /home/username/picture.jpg *)

And you can see the generated picture:

sample image generated in interactive session in MathKernel

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- *)
Export["/home/username/picture.jpg",plot]
(* Out[2]= /home/username/picture.jpg *)

The reference says that UsingFrontEnd is necessary for functions like NotebookWrite[], when used from MathKernel. end edit 2

share|improve this answer
    
this looks promising. I'll try. So you say I have to say UsingFrontEnd every time before I invoke some kind of plotting command or export graphics? Is there a way to just turn the switch on at the beginning of the script and turn it off at the end? –  preeti Nov 21 '12 at 19:35
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.