How to include a reference file into a MMD doc?

Hello everyone! I’m a french user of Scrivener and I’d like to use it with MMD and Latex. It works well when I compile a document with the APA template, but I need to insert references and I don’t know how to include a bibfile in my document. How could I do that? Thanks for your help.

Something that is good to know is that if you name your compile folder “something-mmd”, where “something” is whatever you want, then the stuff you put into that folder will not be removed when you compile into it. You can thus assemble .sty files, .bib files, logo graphics and anything else you need to support the final typesetting. Scrivener will only overwrite the images you have in your text and any .tex files it creates.

That said, you might want to have a look at Scrivomatic. It’s a Scrivener-based setup for working with Pandocomatic, which is itself a tool for automating the production of more complex documents, handling stuff like bibliographies. You would be using Pandoc instead of MultiMarkdown for the conversion to LaTeX, so there are some slight differences, but both are CommonMark compatible and so you shouldn’t find there to be too many issues with switching.

2 Likes

Thank you for your answer. I didn’t know it could be harder to manage than Latex itself!

The easier way is the first method for sure. You just don’t keep the bibliography file in Scrivener anywhere, but where you compile to, and I believe that’s about all you have to do.

Using Pandoc for the bibliography is simple, you simply pass the location of your bib file and CSL style (it uses a citeproc engine) and it will generate a fully formatted bibliography and citations across any output (HTML, LaTeX, EPub, Word, plain text etc.) Pandoc can create bibliographies itself (CSL), but you can also use it to enable BibLaTeX or NatBib, and it will handle all the changes for you without any document changes. My pandocomatic workflow shows you examples of this, you can just have a look at the workflow without needing to implement everything. In my case I can switch from citeproc to BibLaTeX or NatBib engines just by changing the pandoc templates used: https://github.com/iandol/dotpandoc/blob/master/pandocomatic.yaml#L24 — though in 99% of cases the CSL engine is just easier to use and there are thousands of styles available (more flexible than BibLaTeX)…

If you want to keep using MMD instead of Pandoc, then you can also modify the MMD LaTeX templates to include the BibLaTeX package directly, which is as easy as including it in your header and passing in the location of your bib file…

1 Like

Thank you so much for taking time to answer me. I’ve been using Latex for years now and it took me a lot of time to build templates, create a doctoral thesis template and understand the way it works and I must admit it’s a pain for me to have to dive again into the mechanics of Pandoc. So, considering that I have Pandoc on my computer, this is what I understand:

  1. I have a scrivener file
  2. I create a folder with a bibfile
  3. Do I have to put the pandoc template you indicate into that folder?
  4. I compile my scriv file and… this is the point where I’m loosing myself. How does the compiling recognize what contains my folder?

