# How to export the cell above to pdf using the Export[] command (not using GUI)

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)

I am using V12 on windows 10.

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

You can use PreviousCell for this purpose. For example:
Export["code.pdf", NotebookRead[PreviousCell[]]]