banner



Export Notebook As Pdf Mathematica

Pasting externally generated PDF into Mathematica version 8

In Mac OS X, the general system clipboard ("PasteBoard") can shuttle PDF (portable document format) content back and forth between applications. That's very convenient when you're producing and consuming high-quality drawings or mathematical formulas, since PDF can serve as a resolution-independent (vector) format.

There are some common problems with PDF, too — often related to closed-source software that doesn't quite manage to implement the PDF standard or play well with the rest of the operating system (I won't go into problems related to synchronizing X11 and system clipboards here, which for example affects Inkscape):

  • Font incompatibilities can lead to garbled text or equations; this can often be solved by running the source PDF through FontBegone, a utility I wrote long ago which still works fine.
  • Sometimes a PDF clipping that has been copied in cropped form doesn't preserve that cropping information when pasted into other programs; for this reason I wrote clipPDF, a script that manipulates the PasteBoard contents.
  • A recent bug in Mathematica forced me to revisit this idea of manipulating PDF on the PasteBoard, and that's what this page is about.

The problem

In Mathematica version 8, copying a PDF clipping from an external application (like Preview.app) into a Notebook seems to work reasonably well at first sight. However, there is a fatal flaw (and I've reported it to Wolfram):

To see the issue, type Export["text.pdf","This is a test PDF"]; and open the resulting file in Preview.app. Choose the "Select" tool, , and do ⌘-C to copy. Back in Mathematica, start a new cell and type Export["test2.pdf",  ] and paste (⌘-V) the clipboard content into the blank space of this export statement. The resulting new file, test2.pdf, will appear empty in Preview!

So pasted PDF cannot be exported back to a PDF file. This appears to go wrong for all pasted data of this format that I have tried.

A "solution"

As a work-around, I've made a Palette that you can install in Mathematica. Here is a step-by-step walk-through of the installation process (of interest also if you want to install your own palettes):

  • Execute the following Mathematica command:
    CreatePalette[   Column[{Button["Convert PDF on clipboard",       Module[{pdf,         out = "/tmp/MathematicaOutput" <>           StringJoin[Map[ToString, DateList[]]] <> ".pdf",         prog = "from AppKit import *\nboard=NSPasteboard.generalPasteboard()\nresult = board.dataForType_(NSPDFPboardType)\nif result:\n\tresult.writeToFile_atomically_('**FILENAME**',1)\n\" | /usr/bin/python"},        Run["printf \"" <>          StringReplace[prog, {"**FILENAME**" -> out}]];        CopyToClipboard[        If[FileExistsQ[out], pdf = Import[out, "PDF"];          DeleteFile[out];         pdf[[1]], "No PDF"]];       ], Appearance -> "Palette"],      "Click before pasting externally generated PDF"}],   WindowTitle -> "Convert External PDF"];            
  • This will create a new palette:
  • To check that it works, open a Mathematica-generated PDF in Preview or another PDF viewer, copy a part of it (as described above) and click the palette button. The conversion is usually pretty quick, but I don't have any mechanism built in to alert you when it's done. If you now paste anywhere in a Mathematica notebook, the converted PDF should appear. If it doesn't, you may have to wait a moment longer and try pasting again. Then try exporting the pasted PDF as described above, and it should open fine in Preview. There are still pitfalls, see below.
  • If you're satisfied that the button can help you create better PDF graphics for your Mathematica notebooks, you may want to install the palette permanently.
  • To installl the palette, choose the Install item from the Palette menu, as shown here:
  • The Install Mathematica Item dialog asks you for a Source, which you find by looking for Convert External PDF in the pop-up menu, like so:
  • For the Install name, you could choose anything you like — I called it Convert External PDF for obvioius reasons.

The palette is now available as one of the menu items whenever you start Mathematica. Remember, you'll only need this when pasting PDF from sources other than Mathematica itself — copy and paste operations within the FrontEnd pose no problem.

The way this conversion button works is to create a temporary file from the Pasteboard content and use Mathematica's Import function to read it in as PDF. Fortunately, that actually doesn't have the bug that the paste process displays. So if you don't want to use my palette, just save your PDFs and Import them. Clearly, this is not as convenient as the copy-paste method; all I did was to make this detour a one-click step.

Pitfalls

Unfortunately, Import isn't bug-free either when it comes to importing PDF. It seems do do fine re-importing files that were previously exported from Mathematica, but fails with externally produced documents because it can't handle the fonts. Therefore, if you still have problems pasting PDF into your Notebook, you should consider downloading FontBegone. Follow the usage instructions on the linked page. For example, having copied a formula from a document in Preview, click the FontBegone icon first. Then click the Mathematica palette button we just installed. Now you're ready to paste the converted snippet into a Notebook.

Export Notebook As Pdf Mathematica

Source: https://pages.uoregon.edu/noeckel/computernotes/Mathematica/pasteExternalPDF.html

Posted by: perezbaces1957.blogspot.com

0 Response to "Export Notebook As Pdf Mathematica"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel