From d410554f46202b1a47ece7d2fc9d111dcbfd32cc Mon Sep 17 00:00:00 2001
From: Andrew Dunning <adunning@users.noreply.github.com>
Date: Tue, 1 Jan 2019 14:37:42 -0500
Subject: [PATCH 1/9] LaTeX Template: Use Babel for LuaTeX

There are a number of bugs in Polyglossia under LuaLaTeX with common languages, e.g. <https://github.com/reutenauer/polyglossia/issues/182>.
---
 data/templates/default.latex | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 6e8fda613..6856f2cbe 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -286,12 +286,12 @@ $for(header-includes)$
 $header-includes$
 $endfor$
 $if(lang)$
-\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
+\ifnum 0\ifxetex 1\fi=0 % if pdftex or luatex
   \usepackage[shorthands=off,$for(babel-otherlangs)$$babel-otherlangs$,$endfor$main=$babel-lang$]{babel}
 $if(babel-newcommands)$
   $babel-newcommands$
 $endif$
-\else
+\else % if xetex
   % load polyglossia as late as possible as it *could* call bidi if RTL lang (e.g. Hebrew or Arabic)
   \usepackage{polyglossia}
   \setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$}

From 275cedc59c4237b3aeb1ce986ad6ebf429a79daf Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 14:45:20 -0500
Subject: [PATCH 2/9] LaTeX template: Load xcolor if available

The `xcolor` package must be loaded before the `footnote` package, which we load to fix foonotes in tables. Closes #4861.
---
 data/templates/default.latex | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 6856f2cbe..4f85f3733 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -173,9 +173,7 @@ $endif$
 $if(verbatim-in-note)$
 \usepackage{fancyvrb}
 $endif$