I imagine that the easiest option for you if you currently use MMD + LaTeX is that you stick with MMD. Here is the MMD instructions for citations which use the [#citekey] format:

https://fletcher.github.io/MultiMarkdown-5/citations.html

I don’t use MMD, but that doc claims that you can pass bibtex metadata and natbib is enabled in the templates. Perhaps @AmberV can let us know whether the built-in MMD 6 templates includes natbib as the docs for MMD5 suggests?

If you do want to move to Pandoc (which I would recommend as the citation system is more powerful), then the in-text citations use this formatting:

https://pandoc.org/MANUAL.html#citation-syntax

They look like [@citekey] and will be converted depending on whether you use CSL/BibLaTeX/Natbib.

For Pandoc here is what you need to do:

  1. Use [@citekey] for intext citations in your Scrivener project. You could create an inline style if you wanted that would add [@ and ] around the citekeys, that would make changing citation styles easier in the future but is not necessary. For example this simple document:
    Here is the generic text [@adolphs1999], with another pair of references [@adolphs2009; @becker2018].
  2. Compile using Multimarkdown and enable Pandoc format in the post-processing pane of the compile editor. I do have a metadata front-matter so my final markdown compile looks like:
---
title: "Compile Test"
author: Jane Doe
---

# Pandoc test #

Here is the generic text [@adolphs1999], with another pair of references [@adolphs2009; @becker2018].
  1. Manually you can run Pandoc from the terminal and pass the BIB file you are using like so:
    pandoc --citeproc --bibliography="biblio.bib" -o cited.docx compiled.md
  2. BONUS: Scrivener allows you to run Pandoc automatically, and so you would modify your command and add it to the post-processing pane:
  3. In these cases biblio.bib is in the same folder you are compiling to, but you can put this file anywhere and then just put the path of the file in the command.

Now compile in Scrivener and it will run Pandoc for you with the correct parameters, et voila:
Screenshot 2022-07-03 at 11.22.30

Note I was using DOCX output as it doesn’t require the LaTeX pipeline, but you can output latex or PDF directly if your LaTeX system is set up appropriately…

Note you can change the style of the formatted references by changing the CSL used by passing --csl="nature.csl" to the command (you can find thousands of styles from here). So I downloaded the nature style, added the --csl="nature.csl" flag to the command and got:
Screenshot 2022-07-03 at 11.37.39

The LaTeX boilerplate can be downloaded and installed. Grab the project as a zip if you don’t git, and extract the textmf stuff into your user texmf folder. This will have two benefits:

  • With the stock compile formats that use MMD boilerplate, you will no longer get spammed with .tex files into the compile folder. Scrivener will check the textmf for their presence and allow those copies to be included naturally.
  • You can of course modify and add your own boilerplate in that area, and create your own “configs”, as MMD refers to them.

Plus, having all of the files on the disk in an easy to edit fashion also makes it easy to answer questions like this one, as one can simply grep -Ri natbib * in that folder and see it is referenced in the textmf/tex/latex/mmd6/shared/mmd6-memoir-packages.tex file, with the sort&compress option applied.

So you can change how that works centrally, in a way that would impact all of your project, by editing that file. But as you can see it’s a pretty modular setup, so if you want to create your own fork that only changes how a few files work, that’s pretty easy to do.

@nontroppo @AmberV: I’d like to thank you for your help. Your advices helped me understand how I could use Pandoc and MMD with Scrivener. I hope that our discussion will help people to use Scrivener in that way.

Following your instructions @nontroppo, I can output a docx. file with a bibliography, exactly as in your example. Up to now, I couldn’t do that! But:

  1. If the style I use — APA 7 in my field of research — is recognized, it is not the case of the APA layout bibliographic style. How could I fix that?

  2. When I add a .tex to the Scrivener parameters section and when I launch Texpad, my references are not recognized and are bordered with {}.

  3. From that, my question: what do I need to set in order to use that process to obtain a tex document without the Tex markup?

@AmberV: I don’t understand the way I could use the LaTex boilerplate. I mean: once I downloaded it, what do I have to do with it and Scrivener? Thank for your patience too.

This is exactly what I’m searching for : https://chris-grieser.de/pandoc_alfred, a workflow which would enable me to output a scrivener file in a .docx or in a.tex document.

@dominique: I don’t understand the way I could use the LaTex boilerplate. I mean: once I downloaded it, what do I have to do with it and Scrivener? Thank for your patience too.

It has very little to do with Scrivener. They are support files that Scrivener itself makes use of if present, so long as you put them in your texmf folder (usually ~/Library/texmf/tex/latex/ on a Mac). So normally if they aren’t installed Scrivener has its own set of those same files that it outputs into the compile folder. If you do have them installed it won’t do that and you’ll get a cleaner compile folder.

Strictly for that benefit alone, I’m not sure if it is worth it. Where it is better is if, say, you like memoir-book but want to change some things about it. With the files you could duplicate the memoir-book folder, start changing it, and then instruct MMD to use it.

So they can be a model for how you could set up your own document design from scratch as well, using whatever preamble you need. Have a look at how the “manuscript” .tex files are put together. That’s far away from what you need, in terms of document style—but as an example of a case where you have a full preamble and just want to chop it up into the three -leader, -begin and -footer files MMD likes to see, it’s good for that (some of the others are a lot more modular, with lots of smaller files).

So let’s say you do this:

  1. Create a folder in your texmf called “apa” (I create my own designs in a folder along side the root mmd6 folder, so as to keep it original).

  2. Create three files:

    • mmd6-apa-begin.tex
    • mmd6-apa-leader.tex
    • mmd6-apa-footer.tex
  3. Just follow the example for the kinds of things you’d put into these three files. Basically -leader is for initial preamble, and -begin is for all of the stuff in the preamble that would need access to the document’s metadata (author, title, etc.). MultiMarkdown is going to insert a bunch of variables based on those metadata, into the .tex file. It will look like:

    \input{mmd6-apa-leader}
    \def\myauthor{Author}
    \def\mytitle{Title}
    \input{mmd6-apa-begin}
    
    <ALL MARKDOWN DOCUMENT CONTENT HERE>
    \input{mmd6-apa-footer}
    
  4. Lastly, as noted above, you’ll need to tell MMD that this is the design you want to use:

    1. Open File ▸ Compile... and duplicate and edit the “Book (Memoir)” compile format by double-clicking on it.
    2. In the LaTeX Options pane, set the document class to “None (Use Metadata)”.
    3. In the Metadata pane, add a row with the + button in the lower right, and call the field “LaTeX Config”, and in the text field below, type in “apa”.
    4. Give the compile format a better name, save it to “My Formats”, and click the Save button.

And that is pretty much the entirety of customisation in MultiMarkdown. You create the three .tex files using the naming convention, refer to them by the middle part in “latex config”, and that’s it.

Pandoc customisation is a bit more complex, but it is also more rewarding and capable. Like I say, what you see above is it, if you need to change how figure syntax is output, you can’t. I would also say that Pandoc has a much larger user pool and a better support network, through workflows like Pandocomatic, Scrivomatic and this Alfred workflow you linked to as well. A lot of people use it, and so there are a lot of “geeks” that have sought to improve it as well.

MMD is a bit less well known. It’s main advantage is simplicity and straight-forward configuration. As you can see, making your own document design is just creating some .tex files. You don’t really need to learn much about it other than that three-file naming scheme and that document metadata goes in between -leader and -begin. But that also means that stuff like bib file management is entirely up to you. MMD can work with it, but there is nothing like Scrivomatic for MMD.

This is exactly what I’m searching for : https://chris-grieser.de/pandoc_alfred, a workflow which would enable me to output a scrivener file in a .docx or in a.tex document.

This is very much more procedural than what I’ve been talking about above, which is all about compiling and getting your output set up the way you want. How you write using Scrivener is another thing, and I have less experience with that myself (I only write using Markdown, but not formally, so I don’t know how to use Zotero and so on).

Great resource though! Thanks for dropping a link to it. I’ve clipped it for anyone who asks about good citation workflows.

Thank you for those precise suggestions. However, I suppose I should use Latex markup – \cite{} – in order to generate citations?
Using the code below on the screenshot, I can now export a MMD file into Word and have a numbered doc which looks nearly as great as with Latex. But, I can get the same result with my bibliography manager, Bookends. I’d like to get the same result into Latex too. Therefore, I need to know how to convert my Scrivener markdown file into a .tex file. So, I wonder: what would I need to insert into the section called Arguments? Would it be --output=".tex" or something like that?

You must download and save the correct CSL (this is V7: styles/apa.csl at master · citation-style-language/styles · GitHub) and apply it by passing it using the --csl= pandoc flag.

Note Pandoc has a data directory and you can store CSL and other files so Pandoc can find them even if they are not in the working directory (data dir on macOS = ~/.local/share/pandoc/csl/)

The references are in Pandoc format in your Scrivener doc, and when outputting TeX files the CSL engine converts them to final formatting so there are no “raw” citation in the tex file by design. If you want to have raw citations so you can use BibLaTeX or Natbib you can remove --citeproc and add --biblatex or --natbib to the pandoc flags and it will then convert [@citekey] to \cite{citekey} for you etc. So for example: pandoc --biblatex --bibliography="biblio.bib" --output=cited.tex compiled.md — should get you a tex file with biblatex citations, but you must run latexmk yourself to get a PDF

I don’t really understand what you mean; as I mention you can make a PDF file directly by following the info below.

For exporting LaTeX via Scrivener’s post-processing pane, Pandoc is smart enough that you simply change the file output suffix: --output=<$outputname>.tex; for good measure you can also specify latex using --to="latex" but in most cases that is not necessary. You can also “run” latex to build a PDF by specifying the pdf suffix: --output=<$outputname>.pdf and you can specify which engine using --pdf-engine= the options are pdflatex, lualatex, xelatex, latexmk, tectonic, wkhtmltopdf, weasyprint, pagedjs-cli, prince, context, and pdfroff — for TeX I use xelatex as this engine works with UTF8 and supports macOS fonts.

I mentioned Chris’ citation tool here: Great new Markdown Citation Chooser using Alfred!

I don’t think you need his tool for running Pandoc, as Scrivener can do that for you already. But his citation picker works really well. Do you use Bookends, if so in the recent version you can keep a BibTeX file synced to your database, and this can be automated, see the guide here: Best practices to work with the Bib-sync feature - Sonny Software

I’m trying to figure out how it works, step by step, reading some documentation too in a language that is not my native one, but when I compile my Scriv. file using this Pandoc argument :

… this is what I get in Texshop:

… and when I compile it with Texpad, the result is that nor references nor footnotes are recognized:

Am I missing something? In Scrivener, there are multiple compilation choices. Is the one I use right?

m

Using Multimarkdown is correct, and I think you are colse to getting this working). You should be getting TWO files in your compilation folder, you are showing the markdown output .md file in your Texshop screenshot and not the .tex file. Are you sure there isn’t a .tex file in the same folder? If this is the .tex file then there is an error in your command somewhere. Try to add --to=latex to the command to “force” it to know it should be latex output.

