# Customizing MultiMarkdown to make Scrivener easier, part A

m on May 12th, 2008

[UPDATED]

In my last post, I showed how to install the biblatex package along with the MLA style rules for the bibliography and citing in MacTeX. In a later post, I’ll discuss how to roll your own biblatex styles (hint: it’s not that difficult). But for this post, I’m making the first gestures towards the bridge between Scrivener and TeXShop (or, just, .tex).

Scrivener comes built in with support for Fletcher Penney’s MultiMarkdown language. Simply put, MMD lets you type:

Farrell writes, in *Judgment Day*, that “There was really no place to go"[924][#farrell_studs_2004].

And you get, after exporting:

Farrell writes, in {\itshape Judgment Day}, that There was really no place to go''\cite[924]{farrell_studs_2004}.

Similarly, MultiMarkdown changes the “footnote” field in Scrivener (the colored bubble over the text) into \footnote{} regions. There are a few other things MultiMarkdown does, of course, but I’ve included above the basics for writing in the humanities: how to italicize, how to make a (BibTeX/biblatex) citation, and that unicode characters (the fancy quotes are generated in Scrivener) get converted to their LaTeX counterparts. Making blockquotes (start a line with “> “) is displayed in the example at the end.

Now there are two steps not included, that are, all the same, pretty darned important: setting the metadata and choosing the right .xslt. The XSLT is the XHTML/XML translator that converts the xml generated by what you wrote in Scrivener into LaTeX. It works decently, but the default settings are not what I want. The metadata business I’m saving for a separate post.

There were basically four things (for now) that I needed to do to the default MultiMarkdown .xslts to get the result to match what I wanted, so that there would be very, very little editing of the .tex file at the end of the process.

This meant that I would have to edit the .xslt files. The best way to do this is to install your own copy of MultiMarkdown from Fletcher. This installs MMD in ~/Library/Application Support/MultiMarkdown. There, in the XSLT folder, you will find all the .xslts we will be messing with. Anyway, here were the four things I wanted to do, and a present at the end:

## 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"/>

to:

<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:

<xsl:text>\documentclass[oneside,article]{memoir}</xsl>

to:

<xsl:text>\documentclass[12pt,oneside,article]{memoir}</xsl>

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

to:

% 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,

to:

% 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:

\title{\mytitle} \author{\myauthor}   \begin{document}

to:

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

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"/>

to:

 <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:

\bibliocommand % biblatex needs this command before the document begins

\begin{document}

to:

\bibliocommand % biblatex needs this command before the document begins

\DisemulatePackage{setspace}
\usepackage{setspace}
\singlespacing

\begin{document}

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.

UPDATE: As signinstranger pointed out, the memoir class does have doublespacing built in, so there is no need to use setspace. To start typing doublespaced, run the command \DoubleSpacing on a line by itself in Scrivener. When you’re ready to jump to singlespacing, type \SingleSpacing. More information can be found on page 11. of this pdf.

## GIVE ME AN EXAMPLE FILE!

OK. Here’s what I typed into Scrivener:

This is some English text. It is a whole paragraph so we can see the spacing of the document. In order to take up a paragraph, I will repeat this sentence. In order to take up a paragraph, I will repeat this sentence. In order to take up a paragraph, I will repeat this sentence. In order to take up a paragraph, I will repeat this sentence.

\doublespacing

Here is a second paragraph. However, I would like to type in Russian. Luckily, my font is set to Hoefler Text, which has support for Cyrillic. И я могу сейчас писать по-русскии хорошо. Now I’m back to English. But wait, my friend sent me a letter in Chinese, where he tells me about his likes and dislikes. Hoefler can’t handle Chinese, so I need to change the font to let you know what he writes:

\setromanfont{LiHei Pro}

> 我很喜欢吃小孩子。

\setromanfont[Mapping=tex-text]{Hoefler Text}

I’m not letting this friend near my younger siblings!

I think this is enough to show how neat Scrivener can be! I’m in love! Sadly, I didn’t feel like putting in the extra effort of getting Hebrew to print correctly. That’s the next step!

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.

### 4 Responses to “Customizing MultiMarkdown to make Scrivener easier, part A”

1. This post was pasted in edited form on the Scrivener/MMD forum:

http://literatureandlatte.com/.....&sd=a

There, signinstranger addresses three of my issues:

1. signinstranger cautions against hardcoding memoir-xelatex.xslt, though I don’t fully understand why.

2. signinstranger suggests using the \let command to take care of the \Cite and \cite problems. As I write, too much gets automatically capitalized in Scrivener, which means user education is the best plan.

3. Finally, the memoir class’s version of setspace is pointed out in the memmanadd.pdf. I don’t need to call setspace, I can just use the command \OnehalfSpacing or \DoubleSpacing to get “double spacing.”

2. Hi,

I fiddled arround as well and finally got Scrivener to output LaTeX with my custom template.

I found a “solution” to two glitches:
for both you have to edit clean-text-allow-latex.xslt:
– in line 177 change to:
\%–>
%
– in line 195 change to:
<!– leave unterstrich alone!
\_–>
_

so that _ remains _ (and not \_) and LaTeX-comments are still those (however: if you want to enter the ‘%’-sign e sure to escape it \%).

However, this is not an optimal solution:
- Multimarkdowns concept is to not use format-specific code … so all content can be transfered to other formats. I don’t want to miss all of laTeXs features, so I don’t care about this one.
- signinstranger disliked changing the provided xslt-files for consistency reasons: once an update is made this files get replaced and the hand-made additions have to be entered again. Maybe one can contact fletcher and convince him to include this fixes (or make it right!).

3. ups – the html-tags got removed.
New try without the

– in line 177 change to: