Configuring Scrivener to output LaTeX

Write here, write now. Scrivener.The previous post explained why I fell in love with Scrivener for writing journal papers.

Latex logoThis post explains my Scrivener + LaTeX workflow and how I configured Scrivener to output LaTeX.

Edit 25 May 2016: As indicated in the comments below, I no longer use this workflow (which requires MMD). It just got too complicated. I now write the Scrivener manuscript directly in latex and then compile as a text file, which I then compile to latex using my favorite tex editor. This procedure bypasses MMD completely. I’d love to be able to use Scrivener + MMD the way it was intended – compile the same file to any format I want… at least RTF and Tex…*and* have citations, tables, figures, and equations numbered and referenced correctly . Tips welcome. 🙂

So you want to write in Scrivener but compile with Latex?

Scrivener does not do bibliography management, nor does it do typesetting. You must Compile the manuscript in Scrivener and then use whatever biblio manager and typesetter you normally use.

The following two posts are quite helpful for getting started on configuring Scrivener to output in Latex. It actually looks more complicated than it really is. The configuration I’m using (explained below) is a variant of these.

It didn’t take me too long to get everything up and running – including making some modifications to the workflow (explained below). You do however need to install MultiMarkdown 3 (MMD3) first.

My Scrivener/LaTeX workflow

Here’s my current workflow (configuration details explained in the next section):

  • Start a new project or import existing latex file(s) into Scrivener
  • Configure Latex options and include the latex header, preamble, footer, biblio information; save the configuration
  • Ensure parts/chapters/sections/subsections are created correctly: create one ‘file’ per section (also makes it easier to do split-view on two different sections)
  • Write the manuscript content in Scrivener; use the research and notes features extensively (they’re excluded when you Compile the manuscript)
  • Add citations using MMD notation
  • Include latex code (in the manuscript or in separate files) or MMD notation for figures, tables, equations, lists, and cross referencing
  • Output Latex file (confusingly called Compile in Scrivener)
  • Run Texmaker and compile Latex file as usual
  • Do revision control within Scrivener – they’re called Snapshots

My Scrivener/LaTeX setup and configuration

I configured the Latex compile options a little bit different from the two posts above, but the output is nearly the same. You can try whatever works for you. My way may not necessarily be the best way. Suggestions and comments are welcome!

1. Configure Latex options

From the main menu, File -> Compile will take you to the right configuration screen. (MMD3 needs to be installed, otherwise you won’t see all the compile options).

Contents screen: Check or uncheck the sections to be included in the output.

Formatting screen: Check both Title and Text to include the section Title in the latex file, otherwise none of the heading names will show up. Uncheck Title to use the manual section naming and referencing option below. Also change Compile for: to  MultiMarkdown to Latex (.tex)

compile_menu_1formatting

Latex screen: Set Format as -> Custom and  LaTeX document class -> Custom, and define your own latex header/footer info.

This is where my configuration is different from the websites above. I put all the relevant latex info in these 3 Latex screens, whereas others put them in the Meta-data screen (e.g. biblio file definition, location to preamble file, etc).

compile_menu_2latex_2begin

Here’s an example of the latex code I used:

Header screen:

\documentclass[a4paper]{spie} 
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{multirow}
\title{Paper title}
\author{First Author\supit{a}, 
Second Author\supit{b},
and Third Author\supit{c}
\skiplinehalf
\supit{a}University of Awesomeness; \\
\supit{b}Technical University; \\
\supit{c}University Hospital; \\
}
\authorinfo{Send correspondence to First Author, 
E-mail: me@myuniversity.edu}

Begin document screen:

\ifx\citep\undefined
   \let\citep\cite
   \let\citet\cite
\else
   \renewcommand\citep{\cite}
   \renewcommand\citet{\cite}
\fi
\begin{document}
\maketitle

Footer screen:

%%%%% References %%%%%
\bibliographystyle{spiebib} 
\bibliography{my_biblio}
\end{document}

Meta-data screen:

I only added 1 key. Click Add Key, then define:

Keys:    Base Header Level 
Values:  3
  • Level 1 creates \part , \chapter , \section     …  and each subsequent level (n) ignores the (n-1) first levels:
  • Level 2 creates \chapter , \section , \subsection, \subsubsection
  • Level 3 creates \section , \subsection , \subsubsection (this is what I use for articles)