Another option for debugging: This compiled markdown can be processed by pandoc directly, you could pass --verbose to the terminal command to see whether there are any errors[1] that get returned:

> pandoc --biblatex --bibliography="lectures.bib" --output=out.tex --verbose compiled.md

Scrivener itself normally hides any outputs from --verbose, but you can “redirect” standard output that scrivener normally hides to a file using the post-processing command:

post-processing arguments:

--biblatex --bibliography="lectures.bib" --output=<$outputname>.tex --verbose <$inputfile> >>compile.log 2>&1

This should redirect any pandoc verbose output (2>&1 forces errors to go to standard output and >> forces output to go to a file) to a compile.log file in the same folder. You can see if any errors are present by reading the log file.


NOTE: you do not need --csl if you are specifying --biblatex — citeproc+csl are a different way to build bibliographies. As I mentioned before I much prefer citeproc+csl to biblatex and I only ever use natbib or biblatex if the journal gives me no other choice…


[1] this is one advantage of my scrivomatic wrapper script, it adds full logging from scrivener so you can easily debug any problems.

I have had a hard time getting Scrivomatic to work on Windows 10 starting with the repository as is. I now use Quarto instead. It is constantly being updated and developed by the guys at R. Most configuration can be done in the file’s metadata so you don’t have to update your command line arguments every time you make some changes.

