4 tricks with MMD and LaTeX exporting via .xslt editing.


I am in the process of documenting the heck out of the dissertation I’m getting ready to write. The drafting will be done in Scrivener and the publishing in TeX, so I’m very interested in how the two play together via MMD. Anyway, on my blog I’ve started a category, “The Humanities Dissertation Project,” and I’ve put up my first Scrivener/MMD specific post today:

1984produkts.com/donkeyhotti … er-part-a/

This gives documentation on:

  1. Getting MMD to use XeLaTeX instead of LaTeX
  2. Getting MMD to use biblatex
  3. Getting MMD to let you type LaTeX straight into Scrivener
  4. Getting MMD to let you doublespace

At least two of these tips I built from what I read in this forum, but my buddy wanted all of them in one place (so he could be convinced that Scrivener was worth his money). Anyway, below I paste in the directions, should my blog ever go belly up. Thanks KB and Fletcher for making my life both easier and more interesting!!!

Getting MMD to use XeLaTeX

I would normally use the “article” document class in LaTeX to write something like a dissertation proposal. MMD, however, prefers to keep everything within the memoir class, which has a flag that mimics the article class. In the Scrivener project MultiMarkdown Settings metadata, I tell Scrivener to use article.xslt as the value for the “LaTeX XSLT.” So the first file we have to change is ~/Library/Application Support/MultiMarkdown/XSLT/article.xslt.

The change I need to make here is simple: we can see that it imports the memoir.xslt file. Fletcher has written a memoir-xelatex.xslt. So change line 39 (in the version I’m using) from:

<xsl:import href="memoir.xslt"/>


<xsl:import href="memoir-xelatex.xslt"/>

There is not much difference between the two memoir*.xslts. In fact, the xelatex one calls memoir.xslt. But it does declare the fontspec package, and it disables the inputenc package.

While you have article.xslt open, though, you might as well change line 52 as well, to get your text to be 12pt. Change:




Congrats. This was the easy step. If you export a document from Scrivener now, it’ll include the fontspec package, so you can change the .tex file and specify which font you like.

Getting MMD to use biblatex

This is a bit trickier, and it assumes that you’ve installed both biblatex and the MLA definitions. If you haven’t, read my tutorial on doing that. First, let’s open the file we declared in the previous step, memoir-xelatex.xslt. Here we can see the “latex-header” template. On line 55, you can even see where Palatino is the default font for XeLaTeX. If you like, change that to something more dramatic, like Hoefler Text. Now it’s possible to include the biblatex changes here, but then if you ever write a file that does not use the memoir-xelatex.xslt, you will have to add the biblatex support in that xslt. Better hold off.

So instead, then, let’s move up the inheritance tree to the regular memoir.xslt, which gets called by memoir-xelatex.xslt. Here we see the template for “latex-footer”, which includes where the bibliography command is printed. In biblatex, you print the bibliography using the \printbibliography command, so, starting on line 55, we can change:

% Bibliography \bibliographystyle{\mybibliostyle} \bibliocommand


% Bibliography %\bibliographystyle{\mybibliostyle} %\bibliocommand \printbibliography % this is how biblatex wants me to make the bib.

We could now declare the biblatex package elsewhere in memoir.xslt, but it’s better to move up the tree one more step, into xhtml2latex.xslt. Still, if you’ve got memoir.xslt open, hop on over to line 108 and change the margins from 1.5in to the more reasonable 1.0in.

Now that you’ve got xhtml2latex.xslt open, we can see that lots and lots of the magic gets done here. But we want to jump down to where the “latex-intro” template begins, which is at line 1077. The first step here is to commend out pagebackref. In general, I’m not a big fan of the hyperref package, as I think having hyperlinks in pdfs is often unsightly. But no matter what you think of hyperref, biblatex complains about pagebackref all the same. So change line 1090 from:



% pagebackref, % biblatex complains about this.

The next step is to call the biblatex package with the MLA style, as well as run the bibliography command. In the default memoir.xslt, \bibliocommand is run in the footer. Biblatex wants it run before the \begin{document}. So move to where \begin{document} happens (line 1135), and just before it, change:





\usepackage[style=mla]{biblatex} % Adds MLA biblatex.
\bibliocommand % biblatex needs this command before the document begins


Note that \bibliocommand is a nonce command defined by MMD during the process of translation. If you were writing a LaTeX document from scratch, you would type \bibliography{/your/path/to/the/bibtexfile.bib} instead.

Save, and now when you export (assuming that you set your BibTeX metadata in Scrivener appropriately), you should be able to roll the MLA biblatex bibliography.

Get MMD to let me type LaTeX straight into the Scrivener window

