3
$\begingroup$

My system:

Mathematica 11.2, Windows 10

Problem: I am (perpetually) working on my lecture notes, adding graphics produced by Mathematica. Obviously the notebooks I created for this purpose are in the same folder as the TeX source and such. I am constantly in need of saving new graphics and updating old ones. When I click Save Graphics As it often happens that I am pointed at some folder from an earlier session or whatnot. Occasionally I have seriously misplaced some graphics, now I have learned to look more carefully. But having to change the folder feels very unnatural to me.

I am also switching between projects, and therefore also folders. Actually I have one large folder for handling calculations related to Math.SE! And I need to jump from that folder to some other and back a few times per week.

It is even more annoying, when I am exporting graphics/animation, and need to somehow recall how to type in the absolute path of the folder I want.

Question:

How can I configure Mathematica to behave the way I want it here? That is, make the folder of the current notebook the default folder for all saving/exporting?

I checked out the menus. Nothing promising under File. There is Edit->Preferences, but nothing there looks related. Where are the rest of the user configurable settings?


Update: The posted answer has good and useful suggestions, and helps me in other ways. But it did not solve the problem with the mouse operation Save Graphics As. My uneducated guess is that this is because I want to configure the FrontEnd to also default to using the folder of the notebook.

Update 2: I posted this query at the Wolfram support forum. Other users there suspect that the default folder may be decided by Windows. That suggestion prompted me to ask a related question at SuperUser.SE.

$\endgroup$
1
  • $\begingroup$ Digging a bit deeper in the help files gives the impression that SetDirectory and NotebookDirectorymay be relevant commands. I will try those, but it sure feels like this is something that should be configurable by the user from the settings rather than bothering the kernel with it. $\endgroup$ Commented Jun 25, 2020 at 9:15

3 Answers 3

3
$\begingroup$

This is what I do for all my school HW's. At start of each notebook, I have

 SetDirectory[NotebookDirectory[]];
 ...above makes default directory the same as the one where the notebook is

 [CELL] image is generated in this cell using Mathematica Plot command

 ... in the next cell, type
 [CELL] Export["../my_images_folder/image1234.pdf", NotebookRead[PreviousCell[]]]

The the above export automatically saves the image to folder call my_images_folder which is under the parent folder where the notebook is (I keep folder for images, folder for notebook, and folder for the latex).

This way, when you look at the notebook again, you know which pdf image file goes with which image as well. It is always the image in the cell above the Export command.

This is the structure of each HW folder I have

                        HW1/
                         |
                 +-------+-----------+-----------------------------+                
            images/   notebooks/   latex/                      Makefile
       image1.pdf      file.nb     HW.tex           
       image2.pdf                 /includegraphics[..image1]

When done, I just do make and it automatically gives me a HW1.pdf.

To make sure the default directory is always the same as the notebook, when evaluating anything in the notebook, you can make the cell an initialization cell

Using initialization cells, you can specify that particular input cells of a notebook should be evaluated first.

What this means, if you open the notebook and try to generate the plot again, then Mathematica will evaluate that initialization cell automatically and sets the default directly to the correct place. This way you do not end up saving the plots to the wrong images folder. (First time, it will ask you if you want to evaluate the initialization cells first, you can tell it to do that automatically each time so you do not have to answer this question each time).

The above Export command is thanks to Carl Woll. See how-to-export-the-cell-above-to-pdf-using-the-export-command-not-using-gui

$\endgroup$
7
  • $\begingroup$ @JyrkiLahtonen You do not have to write the command each time. The command is there in the top most cell in your notebook. You can either just evaluate that one cell, or make that cell an initialization cell, which automatically evaluates. I have it as normal cell, and I just evaluate it when I visit that notebook again. $\endgroup$
    – Nasser
    Commented Jun 25, 2020 at 10:04
  • $\begingroup$ @JyrkiLahtonen add about initialization cell. You can always convert any cell to initialization cell, by right-clicking on the right edge of the cell and choose initialization cell. $\endgroup$
    – Nasser
    Commented Jun 25, 2020 at 10:16
  • $\begingroup$ Thanks. One of my pet project notebooks begins with a dozen or so definitions/variable declarations. My use of Mathematica just became significantly simpler :-) How have I managed to miss that all these years? $\endgroup$ Commented Jun 25, 2020 at 10:19
  • $\begingroup$ I'm afraid this did not solve the problem with Save Graphics As. I may be wrong for I don't know which software component does what, but I guess the problem is that your suggestions affect the Kernel, but I also want the FrontEnd to co-operate :-( $\endgroup$ Commented Jun 27, 2020 at 12:32
  • $\begingroup$ @JyrkiLahtonen what you observed, is something I saw also long time ago. SetDirectory[NotebookDirectory[]]; only affects the location related to Export. SaveAs still remembers the last notebook folder. Same as Open->Open it does not start at where SetDirectory[NotebookDirectory[]]; is set either, but starts at earlier notebook location. There is no option to change that I know about. That is why I use Export only to save images, which is what the example I put shows. $\endgroup$
    – Nasser
    Commented Jun 27, 2020 at 12:40
2
$\begingroup$

I asked this same question at the Wolfram Community.

Ian Hojnicki of Wolfram gave the following piece of advice. Give the command

CurrentValue[$FrontEndSession, "NotebookBrowseDirectory"] = 
 NotebookDirectory[EvaluationNotebook[]]

I tested it today, and it works as prescribed. I guess I will be copy/pasting that command to the relevant notebooks as an initialization cell as explained in Nasser's answer.

$\endgroup$
1
$\begingroup$

I'm not sure that this is an answer, but it was too long for a comment.

I was trying to recreate your Save Graphics As issue in v12.0 on Win10, and this is the behaviour that I've found: Save As sets the location to which Save Graphics As points to. The location will be the directory where the last Save As occurred.

These are the tests that I've ran:

  • if a new notebook is started (either in a current or a fresh session) and saved to some location /loc/ with either Save or Save As, then Save Graphics As will point to /loc/. The Save Graphics As will now point to /loc/ for all other notebooks, either in the current or a fresh session.

  • if an existing notebook is opened, Save Graphics As will point to either to the last location where a fresh notebook was saved with either Save or Save As, or to the last location where an existing notebook was saved with Save As, but not with Save

  • if an existing notebook is copy-pasted to a new location, opened in Mathematica and then saved with Save As, then Save Graphics As will point to the new location; however, if it is saved with just Save, the Save Graphics As will still point to the old location.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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