I am not a LaTeX expert, and having documents published according to conference specifications real fast is very crucial for me as a graduate student. Just specifying a template in the metadata shaved hours of editing a document to conform to conference specs and now only have to deal with the content I need to write. Quarto made this real easy for me. It just works out of the box.

I’m sorry but, it seems that it does not work at all, whatever I try and I tried a lot of things. The only thing that I’m happy with is that I can now to output a tex document from Scrivener. But, the same problem subsists: my citations are not recognized.

Are you sure you are outputting tex, from your previous screenshot you were showing us markdown?


Can you try this from your terminal (simplify the problem space to a minimum). You can run Pandoc directly and enter markdown “live” in the terminal window: so for example type this command into the terminal (changing to your bib file path):

> pandoc --biblatex --bibliography=/Users/ian/.local/share/pandoc/Core.bib --to=latex

press [enter] in terminal, and now pandoc is waiting for input; then type or paste in:

# This is a heading

Here is some text with a citation [@adolphs2009]. Here is *emphasis*. 

and press [ctrl]+[d], this tells pandoc to process the input and output to the terminal; this is what we should get:

\hypertarget{this-is-a-heading}{%
\section{This is a heading}\label{this-is-a-heading}}

Here is some text with a citation \autocite{adolphs2009}. Here is
\emph{emphasis}.

Note that [@adolphs2009] has been transformed to \autocite{adolphs2009} as expected and this can now be parsed when converting latex to a pdf using latexmk or your GUI.


Note if we do the same but using --citeproc instead of --biblatex we get this latex output:

> pandoc --citeproc --bibliography=/Users/ian/.local/share/pandoc/Core.bib --to=latex
# This is a heading

Here is some text with a citation [@adolphs2009]. Here is *emphasis*.   
          
\hypertarget{this-is-a-heading}{%
\section{This is a heading}\label{this-is-a-heading}}

Here is some text with a citation (Adolphs 2009). Here is
\emph{emphasis}.

\hypertarget{refs}{}
\begin{CSLReferences}{1}{0}
\leavevmode\vadjust pre{\hypertarget{ref-adolphs2009}{}}%
Adolphs, Ralph. 2009. {``The Social Brain: Neural Basis of Social
Knowledge.''} \emph{Annual Review of Psychology} 60: 693--716.
\url{https://doi.org/10.1146/annurev.psych.60.110707.163514}.

\end{CSLReferences}

Here we can see that the CSL engine has turned the temp citation to final text and created a bibliography for us. If you don’t see the same outputs then there is a problem with your install somewhere…

When I launch the terminal, this is what I get: is this an answer?

cnf

It is hard to see from that screenshot… But you must replace my bib file path with your one. Lets say your user name is dom, and you keep your bib file on the Desktop:

pandoc --biblatex --bibliography=/Users/dom/Desktop/lectures.bib --to=latex

EDIT: technically just for testing latex output you don’t even need to specify the bib file at all:

> pandoc --biblatex --to=latex
# This is a heading

Here is some text with a citation [@adolphs2009]. Here is *emphasis*. 
            
\hypertarget{this-is-a-heading}{%
\section{This is a heading}\label{this-is-a-heading}}

Here is some text with a citation \autocite{adolphs2009}. Here is
\emph{emphasis}.