I recently put together a little demonstration of how Scrivener could be used to generate a bunch of WikiMedia formatted .txt files from a single compile job. The premise is very simple, and only took a few minutes to convert to a LaTeX splitter instead:
- Have a compile format with a Layout that generates a token as a separator, to indicate where the next file should begin.
- Build a shell script that splits the compiled output up by this separator and prints each chunk of text into its own temp file.
- Process each temp file using MultiMarkdown/Pandoc/etc. to create the .tex file and save the output into the compile target folder.
- Don’t worry, with Scrivener we can automate all of this.
File splitter sample project (226.5 KB)
The attachment includes a sample project and a quick and dirty Ruby script that handles the splitting and conversion:
-
Scroll through a few of the sample documents. It’s all pretty basic MMD-formatted stuff. There are a few footnotes and images for testing to make sure they all end up properly referenced in each .tex file that needs them. Of particular note here is that you shouldn’t need to change much if anything of how your project is currently set up. You don’t even have to use folders and files if that’s not how you work—the whole Section Type <> Section Layout binding system gives you that flexibility. I’m using a Section Type called “LaTeX File” but that’s just for clarity—more likely that would be “Chapter”; the name doesn’t really matter.
-
Go to
File ▸ Compile...
and take a look at the preview column in the middle, where the layout being used by “LaTeX File” is of most interest. -
The rest is all done in the “MultiFile Output” compile format on the left. Double-click that to edit it.
-
The “New File” Section Layout is what is assigned to folders. Click on the “Prefix” tab to see where the split marker is inserted. It’s nothing special, only sufficiently unique enough to probably not be encountered naturally.
I am using the <$title_no_spaces> placeholder to generate file system friendly names from the folder titles, but it might be better to clean filenames in the post-processing script if your chapter titles have potential problematic characters in them. The serial number will help keep these files in binder order on the disk.
-
Next, in the Text Layout pane we’ve inserted a manual file split marker at the very bottom of the document. This will directly precede the chunk of text where Scrivener inserts figure and footnote references. The way this works is that Scrivener appends all markers at the very bottom of the document. Our script will lop that off and append it to each temp file. The unused reference tokens from other sections are no matter, MMD ignores them.
-
Last but not least is the Processing pane. We enable post-compile shell scripting, with the script itself embedded in the compile settings (Click the
Edit Script
button to view it. A copy of the script has also been provided as a stand-alone file, for your reference). Lastly we delete the temporary Markdown file used to generate the output, leaving only the .tex files. You can switch that off if you want to “debug” the process or modify the script.The script itself doesn’t take any arguments other than the compile output filename, which Scrivener automatically supplies if you do not otherwise.
Save the Format with the updated path to the script and give it a test compile. Note that since this includes images, you’ll be giving a folder name when asked for a name at the top, all of the .tex files and images will end up in that folder. If your work doesn’t have images, then you might want to create your own folder to save into, as this will of course be generating a bit of clutter.
You’re going to need your own copy of MultiMarkdown installed for two reasons:
- The Ruby script assumes ‘multimarkdown’ is on your path.
- Everything is set up for MMD6, including the .tex boilerplate files for “article”, which is what the demo settings currently are. If you want something else, edit the “LaTeX Config” metadata key in the compile overview screen to “memoir-book”, or whatever you need.
I think that’s everything that might need explanation. Let me know if you can’t get it working or run into any other problems or areas that are confusing. If you want to import this into your project, it might be best to integrate the ingredients into a duplicate of your main compile format if you do a lot of custom stuff with it. If you’re using a pretty stock setup though, easiest thing might be to just export the format from this demo project as a file (gear button below Format sidebar in compile overview) and import into your project—or edit the format and save it to “My Formats” instead of being project-based. That’ll open it up to every project you have. Each compile Format has its own layout assignment settings, meaning you can switch between a singular PDF and a bunch of .tex files with a single click once you get it set up.