# Automatically export .nb to a package upon saving while preserving section headings

Is it possible to automatically export a .m package when I save my .nb as can be done using the option "AutoGeneratedPackage->Automatic" see https://mathematica.stackexchange.com/a/31323/45020 but presrving the section headings and text cells? (Still excluding non-initialization cells.) Unfortunately the AutoGeneratePackage does not include headings by default.

It was pointed out to me by Jason B. that the default export using Save As Package does preserve the headings as I would like. Can we get this behavior also in some automated method (ideally exporting every time I save the .nb).

About my edit: Originally I did not know that Save As Package does preserve the headings. I assumed that the behavior of "AutoGeneratedPackage->Automatic" was the default. While Save As does offer a solution I would much prefer an automatically exporting one.

• Try File->Save As-> and select Package from the dropdown menu. Just be aware that any code you want to save to the package must be in a Code cell. Jun 11 at 14:21
• Right, so I know the standard ways of exporting a package. My question is explicitly about a way to include headings and text cells. As far as I know these are mutually exclusive and so cannot be Code cells. Jun 11 at 14:27
• When I try the standard way of exporting a package, text cells and Section/Subsection/etc are preserved, is this not the case when you tried it? Jun 11 at 14:42
• I apologize. I thought I was using the equivalent of a standard export. Clearly you are correct and this question is based on a misunderstanding on my part. I am using "AutoGeneratedPackage->Automatic" (see mathematica.stackexchange.com/a/31323/45020). This automatic method does not preserve text cells and headings. I will edit my question to ask the better question of how to automatically generate a package that preserves these. Jun 11 at 14:51