I often want to have the same code at the beginning of every new notebook. Is it possible to configure Mathematica, such that whenever you create a new notebook some user-defined code will always be created with the new document.

E.g. commonly used plot configurations, packages, directory setting etc.


fm[name_, size_: 7] := 
 Graphics[{EdgeForm[], PolygonMarker[name, Offset[size]]}]
PlotStyles = {Frame -> True, FrameStyle -> Directive[Black, Thin], 
   Axes -> False, ImageSize -> 350, AspectRatio -> 1.0};

At the begining of every new notebook.

  • 1
    $\begingroup$ You could put that code in a package and load it with a single command. $\endgroup$ – Szabolcs Oct 31 '16 at 13:14
  • $\begingroup$ @Szabolcs That's not a bad idea, but sometimes I will need to tweak settings now and then, so I'd like to be able to see it in full. $\endgroup$ – Q.P. Oct 31 '16 at 13:15
  • $\begingroup$ Make a palette with a button that can insert the code? CreatePalette@PasteButton["Paste", Defer[3*4]]. (Just to make it clear: I don't know how to do what you are asking for, or if it is even possible. That's why I keep suggesting other solutions.) $\endgroup$ – Szabolcs Oct 31 '16 at 13:30
  • $\begingroup$ @Szabolcs does this persist after mathematica is closed and reopened though? $\endgroup$ – Q.P. Oct 31 '16 at 13:34
  • $\begingroup$ Save the palette. Look up the options of CreatePalette and give it a nice name. File -> Install..., Type: Palette, Source: choose the palette notebook. It'll save to $UserBaseDirectory/SystemFiles/FrontEnd/Palettes. Access it through the Palettes menu. $\endgroup$ – Szabolcs Oct 31 '16 at 14:03

I recommend creating a palette with a button that can insert the code for you. Then save the palette and make it easy to access through the palettes menu.

Create palette

Suppose your code is (for sake of simplicity),

code1 = HoldComplete[1+1];

The create the palette:

   PasteButton["Template1", Defer @@ code1]
 WindowTitle -> "Templates"

I put in a column in case you want multiple buttons that insert different pieces of code.

enter image description here

Install palette for permanent use

Now go to File → Install..., and in the dialog that comes up select Palettes, then the palette you just created.

enter image description here

For Install Name, type a filename, e.g. Templates.nb, then press OK. Now close the palette.

From now on the palette will be permanently present in the Palettes menu. If you want to remove it, the file is located at

SystemOpen@FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "Palettes"}]

Simply remove it.

It is my personal opinion that inserting code in every single notebook you open will eventully be both annoying and counterproductive. I recommend the palette solution instead, which just takes a single mouseclick, so it's simple and quick. It lets you keep several code snippets and insert whichever you want.

If you're feeling up to it, you can even create a whole snippet system where the snippets are stored in a file (perhaps notebook) and can be selected and inserted with a simple GUI (e.g. dropdown boxes). Many text editors have such a thing.

| improve this answer | |

Place the code in your init.m file. It will then be run each time the kernel is started. On Linux the init.m file is in ~/.Mathematica/Kernel/. I forget where it is on Windows but just look at the output of $Path and you will be able to find the "Kernel" directory within your account directories.

| improve this answer | |
  • $\begingroup$ thanks for the suggestion, will it still be possible to edit the code in my each individual notebook? For instance I may want to change the directory or the image size from notebook to notebook. $\endgroup$ – Q.P. Nov 2 '16 at 21:12
  • 1
    $\begingroup$ I don't think this is what he was asking. He doesn't just want code to be evaluated every time he opens a new notebook, but he explicitly wants it placed at the top of that notebook (I think). $\endgroup$ – ktm Nov 2 '16 at 21:13
  • $\begingroup$ 6014: Yes I agree this is not exactly what was asked but I think it is a workable solution. |Penguin>:, No you will not be able to easily edit the code for each notebook but you could generalize the function to accept options such that it will work as needed in all cases. $\endgroup$ – c186282 Nov 2 '16 at 21:19

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.