compile_menu_5meta_data

Save Preset…: You should use this option to save your latex configuration. Sometimes changing the Latex options erases your custom header/footer text (e.g. if you change Format As to Article and then back to Custom).

2. Adding references to sections can be a bit tricky if you change the section name – the latex label will also change

When you compile as Latex, the section names you see in the Binder or Outline view will automatically be converted to latex labels with MMD if you have enabled the Title option (see step 1).

In this screenshot, the section (file) being edited is called Step 16: Compiling the Draft. MMD will generate the following latex label, which you can refer to within the manuscript (see point 4 below): \label{step16compilingthedraft}.

So if you change the name of the section in Scrivener, MMD will also change the latex label. There doesn’t seem to be a workaround for this currently, so keep track of your section names if internal section referencing is important (and it usually is).

Two alternative options:

(A) Use MMD3 to create internal cross referencing (see point 4 below).

(B) Manually add in latex code for your section names together with the labels you define (see point 5 below). Optinally disable the Title output (File -> Compile -> Formatting) to suppress automatical label generation, but don’t forget to add your own latex section headings since those will be suppressed too.

3. To include references, I use MMD notation

Writing [#citation2015] will produce \citep{citation2015} when compiled as Latex.

By default, \citep{} is produced.  You can generate \citet{} by adding a semi-colon at the end:  [#citation2015;].

But sometimes you need  \cite{} instead. Here is a fix I found. Just add this in the preamble (already included in the Begin document code above:

\ifx\citep\undefined
  \let\citep\cite
  \let\citet\cite
\else
  \renewcommand\citep{\cite}
  \renewcommand\citet{\cite}
\fi

4. MMD notation can also be used for equations, tables, lists, and cross referencing

You can also use MMD to create other types of objects like equations, tables, and lists. Refer to the MMD syntax cheat sheet for helpful MMD alternatives to latex code.

Note: equations using $…$ notation will convert with MMD, but you do need to leave spaces around the equation, otherwise the $ symbol gets escaped with a \ after conversion.

5. To output raw latex code (equations, figures, tables, labels, section referencing), wrap it with HTML comments

Use HTML comments to pass latex code through untouched by MMD:

<--!  LaTeX code here -->

To include a reference to the automatically generated Step 16 section label above, you can do:

Refer to section <--!~\label{step16compilingthedraft}--> 
for further information.

OK, it’s a bit kludgy, but it’s so much fun to write in Scrivener, that I’m willing to put up with a bit of kludge (cause, you know, latex on its own is so easy to read).

6. Optional: Add LaTeX code for equations and tables in separate files

It can be cumbersome to include equations or tables in the Scrivener file if you need to compile and debug the latex code as you’re writing it. A work-around is to add the code in one or more separate files and include a latex include command in the Scrivener manuscript. You can then debug the code in your latex editor/compiler without having to re-compile the Scrivener file with each edit.

Add this code to the Scrivener file:

<--! \input{manuscript_tables} -->

Then add and debug the latex code for your equations/tables in the manuscript_tables.tex file.

7. Compile Scrivener manuscript

These steps will generate 4 output files:

  • YourOutputFilename.tex => contains the contents of the manuscript
  • mmd-scrivcustom-header.tex=> contains text from the Header screen under File -> Compile -> Latex
  • mmd-scrivcustom-begin-doc.tex => contains text from the Begin Document screen
  • mmd-scrivcustom-footer.tex => contains text from the Footer screen

8.  Compile the tex files as usual with your favorite latex compiler, and you’re good to go!

I admit, working with Scrivener + Latex is not as seamless as writing exclusively in a Latex editor, but the added-value offered by Scrivener’s features can be worth it. As an alternative, MMD notation may simplify some things (even though I have been resistant to using it).

Compiling Scrivener as Word

Sometimes you need to create a manuscript in Word format. You can compile your Scrivener manuscript directly to Word, but then you need a bibliography manager that works with Word. You might try Endnote, Zotero, or other reference managers that have Word plugins. If you use Jabref, you can try Bibtex4Word to insert references into Word, but I found it awkward to use.

Since I’m using Jabref, I first compile the Scrivener manuscript as Latex, then run Latex2RTF on the latex files to get a Word file. I recently spent some time looking into other reference managers, but nothing else seems as easy, stable, or free as Jabref. While some of the other reference managers do have some nice features, I want to stay with a tool that integrates easily with Latex.

If Scrivener or MMD could integrate with reference managers (like Jabref) for citations, the Scrivener to Word workflow could potentially skip the conversion to latex. Wishful thinking!

Converting LaTeX to RTF/Word

Advertisements

9 thoughts on “Configuring Scrivener to output LaTeX

  1. Thanks. Very clearly written and it’s helping me understand how to do this.

    One question. What’s the advantage of using MMD for the citations? Why not simply write the citation markup in latex directly?

    Also, is there any way to backtrack? For example, if I make a few changes in the Latex document, do I need to also make them in Scrivener manually. In other words is there a way to “uncompile” to get those changes back into Scrivener?

    Like

  2. Thanks Tom!

    The advantage of MMD – at least with this workflow – is that (a) you can then export (“compile”) the manuscript in other formats like RTF, assuming they play nice with your citation manager and (b) you don’t have to use the latex escape sequence every time i.e. <–! \cite{citation} –>.

    However, I’ve finally converted to just writing the whole document in latex format directly (without the escape sequences) and then compiling as a plain text file – this bypasses MMD completely. I then compile the text file to PDF using my favorite Tex editor. The reason is that half-latex, half-MMD was just more trouble than it was worth, and since MMD wasn’t able to do everything I needed, I switched to all-latex without MMD. I’m still looking for a better way to do this (especially since I just had to convert a latex file to word format for a journal publication, and it was a bit of a nightmare even with a latex converter tool).

    I had hoped Pandoc would solve the MMD/latex/word conversion issues – just write everything in MMD and then compile to RTF, Latex, whatever your heart desires – but I had issues with getting figure and table numbering to work. That was last year, though, so perhaps they’ve improved it in the meantime.

    As for your second question – no, there’s no way to backtrack. You have to manually copy the edits back to the Scrivener file.

    Good luck!

    Liked by 2 people

  3. “(especially since I just had to convert a latex file to word format for a journal publication, and it was a bit of a nightmare even with a latex converter tool).”

    I also just went through a similar nightmare. Any new insights? Are you compiling from Scrivener to a Plain Text file? Or are you writing in your LaTeX compiler? I’m having a hard time following that aspect of your update posts. Really, really interested in find a handy way to output LaTeX or text for Word, etc. for co-authors.

    Liked by 1 person

    • Personally I just used Scrivener for a journal article. I put all the LaTeX and text into different documents, compiled the document into plain .txt format, and then opened it in TexWorks for creating a pdf.

      As for outputting in Word format for co-authors, this does not seem to be in the scope of a Scrivener to Latex pipeline. pdf into Word is notorious for causing typesetting issues. There are some partial solutions for this. Firstly you could convert pdf to docx using Adobe Acrobat or open a pdf in MS Word (in my experience Word won’t open a pdf as expected, though the process is getting a lot better). Alternately there are some methods for Latex to Word, such as http://grindeq.com/index.php?p=latex2word

      Finally you could just share pdf or Latex documents with coauthors. The main question I have is why would you want to use different formats than your coauthors? Either teach them Latex or everyone can use Word.

      Just thought I’d add my two cents, for what it’s worth. Not really sure the best methods but I know there are some out there. Anyone else got some ideas?

      Liked by 1 person

      • By this comment, “I put all the LaTeX and text into different documents”, I mean that I do it inside one Scrivener project. Because the Latex is separate from the text, I can create different Custom formats, one for with Latex and one for without. The main difficulties I found was that small inline Latex tags, such as \cite, can’t really go in their own documents. Basically, when making the plain text version, I didn’t include in the compile the LaTeX for the header, or tables and figures. But in the publication version, I did output those so it made a complete LaTeX document.

        Like

  4. Pingback: Scrivener, Bookends & LaTeX | Asiasuppe & Krautrock

  5. Pingback: Scrivener 로 LaTeX 문서 작성 소소한 팁 #1 – Fithele in WordPress

  6. I use LaTeX in Scrivener, export in plain text, then Sublime Text compiles it, and use a script for automating the process. I agree, MMD was a pain, since it does not work with BibLaTeX. To export to xdoc I upload the PDF to Adobe Acrobat DC, and it produces a nice editable copy for Word.

    Like

  7. Pingback: R / LaTeX 관련 문서 / 링크 모음 | Eka-research

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s