# How do I insert a piece of code in Mathematica?

In Fortran, I can use include "file" to insert a piece of code. Is there a similar method in Mathematica?

-
Have a look at the docs on packages. – b.gatessucks Dec 27 '12 at 13:45
@b.gatessucks I think that should be an answer... – acl Dec 27 '12 at 14:43
the semantics of include "file" is completely different from using a package. include copies the content of file directly and put it at the place where include statement was. It acts exactly as if one used an editor and used copy-paste to put extra code at that location. This is the same with c #include as well. Here is reference to Fortran include. docs.oracle.com/cd/E19957-01/805-4939/6j4m0vna1/index.html – Nasser Dec 27 '12 at 15:10
@acl Can't do it right now, please do go ahead. – b.gatessucks Dec 27 '12 at 17:12

Put your external code snippet into a file toInclude.m, for example with the content

Plot[Sin[x],{x,0,Pi}]


Now in the "parent" notebook (assuming it's in the same directory as the code snippet), execute the command

Import["toInclude.m"]


or

<< "toInclude.m"


and you're done. Here are the references for << (Get) and Import.

-