This is not yet fully perfected, as some escaping still gets done. But it’s not a lot. To type LaTeX into Scrivener, the only change you need to do here is made in xhtml2latex.xslt, which we just used. Change line 40 from:

<xsl:import href="clean-text.xslt"/>


<xsl:import href="clean-text-allow-latex.xslt"/>

Now you can type LaTeX code straight into the Scrivener window. Apparently, LaTeX code typed within HTML comments (<!– and –>) goes through cleanly, regardless. If you’re typing something longer, an equation, say, I would encourage typing it within the HTML comments.

There are two glitches I’ve found:

  1. If I type, say, \cite[924]{farrell_studs_2004} in Scrivener, MMD changes that to \cite[924]{farrell_studs_2004}, which makes BibTeX freak out. The [page][#key] MMD syntax is an ok alternative, but it limits some of the power of biblatex. Of course, doing a global find replace in on _ is not the end of the world.
  2. If I type Here is a sentence.\cite[page]{key}, Scrivener changes the \cite to a \Cite. This is, of course, a different command. And it is one that biblatex doesn’t understand. So it freaks. I could either disable auto-caps on Scrivener, or just pay attention to this quirk. It seems that Scrivener should know to avoid auto-capping after a backslash!

All in all, though, a nice feature.

Get MMD to double space for me

Interestingly enough, this step took me the longest to figure out, since no matter what I did, XeLaTeX would flip out when I’d run the command \doublespacing. It turns out that the memoir class doesn’t like the setspace package. I have no idea why. In any case, go back to xhtml2latex.xslt, and scoot down to line 1136, which should be where we declare \bibliocommand for biblatex. Here, change:

[code]\bibliocommand % biblatex needs this command before the document begins



[code]\bibliocommand % biblatex needs this command before the document begins



Now this defaults to singlespacing. However, since you can flippin’ type LaTeX straight into Scrivener, wherever you want to start double spacing, just type \doublespacing on a line by itself, and everything from then on will be double-spaced until you toss out a different spacing command.


OK. Here’s what I typed into Scrivener:

And here is the pdf I got: http://www.1984produkts.com/donkeyhottie/wordpress/wp-content/uploads/2008/05/tester.pdf.

My trouble with the Yiddish text I was using was that I could not get, in three tries, a font to render the alef with the patah correctly except for Times New Roman, which wrote the letters rtl, but the words ltr. So I punted. I found, however, this page which describes how to get Hebrew (and any other rtl) language up and running in XeTeX. And, since you can type LaTeX straight into Scrivener, you can type merrily away in your rtl language of choice surrounded by whatever environment you choose. But adding the various extra settings in the preamble are beyond the scope of this little tutorial.
tester.pdf (28.8 KB)

You can select memoir-xelatex.xslt in Scrivener’s “MultiMarkdown Settings” (Key=LaTeX XSLT), there’s no need to hard-wire the xslt files.


→ memmandadd.pdf, page 11

Correct. But can I have multiple LaTeX XSLTs defined per .scriv project? I don’t think so. I’m using, in the examples above, the article.xslt, which imports memoir.xslt, in order to write my diss. proposal. I would rather have it import memoir-xelatex.xslt.

Yes, but Scrivener does that for many more commands than just \cite. Any command at the opening of a line gets autocapped, including \epigraph (for which I know there is an MMD trigger), \begin{}, \doublespacing, etc. I think user awareness of this feature is the best route, unless Scrivener would not autocap anything preceded by a . It’s not at all a complaint I’m making here–just documenting an observation.

Thanks. Now I wonder why I found the answer the way I did in google.

Oh sorry, you’re right. :blush:

I tried to reproduce this behaviour, but Scrivener does not “autocap” LaTeX commands here (latest beta). That’s very odd…

I’m using 1.11, and it certainly does. LaTeX commands beginning on new lines get capitalized as soon as I type “{” or space, and the same happens after periods. And now I see that my friend just IMed me, without prompting, complaining about this very same thing… Again, if I turn off the auto-capitalize beginnings of sentences preference, it works fine.

Try upgrading to the latest beta 1.12 … don’t be put off by the fact that it’s a beta, it’s as stable as 1.11 and has various things smoothed out … signinstranger says he is using the latest beta …

I guess there might also be a question of which version of OS-X you and he are using. If you’re using 10.4.x, and he’s using 10.5.2, for instance …


No… it turns out that this is indeed Scriveners default behaviour. I was just too stupid to enable “Capitalize first letter” properly in the preferences. I guess we have to ask Keith, if he can do something about it. Let’s hope that this isn’t some funny feature of the cocoa text system.