Reproducing Scrivener's built-in Multimarkdown -> LaTeX (.tex) compile process

User avatar
rdale
Posts: 2043
Joined: Tue Jul 14, 2015 1:07 pm
Platform: Mac, Win + iOS
Location: St. Louis, MO
Contact:

Thu Jan 23, 2020 4:09 pm Post

I'm interested in creating my own Pandoc compile setup for use with Latex (eventually to go straight to PDF), but had some issues setting it up with the latex template I'm using. I have a working Scrivener compile setup using a custom MMD to Latex format, complete with custom section layouts and styles that produce escaped latex commands (using back-ticks and {=latex} appended to each command).

So I wanted to transition a bit at a time to my own scripting, and the first stage I want to attempt is to use the straight MMD compile settings (no pandoc involved at this stage) to output through a script that can reproduce what Scrivener does when I choose MMD -> Latex as an output format.

Is that possible/easy? Is there a shell script somewhere in the Mac Scrivener bundle that it runs, or is there built-in Objective-C code that does the conversion?
FKA: robertdguthrie
AKA: R Dale Guthrie, Robert, Mr. Obscure, and "Oh, it's you again".

User avatar
nontroppo
Posts: 1246
Joined: Mon Mar 05, 2007 5:22 pm
Platform: Mac
Location: Airstrip One

Fri Jan 24, 2020 10:41 am Post

Scrivener uses mmd, and mmd offers a command line option and a wrapper to convert mmd to latex:

https://fletcher.github.io/MultiMarkdow ... index.html

Use mmd2tex as a convenience, or just multimarkdown -t latex. So if you have a very simple compile from Scrivener (compile.md),


Code: Select all

---
title: A test
author: Joanna Doe
---

# Introduction
Blah blah blah.


just run it through mmd on the commandline:

Code: Select all

➜ multimarkdown -t latex test.md
\def\mytitle{a test}
\def\myauthor{Joanna Doe}

\part{Introduction}
\label{introduction}

Blah blah blah.

\end{document}


This outputs to stdout, you can redirect it to a file:

Code: Select all

multimarkdown -t latex test.md > test.tex


Or use the wrapper

Code: Select all

mmd2tex test.md


Now if you want to make a complete document, you need to make sure you have the latex headers installed, so you should read the quickstart guide on how to do this:

https://github.com/fletcher/MultiMarkdo ... kStart.pdf

If you installed via homebrew, then mmd does not include these and you need to get them manually. Then you need to put them in a location that you can easily reference. The default location (/usr/local/share/texmf/tex/latex/mmd6/) at least for me does not get picked up by my MacTeX system by default. Once you move these where LaTeX can find them then you should be good to create a full document (see new metadata value used):

Code: Select all

---
title: A test
author: Joanna Doe
latex config: article
---

# Introduction
Blah blah blah.


Code: Select all

➜ multimarkdown -f -t latex test.md
\input{mmd6-article-leader}
\def\mytitle{A test}
\def\myauthor{Joanna Doe}
\input{mmd6-article-begin}

\part{Introduction}
\label{introduction}

Blah blah blah.

\input{mmd6-article-footer}
\end{document}


---------
Pandoc is much simpler in this regard as it contains a default latex template it uses to generate a complete document, no setup needed:

Code: Select all

➜ pandoc -s -t latex test.md
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={A test},
  pdfauthor={Joanna Doe},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering

\title{A test}
\author{Joanna Doe}
\date{}

\begin{document}
\maketitle

\hypertarget{introduction}{%
\section{Introduction}\label{introduction}}

Blah blah blah.

\end{document}

User avatar
rdale
Posts: 2043
Joined: Tue Jul 14, 2015 1:07 pm
Platform: Mac, Win + iOS
Location: St. Louis, MO
Contact:

Thu Feb 06, 2020 3:19 pm Post

Thank you! I should have delved past the "MMD syntax" pages of the MMD documentation. Thanks for laying it all out for me like that, it'll save me a lot of time in experimenting with options.
FKA: robertdguthrie
AKA: R Dale Guthrie, Robert, Mr. Obscure, and "Oh, it's you again".