Sorry if this was asked before. I googled and could not find anything.

Many times I need to export a cell to a PDF. It is always the cell above. Currently I do this using the GUI, by selecting the cell first, then File->Save selection As then save it to a PDF file.

But sometimes I lose track of what file I saved the cell to (If I do not remember to add a comment).

Is it possible to use the Export command? This way by looking at the command below the cell, I know the name of the file.

With plots and graphics, this is easy to do, since I could do p=Plot[...] then do Export["plot.pdf",p]

Any suggestions how to do this for cell? It will always be the cell above where the command is issued, if this makes it any easier. (So can't use any hardcoded specific current cell number, since the cell numbers can change when modifying the notebook)

Mathematica graphics

I am using V12 on windows 10.

  • $\begingroup$ Does Export["code.pdf", NotebookRead[PreviousCell[]]] do what you want? $\endgroup$ – Carl Woll Sep 29 '19 at 2:53
  • $\begingroup$ @CarlWoll Yes !, thank you, that is very good command. If you post that as answer, will be happy to accept it. $\endgroup$ – Nasser Sep 29 '19 at 2:58

You can use PreviousCell for this purpose. For example:

Export["code.pdf", NotebookRead[PreviousCell[]]]

should grab the cell contents of the previous cell, and save it to your file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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