When compiling from multimarkdown to Latex, is there a way to to run a post-processing script (e.g. batch file on the command line) once the compile process finishes? I am basically looking for a way to automate the whole process of producing a pdf via Latex and possibly add additional steps by running pandoc as well. I see that the raw mmd compile designer has a processing pane, but this does not exist for the mmd → latex compile format. I’m on windows.
It is better to use the direct MMD compile option, do not use the built-in prefabs.
In the post-processing pane you want to add a script. This will handle calling pandoc and manually running any post-processing tools you may need. You can roll this yourself, but there are existing tools that already do this.
I use pandocomatic. This awesome tool combines a pandoc recipe manager with extensive hooks to run scripts. For my Scrivener LaTeX workflow this is what happens:
pandocomatic allows: preprocessors
to run before the markdown is passed to pandoc, postprocessors
that runs on the TeX file after pandoc has generated it (see dotpandoc/postprocessors/fixLaTeX.rb at master · iandol/dotpandoc · GitHub for what I run), and cleanup
which can do general cleaning or e.g. transfer final files to a web site etc.
You don’t need to use pandocomatic, but I personally find it is great that all my scripts and my pandoc settings, metadata and filters are specified in a single recipe. I run pandocomatic from the Scrivener post-processor, then it takes over. The recipe that pandocomatic will use is specified in the Scrivener metadata, so you can simply edit this in the Binder and set it as front-matter. Other people use the venerable make
command, or newer tools like just
.
NOW, this is the most elegant and flexible way to automate, but most of these tools are written on Unix systems, and on Windows you will need to adjust the tools used, the paths where things are stored, whether you will use WSL2 or MSYS or git-bash or will try to translate to Powershell etc. At least when I tried a few years ago to set up my toolchains on Windows for a colleague, it was frustrating for me at least. My memories were that WSL was going to be the best way to handle it, but due to some path errors I think we ended up with him just dragging the markdown and using a Linux VM for post-processing…
Thanks @nontroppo for the response. I have read about this but since I am on Windows the technical stuff is a deal-breaker. Just not worth it for what I’m trying to accomplish – TBH I am just being lazy and trying to eliminate the single step of running a .bat file after compiling. I am curious about why it might be better to use the direct MMD compile option.
Well, the “better” is exactly what you asked for in that you can run post-compile-processing automatically. You also have more control as to the exact settings pandoc uses; want to toggle list of figures, want to try a different bibliography engine, basically any of the settings pandoc allows you to change are now accesible to you. You may not need this flexibiity, and I do agree that the extra complexity as you on Windows may not then be worth it for you.
Scrivener does not have a post-processing + post-compile command. So there isn’t an alternative from Scrivener’s side:
- Run your post-compile script manually.
- Use Scrivener’s post-processing with MMD direct and a tool like pandocomatic.
There is another solution outside of Scrivener. There should be tools that can automate running a bat file; on macOS I can use launchd to watch a folder or a file and trigger a command if that file/folder changes. You could do something similar so you use the Scrivener prefab to make the TeX and an external tools that monitors the comile folder for the change, runs your post-compile tweaks then runs latexmk for you all automatically? So you would just hit compile in Scrivnener and get a polished PDF without further steps.
Thanks. Not having used the plain vanilla MMD compile option, I am not 100% sure how it works. If one ticks the ‘post-process on command line’ box and then then provides the path to an arbitrary file that can be run from the command line into the ‘path’ field, does Scrivener just run that file? Or is there something more complicated going on? I’m just wondering if I should turn this question into a feature request, just to add the postprocessing option to the MMD → latex compile option (since it already exists for the plain vanilla MMD compile, this seems like a small ask). It seems to me that the only reason to compile to .tex is to compile (again) the .tex file to produce pdf or whatever. The only thing that my .bat file does in Latex is pdflatex myproject.tex
so there is nothing complicated whatsoever, it would just be nice to do this automatically and given that Scrivener already has some post-compile processing implemented, it seems like this could be a popular feature for those using Latex.
Post-processing in Scrivener refers to processing the markdown itself. When you run MMD > LaTeX Scrivener is automating the tool that runs on the MD file, not any downstream files generated by that. What you want is not directly possible for Scrivener as from what I understand it runs MMD only (i.e. Scrivener calls MMD and it is MMD that converts MD to TeX and runs pdflatex
, it is out of Scrivener’s hands…)
You can of course make a feature request
The plain MMD + post-procesing script allows you to do what you want, as the script can do something like:
pandoc --to latex --output myscrivdoc.tex myscrivdoc.md
tweak-latex.bat myscrivdoc.tex
pdflatex myscrivdoc.tex
It shouldn’t be too hard, and now if you want to tweak any setting, you have full access to the commands that create your final work.