3
$\begingroup$

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.

$\endgroup$
  • 4
    $\begingroup$ Why not put all that code in a package? $\endgroup$ – J. M. will be back soon Jul 8 '12 at 14:15
  • 2
    $\begingroup$ Here's a tutorial index that covers package's alongside the modularity, in general. $\endgroup$ – rcollyer Jul 8 '12 at 14:22
6
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ This is great, it's exactly what I need. I'm going to try it out, thank you. $\endgroup$ – Frank Jul 9 '12 at 0:49

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.