-$if(colorlinks)$
-\usepackage{xcolor}
-$endif$
+\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
 \usepackage{hyperref}
 \hypersetup{
 $if(title-meta)$

From b6d13467f6efe0abd09eef6bba7f06138ac9e9ed Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 14:47:20 -0500
Subject: [PATCH 3/9] LaTeX template: Load xurl if available

This breaks URLs in more locations. Only available with TeX Live 2018 and later.
---
 data/templates/default.latex | 1 +
 1 file changed, 1 insertion(+)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 4f85f3733..d396901e5 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -174,6 +174,7 @@ $if(verbatim-in-note)$
 \usepackage{fancyvrb}
 $endif$
 \IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \usepackage{hyperref}
 \hypersetup{
 $if(title-meta)$

From 316083698379ab7a9d14c4010e8c212e89cf720f Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 14:57:19 -0500
Subject: [PATCH 4/9] LaTeX template: Remove obsolete fixltx2e package

---
 data/templates/default.latex | 1 -
 1 file changed, 1 deletion(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index d396901e5..c88339429 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -70,7 +70,6 @@ $if(linestretch)$
 $endif$
 \usepackage{amssymb,amsmath}
 \usepackage{ifxetex,ifluatex}
-\usepackage{fixltx2e} % provides \textsubscript
 \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
   \usepackage[$if(fontenc)$$fontenc$$else$T1$endif$]{fontenc}
   \usepackage[utf8]{inputenc}

From 6a71997fcbdb74c9a515646d9022a6ee4b42bcd5 Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 14:59:19 -0500
Subject: [PATCH 5/9] LaTeX template: Reindent

Use two spaces to be consistent internally and with other templates.
---
 data/templates/default.latex | 117 +++++++++++++++++++++--------------
 1 file changed, 71 insertions(+), 46 deletions(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index c88339429..80c5a2dc5 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -2,14 +2,40 @@
 \PassOptionsToPackage{hyphens}{url}
 $if(colorlinks)$
 \PassOptionsToPackage{dvipsnames,svgnames*,x11names*}{xcolor}
-$endif$$if(dir)$$if(latex-dir-rtl)$
+$endif$
+$if(dir)$
+$if(latex-dir-rtl)$
 \PassOptionsToPackage{RTLdocument}{bidi}
-$endif$$endif$%
-\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(papersize)$$papersize$paper,$endif$$if(beamer)$ignorenonframetext,$if(handout)$handout,$endif$$if(aspectratio)$aspectratio=$aspectratio$,$endif$$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}
+$endif$
+$endif$
+%
+\documentclass[
+$if(fontsize)$
+  $fontsize$,
+$endif$
+$if(lang)$
+  $babel-lang$,
+$endif$
+$if(papersize)$
+  $papersize$paper,
+$endif$
+$if(beamer)$
+  ignorenonframetext,
+$if(handout)$
+  handout,
+$endif$
+$if(aspectratio)$
+  aspectratio=$aspectratio$,
+$endif$
+$endif$
+$for(classoption)$
+  $classoption$$sep$,
+$endfor$
+]{$documentclass$}
 $if(beamer)$
 $if(background-image)$
 \usebackgroundtemplate{%
-\includegraphics[width=\paperwidth]{$background-image$}%
+  \includegraphics[width=\paperwidth]{$background-image$}%
 }
 $endif$
 \usepackage{pgfpages}
@@ -25,22 +51,22 @@ $endfor$
 \raggedbottom
 $if(section-titles)$
 \setbeamertemplate{part page}{
-\centering
-\begin{beamercolorbox}[sep=16pt,center]{part title}
-  \usebeamerfont{part title}\insertpart\par
-\end{beamercolorbox}
+  \centering
+  \begin{beamercolorbox}[sep=16pt,center]{part title}
+    \usebeamerfont{part title}\insertpart\par
+  \end{beamercolorbox}
 }
 \setbeamertemplate{section page}{
-\centering
-\begin{beamercolorbox}[sep=12pt,center]{part title}
-  \usebeamerfont{section title}\insertsection\par
-\end{beamercolorbox}
+  \centering
+  \begin{beamercolorbox}[sep=12pt,center]{part title}
+    \usebeamerfont{section title}\insertsection\par
+  \end{beamercolorbox}
 }
 \setbeamertemplate{subsection page}{
-\centering
-\begin{beamercolorbox}[sep=8pt,center]{part title}
-  \usebeamerfont{subsection title}\insertsubsection\par
-\end{beamercolorbox}
+  \centering
+  \begin{beamercolorbox}[sep=8pt,center]{part title}
+    \usebeamerfont{subsection title}\insertsubsection\par
+  \end{beamercolorbox}
 }
 \AtBeginPart{
   \frame{\partpage}
@@ -89,16 +115,16 @@ $for(fontfamilies)$
   \newfontfamily{$fontfamilies.name$}[$fontfamilies.options$]{$fontfamilies.font$}
 $endfor$
 $if(mainfont)$
-    \setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$}
+  \setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$}
 $endif$
 $if(romanfont)$
-	\setromanfont[$for(romanfontoptions)$$romanfontoptions$$sep$,$endfor$]{$romanfont$}
+  \setromanfont[$for(romanfontoptions)$$romanfontoptions$$sep$,$endfor$]{$romanfont$}
 $endif$
 $if(sansfont)$
-    \setsansfont[$for(sansfontoptions)$$sansfontoptions$$sep$,$endfor$]{$sansfont$}
+  \setsansfont[$for(sansfontoptions)$$sansfontoptions$$sep$,$endfor$]{$sansfont$}
 $endif$
 $if(monofont)$
-    \setmonofont[Mapping=tex-ansi$if(monofontoptions)$,$for(monofontoptions)$$monofontoptions$$sep$,$endfor$$endif$]{$monofont$}
+  \setmonofont[Mapping=tex-ansi$if(monofontoptions)$,$for(monofontoptions)$$monofontoptions$$sep$,$endfor$$endif$]{$monofont$}
 $endif$
 $if(mathfont)$
 $if(mathspec)$
@@ -151,21 +177,20 @@ $endif$
 $endif$
 % use upquote if available, for straight quotes in verbatim environments
 \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
-% use microtype if available
-\IfFileExists{microtype.sty}{%
-\usepackage[$for(microtypeoptions)$$microtypeoptions$$sep$,$endfor$]{microtype}
-\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
+\IfFileExists{microtype.sty}{% use microtype if available
+  \usepackage[$for(microtypeoptions)$$microtypeoptions$$sep$,$endfor$]{microtype}
+  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
 }{}
 $if(indent)$
 $else$
 \makeatletter
-\@ifundefined{KOMAClassName}{%
+\@ifundefined{KOMAClassName}{% if non-KOMA class
   \IfFileExists{parskip.sty}{%
     \usepackage{parskip}
   }{% else
     \setlength{\parindent}{0pt}
     \setlength{\parskip}{6pt plus 2pt minus 1pt}}
-}{% else
+}{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
 $endif$
@@ -177,24 +202,24 @@ $endif$
 \usepackage{hyperref}
 \hypersetup{
 $if(title-meta)$
-            pdftitle={$title-meta$},
+  pdftitle={$title-meta$},
 $endif$
 $if(author-meta)$
-            pdfauthor={$author-meta$},
+  pdfauthor={$author-meta$},
 $endif$
 $if(keywords)$
-            pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$},
+  pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$},
 $endif$
 $if(colorlinks)$
-            colorlinks=true,
-            linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
-            filecolor=$if(filecolor)$$filecolor$$else$Maroon$endif$,
-            citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
-            urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
+  colorlinks=true,
+  linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
+  filecolor=$if(filecolor)$$filecolor$$else$Maroon$endif$,
+  citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
+  urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
 $else$
-            pdfborder={0 0 0},
+  pdfborder={0 0 0},
 $endif$
-            breaklinks=true}
+  breaklinks=true}
 \urlstyle{same}  % don't use monospace font for urls
 $if(verbatim-in-note)$
 \VerbatimFootnotes % allows verbatim text in footnotes
@@ -262,12 +287,12 @@ $if(subparagraph)$
 $else$
 % Redefines (sub)paragraphs to behave more like sections
 \ifx\paragraph\undefined\else
-\let\oldparagraph\paragraph
-\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
+  \let\oldparagraph\paragraph
+  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
 \fi
 \ifx\subparagraph\undefined\else
-\let\oldsubparagraph\subparagraph
-\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
+  \let\oldsubparagraph\subparagraph
+  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
 \fi
 $endif$
 $endif$
@@ -371,9 +396,9 @@ $endif$
 $if(beamer)$
 \begin{frame}
 $if(toc-title)$
-\frametitle{$toc-title$}
+  \frametitle{$toc-title$}
 $endif$
-\tableofcontents[hideallsubsections]
+  \tableofcontents[hideallsubsections]
 \end{frame}
 $else$
 {
@@ -404,9 +429,9 @@ $endif$
 $endif$
 $if(beamer)$
 \begin{frame}[allowframebreaks]{$biblio-title$}
-\bibliographytrue
+  \bibliographytrue
 $endif$
-\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
+  \bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
 $if(beamer)$
 \end{frame}
 $endif$
@@ -416,8 +441,8 @@ $endif$
 $if(biblatex)$
 $if(beamer)$
 \begin{frame}[allowframebreaks]{$biblio-title$}
-\bibliographytrue
-\printbibliography[heading=none]
+  \bibliographytrue
+  \printbibliography[heading=none]
 \end{frame}
 $else$
 \printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

From 8feda269b0c8af4449d5201b6b117e30bf1c8f86 Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 17:31:39 -0500
Subject: [PATCH 6/9] LaTeX template: Use `bookmark` if available

The `bookmark` package can sometimes correct the levels of headings where `hyperref` cannot: see <https://komascript.de/release3.26>.
---
 data/templates/default.latex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 80c5a2dc5..8193b428c 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -199,7 +199,7 @@ $if(verbatim-in-note)$
 $endif$
 \IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
-\usepackage{hyperref}
+\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
 $if(title-meta)$
   pdftitle={$title-meta$},

From 2c0af6e3a3392aad743cb523303a5e9e111c5f61 Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 17:47:02 -0500
Subject: [PATCH 7/9] Update LaTeX tests

---
 test/lhs-test.latex             | 31 ++++++++++++++--------------
 test/lhs-test.latex+lhs         | 31 ++++++++++++++--------------
 test/writer.latex               | 35 ++++++++++++++++----------------
 test/writers-lang-and-dir.latex | 36 +++++++++++++++++----------------
 4 files changed, 69 insertions(+), 64 deletions(-)

diff --git a/test/lhs-test.latex b/test/lhs-test.latex
index 015a55255..5e266a082 100644
--- a/test/lhs-test.latex
+++ b/test/lhs-test.latex
@@ -1,11 +1,11 @@
 \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
 \PassOptionsToPackage{hyphens}{url}
 %
-\documentclass[]{article}
+\documentclass[
+]{article}
 \usepackage{lmodern}
 \usepackage{amssymb,amsmath}
 \usepackage{ifxetex,ifluatex}
-\usepackage{fixltx2e} % provides \textsubscript
 \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
   \usepackage[T1]{fontenc}
   \usepackage[utf8]{inputenc}
@@ -16,25 +16,26 @@
 \fi
 % use upquote if available, for straight quotes in verbatim environments
 \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
-% use microtype if available
-\IfFileExists{microtype.sty}{%
-\usepackage[]{microtype}
-\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
+\IfFileExists{microtype.sty}{% use microtype if available
+  \usepackage[]{microtype}
+  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
 }{}
 \makeatletter
-\@ifundefined{KOMAClassName}{%
+\@ifundefined{KOMAClassName}{% if non-KOMA class
   \IfFileExists{parskip.sty}{%
     \usepackage{parskip}
   }{% else
     \setlength{\parindent}{0pt}
     \setlength{\parskip}{6pt plus 2pt minus 1pt}}
-}{% else
+}{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\usepackage{hyperref}
+\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
+\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
-            pdfborder={0 0 0},
-            breaklinks=true}
+  pdfborder={0 0 0},
+  breaklinks=true}
 \urlstyle{same}  % don't use monospace font for urls
 \usepackage{color}
 \usepackage{fancyvrb}
@@ -80,12 +81,12 @@
 \setcounter{secnumdepth}{0}
 % Redefines (sub)paragraphs to behave more like sections
 \ifx\paragraph\undefined\else
-\let\oldparagraph\paragraph
-\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
+  \let\oldparagraph\paragraph
+  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
 \fi
 \ifx\subparagraph\undefined\else
-\let\oldsubparagraph\subparagraph
-\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
+  \let\oldsubparagraph\subparagraph
+  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
 \fi
 
 % set default figure placement to htbp
diff --git a/test/lhs-test.latex+lhs b/test/lhs-test.latex+lhs
index fc16ff95a..4b829e6c9 100644
--- a/test/lhs-test.latex+lhs
+++ b/test/lhs-test.latex+lhs
@@ -1,11 +1,11 @@
 \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
 \PassOptionsToPackage{hyphens}{url}
 %
-\documentclass[]{article}
+\documentclass[
+]{article}
 \usepackage{lmodern}
 \usepackage{amssymb,amsmath}
 \usepackage{ifxetex,ifluatex}
-\usepackage{fixltx2e} % provides \textsubscript
 \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
   \usepackage[T1]{fontenc}
   \usepackage[utf8]{inputenc}
@@ -16,25 +16,26 @@
 \fi
 % use upquote if available, for straight quotes in verbatim environments
 \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
-% use microtype if available
-\IfFileExists{microtype.sty}{%
-\usepackage[]{microtype}
-\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
+\IfFileExists{microtype.sty}{% use microtype if available
+  \usepackage[]{microtype}
+  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
 }{}
 \makeatletter
-\@ifundefined{KOMAClassName}{%
+\@ifundefined{KOMAClassName}{% if non-KOMA class
   \IfFileExists{parskip.sty}{%
     \usepackage{parskip}
   }{% else
     \setlength{\parindent}{0pt}
     \setlength{\parskip}{6pt plus 2pt minus 1pt}}
-}{% else
+}{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\usepackage{hyperref}
+\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
+\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
-            pdfborder={0 0 0},
-            breaklinks=true}
+  pdfborder={0 0 0},
+  breaklinks=true}
 \urlstyle{same}  % don't use monospace font for urls
 \usepackage{listings}
 \newcommand{\passthrough}[1]{#1}
@@ -45,12 +46,12 @@
 \setcounter{secnumdepth}{0}
 % Redefines (sub)paragraphs to behave more like sections
 \ifx\paragraph\undefined\else
-\let\oldparagraph\paragraph
-\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
+  \let\oldparagraph\paragraph
+  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
 \fi
 \ifx\subparagraph\undefined\else
-\let\oldsubparagraph\subparagraph
-\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
+  \let\oldsubparagraph\subparagraph
+  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
 \fi
 
 % set default figure placement to htbp
diff --git a/test/writer.latex b/test/writer.latex
index 46e9b7a38..901a496b6 100644
--- a/test/writer.latex
+++ b/test/writer.latex
@@ -1,11 +1,11 @@
 \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
 \PassOptionsToPackage{hyphens}{url}
 %
-\documentclass[]{article}
+\documentclass[
+]{article}
 \usepackage{lmodern}
 \usepackage{amssymb,amsmath}
 \usepackage{ifxetex,ifluatex}
-\usepackage{fixltx2e} % provides \textsubscript
 \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
   \usepackage[T1]{fontenc}
   \usepackage[utf8]{inputenc}
@@ -16,28 +16,29 @@
 \fi
 % use upquote if available, for straight quotes in verbatim environments
 \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
-% use microtype if available
-\IfFileExists{microtype.sty}{%
-\usepackage[]{microtype}
-\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
+\IfFileExists{microtype.sty}{% use microtype if available
+  \usepackage[]{microtype}
+  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
 }{}
 \makeatletter
-\@ifundefined{KOMAClassName}{%
+\@ifundefined{KOMAClassName}{% if non-KOMA class
   \IfFileExists{parskip.sty}{%
     \usepackage{parskip}
   }{% else
     \setlength{\parindent}{0pt}
     \setlength{\parskip}{6pt plus 2pt minus 1pt}}
-}{% else
+}{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
 \usepackage{fancyvrb}
-\usepackage{hyperref}
+\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
+\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
-            pdftitle={Pandoc Test Suite},
-            pdfauthor={John MacFarlane; Anonymous},
-            pdfborder={0 0 0},
-            breaklinks=true}
+  pdftitle={Pandoc Test Suite},
+  pdfauthor={John MacFarlane; Anonymous},
+  pdfborder={0 0 0},
+  breaklinks=true}
 \urlstyle{same}  % don't use monospace font for urls
 \VerbatimFootnotes % allows verbatim text in footnotes
 \usepackage{graphicx,grffile}
@@ -58,12 +59,12 @@
 \setcounter{secnumdepth}{0}
 % Redefines (sub)paragraphs to behave more like sections
 \ifx\paragraph\undefined\else
-\let\oldparagraph\paragraph
-\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
+  \let\oldparagraph\paragraph
+  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
 \fi
 \ifx\subparagraph\undefined\else
-\let\oldsubparagraph\subparagraph
-\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
+  \let\oldsubparagraph\subparagraph
+  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
 \fi
 
 % set default figure placement to htbp
diff --git a/test/writers-lang-and-dir.latex b/test/writers-lang-and-dir.latex
index de382641d..715026f01 100644
--- a/test/writers-lang-and-dir.latex
+++ b/test/writers-lang-and-dir.latex
@@ -1,11 +1,12 @@
 \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
 \PassOptionsToPackage{hyphens}{url}
 %
-\documentclass[english,]{article}
+\documentclass[
+  english,
+]{article}
 \usepackage{lmodern}
 \usepackage{amssymb,amsmath}
 \usepackage{ifxetex,ifluatex}
-\usepackage{fixltx2e} % provides \textsubscript
 \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
   \usepackage[T1]{fontenc}
   \usepackage[utf8]{inputenc}
@@ -16,25 +17,26 @@
 \fi
 % use upquote if available, for straight quotes in verbatim environments
 \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
-% use microtype if available
-\IfFileExists{microtype.sty}{%
-\usepackage[]{microtype}
-\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
+\IfFileExists{microtype.sty}{% use microtype if available
+  \usepackage[]{microtype}
+  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
 }{}
 \makeatletter
-\@ifundefined{KOMAClassName}{%
+\@ifundefined{KOMAClassName}{% if non-KOMA class
   \IfFileExists{parskip.sty}{%
     \usepackage{parskip}
   }{% else
     \setlength{\parindent}{0pt}
     \setlength{\parskip}{6pt plus 2pt minus 1pt}}
-}{% else
+}{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\usepackage{hyperref}
+\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
+\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
-            pdfborder={0 0 0},
-            breaklinks=true}
+  pdfborder={0 0 0},
+  breaklinks=true}
 \urlstyle{same}  % don't use monospace font for urls
 \setlength{\emergencystretch}{3em}  % prevent overfull lines
 \providecommand{\tightlist}{%
@@ -42,12 +44,12 @@
 \setcounter{secnumdepth}{0}
 % Redefines (sub)paragraphs to behave more like sections
 \ifx\paragraph\undefined\else
-\let\oldparagraph\paragraph
-\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
+  \let\oldparagraph\paragraph
+  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
 \fi
 \ifx\subparagraph\undefined\else
-\let\oldsubparagraph\subparagraph
-\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
+  \let\oldsubparagraph\subparagraph
+  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
 \fi
 
 % set default figure placement to htbp
@@ -55,7 +57,7 @@
 \def\fps@figure{htbp}
 \makeatother
 
-\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
+\ifnum 0\ifxetex 1\fi=0 % if pdftex or luatex
   \usepackage[shorthands=off,ngerman,british,nswissgerman,spanish,french,main=english]{babel}
   \newcommand{\textgerman}[2][]{\foreignlanguage{ngerman}{#2}}
   \newenvironment{german}[2][]{\begin{otherlanguage}{ngerman}}{\end{otherlanguage}}
@@ -66,7 +68,7 @@
   \AddBabelHook{spanish}{afterextras}{\renewcommand{\textspanish}[2][]{\foreignlanguage{spanish}{##2}}}
   \newcommand{\textfrench}[2][]{\foreignlanguage{french}{#2}}
   \newenvironment{french}[2][]{\begin{otherlanguage}{french}}{\end{otherlanguage}}
-\else
+\else % if xetex
   % load polyglossia as late as possible as it *could* call bidi if RTL lang (e.g. Hebrew or Arabic)
   \usepackage{polyglossia}
   \setmainlanguage[]{english}

From fc9ac7993a93e078b0c5fa8a2c9011e444393f87 Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Tue, 1 Jan 2019 19:23:52 -0500
Subject: [PATCH 8/9] LaTeX template: Supply missing for

Partially addresses #5179
---
 data/templates/default.latex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 8193b428c..003cb91a4 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -112,7 +112,7 @@ $else$
 $endif$
   \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
 $for(fontfamilies)$
-  \newfontfamily{$fontfamilies.name$}[$fontfamilies.options$]{$fontfamilies.font$}
+  \newfontfamily{$fontfamilies.name$}[$for(fontfamilies.options)$$fontfamilies.options$$sep$,$endfor$]{$fontfamilies.font$}
 $endfor$
 $if(mainfont)$
   \setmainfont[$for(mainfontoptions)$$mainfontoptions$$sep$,$endfor$]{$mainfont$}

From f3709ccba3b86146b5f3e135acd0521f92511c66 Mon Sep 17 00:00:00 2001
From: Andrew Dunning <andunning@gmail.com>
Date: Wed, 2 Jan 2019 22:34:17 -0500
Subject: [PATCH 9/9] LaTeX template: Require xcolor

---
 data/templates/default.latex    | 2 +-
 test/lhs-test.latex             | 2 +-
 test/lhs-test.latex+lhs         | 2 +-
 test/writer.latex               | 2 +-
 test/writers-lang-and-dir.latex | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/data/templates/default.latex b/data/templates/default.latex
index 003cb91a4..723e0091f 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -197,7 +197,7 @@ $endif$
 $if(verbatim-in-note)$
 \usepackage{fancyvrb}
 $endif$
-\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\usepackage{xcolor}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
diff --git a/test/lhs-test.latex b/test/lhs-test.latex
index 5e266a082..5584b2cfa 100644
--- a/test/lhs-test.latex
+++ b/test/lhs-test.latex
@@ -30,7 +30,7 @@
 }{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\usepackage{xcolor}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
diff --git a/test/lhs-test.latex+lhs b/test/lhs-test.latex+lhs
index 4b829e6c9..f22da75f3 100644
--- a/test/lhs-test.latex+lhs
+++ b/test/lhs-test.latex+lhs
@@ -30,7 +30,7 @@
 }{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\usepackage{xcolor}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
diff --git a/test/writer.latex b/test/writer.latex
index 901a496b6..f6b074099 100644
--- a/test/writer.latex
+++ b/test/writer.latex
@@ -31,7 +31,7 @@
   \KOMAoptions{parskip=half}}
 \makeatother
 \usepackage{fancyvrb}
-\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\usepackage{xcolor}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{
diff --git a/test/writers-lang-and-dir.latex b/test/writers-lang-and-dir.latex
index 715026f01..7b13594da 100644
--- a/test/writers-lang-and-dir.latex
+++ b/test/writers-lang-and-dir.latex
@@ -31,7 +31,7 @@
 }{% if KOMA class
   \KOMAoptions{parskip=half}}
 \makeatother
-\IfFileExists{xcolor.sty}{\usepackage{xcolor}}{}
+\usepackage{xcolor}
 \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
 \IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
 \hypersetup{