Re: Q:Can I turn on a Notebook from the kernel?
- To: mathgroup at smc.vnet.net
- Subject: [mg17389] Re: Q:Can I turn on a Notebook from the kernel?
- From: "P.J. Hinton" <paulh>
- Date: Thu, 6 May 1999 02:44:09 -0400
- Organization: "Wolfram Research, Inc."
- Sender: owner-wri-mathgroup at wolfram.com
On Mon, 3 May 1999 in article <7gdu4d$sb2 at smc.vnet.net>, xingjing at calvin.math.vt.edu (Xing Jing Li) writes: > I apologize my awkward questions since I am just starting work on the > Mathematica and MathLink a couple of weeks. I hope the experts in this > group will help me clear my view on the subject so I can save some > time searching fruitlessly on my own. > > I am working on a project that using Mathematica to do some symbolic > computation. My goal is to put the symbolic expressions in the > notebook to ".gif " or ".pdf" files from a Mathematica which is > running from a remote server. I can let math kernel to evaluate the > expression I feed in from a C program and what I get back is a LONG > expression with all the headers and brackets. (I sort of changed the > "factor.c " in the <mathlink Examples> to start with.) What I want > are the expressions as in the notebook. Is there any way I can turn > on a notebook from the kernel so that I can feed in the stuff I need > to evaluate from a C program them save the output in ".gif" files > or do I have to write some front end to do the work. I have tried the > examples come with the DeveloersKits and all the examples in the > Mathlink Tutorial by Todd Gayley. I also searched The Mathematica > Book for an answer. So far I had no idea how do you do it if you can > do it or can it be done at all. I tried to use Notebook commands in > the Kernel mode and it tells me these are frontend objects and they > are not available. Does it mean I can not access the notebook from > the kernel? It is possible to do something like this, but you are drifting into horribly uncharted waters. The kernel can request graphical represntations of typeset expressions from the front end by way of MathLink packets. The packet I will use in this posting is the ConvertToPostScriptPacket[], which is documented briefly in _The Beginner's Guide to Mathematica Version 3_ by Jerry Glynn and Theodore Gray. (* We define a function that takes three mandatory arguments and two optional arguments. *) createEPS[lnk_LinkObject, epsFile_String, expr_, fmttype:StandardForm style_:"DisplayFormula"] := Module[{cellexpr, epsString, xmin, ymin, xmax, ymax, stmp, bsline}, cellexpr = Cell[BoxData[ToBoxes[expr, TraditionalForm]], "DisplayFormula"]; {epsString, {{xmin, ymin}, {xmax, ymax}}, bsline} = (LinkWrite[lnk, ConvertToPostScriptPacket[cellexpr]]; LinkRead[lnk]); stmp = OpenWrite[epsFile]; WriteString[stmp, "%!PS-Adobe-2.0 EPSF-2.0\n", "%%BoundingBox: ", ToString[xmin], " ", ToString[ymin], " ", ToString[xmax], " ", ToString[ymax], "\n", epsString]; Close[stmp]] (* The first argument has to be a LinkObject[] that refers to a front end. This can be obtained by launching the front end in MathLink mode. The second argument is a string that specifies the name of the file where the result will be written. The third argument is the expression that is to be rendered by the front end. The first optional argument is FormatType, which can be StandardForm or TraditionalForm. The next optional argument is the cell style that should be used to specify the appearance of the text. This function helps the kernel determine where your front end is located if it is to be run locally. If the front end is to run remotely, you may need to set up some sort of rsh command that launches the front end to listen on a given port number. *) feProgram=Switch[$OperatingSystem, "Windows95"|"Windows98"|"WindowsNT", ToFileName[{$TopDirectory},"Mathematica.exe"], "Unix",ToFileName[{$TopDirectory,"Executables",$SystemID},"Mathematica"], _,$Failed] (* Here we create the link to the front end. *) lnk = LinkLaunch[feProgram <> " -mathlink"] (* Here is a test expression for export. *) expr = Expand[(x+y)^30]; (* Now we can call our front end process to get the EPS graphic and write it to a file. *) createEPS[lnk, "testfile.eps", expr, TraditionalForm, "DisplayFormula"] This will create an EPS file which can be fed to Ghostscript and other filter programs to turn it into a bitmap with the desired properties. A few tips on converting EPS into a bitmap can be found here. http://xxx.lanl.gov/help/bitmap#advanced It is essential that you configure Ghostscript to use the Math fonts. A howto can be found here: http://www.wolfram.com/support/Graphics/Formats/EPS/Ghostscript.html If you are running under X Window System, Version 11, Release 6, you can set up a virtual frame buffer X server (Xvfb) to act as the display target for a front end so that you never have to see the front end start. -- P.J. Hinton Mathematica Programming Group paulh at wolfram.com Wolfram Research, Inc. http://www.wolfram.com/~paulh/