Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a piece of code I keep using in new notebooks over and over again and I have to copy and paste it in each new notebook. Problem arises: if I update the code in one file I have to manually do it in all the other files. So I was wondering, is it possible to create one file, let's call it InitCode-7 July.nb, and use a command as an initialization cell in Mathematica to load it? This way I would only have one file to change and it would apply to all the files where I use the same code.

I am familiar with the concept of headers file in other programming languages but I have no idea how to do it in Mathematica. Thank you.

share|improve this question
Why not put all that code in a package? – J. M. Jul 8 '12 at 14:15
Here's a tutorial index that covers package's alongside the modularity, in general. – rcollyer Jul 8 '12 at 14:22
up vote 6 down vote accepted

A bit of an extended comment, but using a package makes all the sense in the world for a situation like you describe.

You can see a specific example at this earlier question: What is a good coding style for setting and changing application level constants?

In the above answer, a package provides a way to supply constants to any other notebooks.

While more specific than your question, you can use the same approach to standardize, manage, and share code, expressions, functions, patterns, constants, and pretty much anything else to any notebook from which you want to call the package.

share|improve this answer
This is great, it's exactly what I need. I'm going to try it out, thank you. – Frank Jul 9 '12 at 0:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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