How to get started using LaTeX in Scrivener

I wanted to try using LaTeX in Scrivener. I loaded the TEX template but don’t really know how I would use this. I tried pasting some LaTeX code in Scrivener, but it has all the markup, and compiles a PDF with the markup listed. Here is a simple tex document that I was trying out, but I can’t figure out a way of editing it in Scrivener:

\documentclass[10pt]{scrartcl}

\usepackage[inner=.5in, outer=1in, top=.75in, bottom=.75in]{geometry}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage{fontspec,xltxtra,xunicode}
\defaultfontfeatures{Mapping=tex-text}
\setromanfont[Mapping=tex-text]{DTL Valiance TPRO}
\setsansfont[Scale=MatchLowercase,Mapping=tex-text]{DTL Caspari TOT Medium}
\setmonofont[Scale=MatchLowercase]{Andale Mono}

\usepackage{commath}

\raggedbottom
\title{Proof of Schwartz Inequality}
\date{\vspace{-5ex}}
\begin{document}

\maketitle

Prove $$x_1y_1+x_2y_2\leq\sqrt{x_1^2+x_2^2}\sqrt{y_1^2+y_2^2}$$\

It is helpful to assume it is true and then work backwards:

\begin{align*}
x_1y_1+x_2y_2 &\overset{?}{\leq}\sqrt{(x_1^2+x_2^2)}\sqrt{y_1^2+y_2^2}\
(x_1y_1+x_2y_2)^2 &\overset{?}{\leq}(x_1^2+x_2^2)(y_1^2+y_2^2)\
(x_1y_1+x_2y_2)^2 &\overset{?}{\leq}x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2\
x_1^2y_1^2+x_2^2y_2^2+2x_1x_2y_1y_2 &\overset{?}{\leq} x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2\
2x_1x_2y_1y_2 &\overset{?}{\leq} x_1^2y_2^2+x_2^2y_1^2\
0 &\overset{?}{\leq} x_1^2y_2^2+x_2^2y_1^2-2x_1x_2y_1y_2 \
0 &\overset{?}{\leq} (x_1y_2)^2-2x_1x_2y_1y_2+(x_2y_1)^2 \
0 &\overset{?}{\leq} (x_1y_2-x_2y_1)^2 \
\end{align*}

The last statement is true, since all squares are $\ge$ 0\

So to prove this

\begin{align*}
0 &\leq (x_1y_2-x_2y_1)^2 \
0 &\leq (x_1y_2)^2-2x_1x_2y_1y_2+(x_2y_1)^2 \
0 &\leq x_1^2y_2^2+x_2^2y_1^2-2x_1x_2y_1y_2 \
2x_1x_2y_1y_2 &\leq x_1^2y_2^2+x_2^2y_1^2\
x_1^2y_1^2+x_2^2y_2^2+2x_1x_2y_1y_2 &\leq x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2\
(x_1y_1+x_2y_2)^2 &\leq x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2\
(x_1y_1+x_2y_2)^2 &\leq(x_1^2+x_2^2)(y_1^2+y_2^2)\
x_1y_1+x_2y_2 &\leq\sqrt{x_1^2+x_2^2}\sqrt{y_1^2+y_2^2}\
\end{align*}

Proof of general form $\sum\limits_{i=1}^n x_iy_i \leq \sqrt{\sum\limits_{i=1}^n x_i^2} \sqrt{\sum\limits_{i=1}^n y_i^2} $\[2em]
Start with the general form of the first proof and work backwards:

\begin{align*}
\sum\limits_{i=1}^n\sum\limits_{j=1}^n (x_iy_j-x_jy_i)^2 &= \sum\limits_{i=1}^n x_i^2 \sum\limits_{j=1}^n y_j^2+\sum\limits_{i=1}^n y_i^2 \sum\limits_{j=1}^n x_j^2-2\sum\limits_{i=1}^n x_iy_i\sum\limits_{j=1}^n x_j y_j\
\sum\limits_{i=1}^n\sum\limits_{j=1}^n (x_iy_j-x_jy_i)^2 &= 2\biggl(\sum\limits_{i=1}^n x_i^2\biggr)\biggl(\sum\limits_{i=1}^n y_i^2\biggr)-2\biggl(\sum\limits_{i=1}^n x_i y_i\biggr)^2\
0&\leq 2\biggl(\sum\limits_{i=1}^n x_i^2\biggr)\biggl(\sum\limits_{i=1}^n y_i^2\biggr)-2\biggl(\sum\limits_{i=1}^n x_i y_i\biggr)^2\
0&\leq \biggl(\sum\limits_{i=1}^n x_i^2\biggr)\biggl(\sum\limits_{i=1}^n y_i^2\biggr)-\biggl(\sum\limits_{i=1}^n x_i y_i\biggr)^2\
\biggl(\sum\limits_{i=1}^n x_i y_i\biggr)^2&\leq \sum\limits_{i=1}^n x_i^2\sum\limits_{i=1}^n y_i^2\
\sum\limits_{i=1}^n x_iy_i &\leq \sqrt{\sum\limits_{i=1}^n x_i^2} \sqrt{\sum\limits_{i=1}^n y_i^2}
\end{align*}

\end{document}

There is no “TEX” template provided by Scrivener. Check with whoever gave you that template as to how to use it. And you do not edit a *.tex document in Scrivener. You use Scrivener to create, write, edit, and then compile a .*tex file that you then use a LaTeX tool to make a document.

Create a new document based on the “General Non-Fiction (LaTeX)” template provided by Scrivener. at the top of the Binder in the new project, just above the “Draft” item, is a document with full instructions.

Repeating what I told you earlier today:

There also is mention of LaTeX in the “Scrivener User Manual”, but I’ve not used other than cursory looking. Instructions in the Template are best to get started, and then I just looked at the Text Settings in the template editing dialog boxes, and made my own LaTeX settings in a file compiled as part of “FrontMatter”.

It’s all in your control. No magic.

1 Like

Thank you. A quick trial of using TEX in Scrivener for the above tex file:

  • Scrivener likes $$ but not $ around formulas

  • It does not like the align function
    The beginning of what works looks a bit like:

Prove $$x_1y_1+x_2y_2\leq\sqrt{x_1^2+x_2^2}\sqrt{y_1^2+y_2^2}$$\

It is helpful to assume it is true and then work backwards:

$$x_1y_1+x_2y_2 }{\leq}\sqrt{(x_1^2+x_2^2)}\sqrt{y_1^2+y_2^2}$$
$$(x_1y_1+x_2y_2)^2 {\leq}(x_1^2+x_2^2)(y_1^2+y_2^2)$$
$$(x_1y_1+x_2y_2)^2 {\leq}x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2$$
$$x_1^2y_1^2+x_2^2y_2^2+2x_1x_2y_1y_2 {\leq} x_1^2y_1^2+x_2^2y_2^2+x_1^2y_2^2+x_2^2y_1^2$$
$$2x_1x_2y_1y_2 {\leq} x_1^2y_2^2+x_2^2y_1^2$$
$$0 {\leq} x_1^2y_2^2+x_2^2y_1^2-2x_1x_2y_1y_2$$
$$0 {\leq} (x_1y_2)^2-2x_1x_2y_1y_2+(x_2y_1)^2$$
$$0 {\leq} (x_1y_2-x_2y_1)^2$$

What is “texmaker”?

Google is your friend.

Re the other points you raise, i have no clue about what you report does not work. I never experienced anything not working as documented by Scrivener nor much trouble with the published docs about LaTeX packages in use by Scrivener or I located and used. I trust you are following the instructions.

1 Like

Admin: could you please move this post to the LaTeX/markdown forum? Thanks.

No worries, done.

I would also point you to a few existing threads, to familiarise yourself with the concept of using Scrivener to generate technical formats. The idea is to generate the file itself, so that it can be processed in the correct engine (xelatex or whatever). Scrivener can automate that for you, as you will discover in some of these threads. You can tell LaTeX to typeset the .tex file that Scrivener helps you generate, so that you get a .pdf instead.

What we are looking at here is changing our perspective of what Scrivener is, from a powerful writing tool that has a rather basic text editor with a few word processing functions, to a powerful writing tool that is used to assemble plain-text formats. That could be LaTeX, that could be DITA, DocBook, PrinceXML or any number of plain-text oriented markup formats. The template you are playing with is, although of practical use to many, a tech demo of this concept.

Another way of thinking about it: Scrivener is acting more like Visual Studio Code here: it can help you create the material you will typeset in xelatex—only instead of a GUI built for programmers, it is a GUI built for writers—and with its comprehensive engine for exporting (the compiler) we can negate how much manual markup we use to a great degree—to the point that some might even write in Scrivener as though they were using a word processor, with barely ever having to use raw LaTeX syntax.

But you can, and that’s where it gets powerful. If Scrivener’s footnote feature is too simple to stack footnotes together adjacent to one another, then you can just type in the \footnote{...} stuff yourself right in the editor, while otherwise depending on Scrivener’s ability to convert its footnotes to that same syntax, for more basic cases elsewhere.

Further Reading

2 Likes

Thank you very much!

Hi IsleofGough,

Something tells me you are well beyond the point of beginning to work with Scrivener+LaTeX. If so, my apologies.

If that is the case, I though it may be useful for others thinking of working with Scrivener+LaTeX to mention a helpful template that I utilized when I started, e.g. the General Non-Fiction (LaTeX) template. The template has been mentioned in other postings here on L&L. The template appears by selecting File > New Project... > Non-Fiction within Scrivener.

For myself, I am well into my third year of a multi-year project. I imagine this will take at least another 3-5 years (assuming I’m still here) to get anywhere near a complete document.

The extensive Front Matter and Back Matter that took me years to develop has paid dividends that I could not have imagined when I started, mostly in the form of a couple hundred custom Scrivener Styles that I use almost every minute that I write.

Scrivener is so flexible and powerful that it has allowed me to create, what I’ve been told are, Styles that were not meant to be used the way that I am using them. Yet, it would not have been possible for me to create my document as it is without those Styles. With Scrivener+LaTeX, I’m able to code Styles that invoke powerful and complex LaTeX macros with just a few clicks.

I hope you are able to take advantage of the power and flexibility that is available from using Scrivener+LaTeX.

Enjoy!

HTH,
scrive
:thinking:

2 Likes

I am actually very new to Scrivener and only tried the template you mentioned yesterday. But I’m not sure I will end up using Scrivener for Latex, as I am pretty quick writing LaTeX directly in texifier, and it’s outline feature for sections and subsections meet my needs. I write DITA or Docbook in OxygenXML and it is a very productive environment. For myself I see Scrivener mostly as a formatted text and file outliner to deliver to some other program for the end result. But I will play around with it for awhile and see how helpful it is. I don’t write in Word, so it could be a good alternative.

1 Like

I think for those comfortable writing LaTeX markup directly then that is OK. Nevertheless the superpower of Scrivener is that you hook up Section Types, Paragraph and Character styles, and regex rules to “inject” the LaTeX markup for you at compile time. This is a significant advantage in that you are just writing text, and the semantic markup now depends on translation by the compiler, not anything in the editor.

Everyone has different priorities but this is a big advantage workflow wise, especially if your writing may also need to output to EPub or DOCX or other formats. Even for sticking with LaTeX, it means your text remains clean and not “polluted” by formatting instructions intended for a layout engine. This is an ideal of the separation-of-concerns for documents workflow…

Going this route, another IMO more flexible route is to use Scrivener > markdown > LaTeX/DOCX/HTML/EPub/etc. — in this case markdown allows for many transformational tools and filters, improving the same-source motif. Pandoc allows powerful filters to work on the semantic structured outlines of your document, and templates (for both markup-based and binary formats) can combine metadata and text in many ways. One (semantically pure) source ⇨ many outputs in a flexible manner.

3 Likes

Hi nontroppo and IsleofGough,

After reading about the various renditions of how we are implementing Scrivener with all the various LaTeX/markdown/XML/texmaker/xelatex/texifier/EPub/HTML/DOCX/etc./etc … languages and packages, I realized that at least one, of many, powerful genius capabilities with Scrivener is the ability to Roll-Your-Own-Interface (RYOI) to suit whatever background and knowledge base the writer is coming from, and then create additional unique features on top of that interface …

Took me a while to visualize that … seems I’m a bit late to the party!

Thanks for helping me recognize what is likely obvious to others …
scrive
:thinking:

I’m glad it works for you @scriv. I am very new to Scrivener but despite a little testing, I’m having problems with tables being converted correctly to latex. Unfortunately, at least for my work, pandoc hasn’t worked very well with conversions.

The instructions by Scrivener that I have pointed you to says:

Tables

Scrivener cannot convert rich text tables into LaTeX code for you. You will need to provide the proper syntax yourself, however you can focus on the tabular data itself, leaving the boilerplate text to Scrivener by making use of the “Table” Section Type when applied to individual binder items. As with binder figures, you can supply a caption as metadata. A document template has been provided as an example starting point. Working this way means you can make global adjustments to table appearance more easily, by editing the “Table w/ Caption” section layout, in the compile format settings.

Which of the above specific instructions do you find does not work?

I used the LaTeX document template in Scrivener. The problem for me, at least, is that if I write directly in one of the LaTeX systems, like Texifier, I can see instantly any issues or coding problems by clicking the compile option. Writing in non tex applications might save me time on carriage returns, but I’m not seeing how it simplifies most of the coding that I would want to call up in LaTeX.

My experience with a lot of non Scrivener programs is that tex and XML or rtf don’t talk well with each other. It is very difficult to single source to tex and other targets. With XHTML, it is pretty easy to single source to a website or ePub and also create a pdf target, as CSS has almost everything as an easy translation to paragraph and character styles (<p.something> and <span.somethingelse>). Table styles and rotated table cells are the only things that generally does not translate well. However, tex is quite a different language and has a lot of things that don’t have a non tex equivalent (like chess notation).

This entirely depends on what features of TeX you are using. It certainly will be the case that the way that TeX specifies some specific feature is not how HTML does, and the potential feature mapping space is vast. For most basic features the mappings are fine, yet it is inevitable that not every possible bridge has been constructed. NEVERTHLESS there are a whole bunch of places we can build ad-hoc bridges for many use-cases:

  • Scrivener replacements — regex rules that are stored in your compile format. This means you can build different replacements for different compile targets.
  • Compiler Section Types / Styles — again places we can selectively inject transformations and wrapping markup.
  • Compiler Section filters — Scrivener can filter different section layouts in the compiler, so you can build sections that are only included for HTML compile for example.
  • Preprocessing scripts — Scrivener can call a script or program after it has translated into markdown BUT before the markdown is translated into HTML/TeX/PDF etc.
  • AST filters — Pandoc at least semantically represents the contents and filters can easily target very specific elements in a way that brute-force regex cannot. You can do a lot, including specific rebuilding of tables as long as a script is available or you can code it yourself.
  • RAW Markup — MMD and Pandoc can “label” raw chunks of markup. So for example you can insert a LaTeX table AND a HTML table when compiling, and they will filter out this to only keep TeX chunks for LaTeX and HTML chunks for HTML/EPub etc.
  • Templates — you can specify particular structures / packages / CSS / JS or what have you and easily choose which will be used to build the target format.
  • Post-processing scripts — after the conversion but before any binary output is generated.

This is the major benefit of a markup-based workflow (some of these apply to anything like XML / asciidoc / generic markdown, but others critically depend on a Scrivener + Pandoc pair). You can step in and tweak as content trundles along all these feature bridges.

Yes, you are slightly closer to the “metal”, which you may consider either a benefit or a distraction to your actual writing of content. But remember Scrivener’s compiler can trigger a full compile to a PDF (you can split screen scrivener and your PDF viewer, compile and refresh). You can also make use of great tools like Marked2 which allows you to see TeX maths and many other features rendered “live” as you write via a markdown intermediate.

In summary, if you do want a single-source there are a bunch of features that help you do most of what you need. Using something like filtered section types or RAW markup filtering is still IMO better than having two separate documents in two different authoring environments. Some of the tools do require some technical expertise (like writing Lua / Ruby filters) but again this is the case for many formats (I personally find Lua or Ruby far more elegant to understand than TeX code). It would be great if there was a tool that could do it all without any user intervention, but this tool doesn’t, and probably cannot, exist.

6 Likes

I’ve written in Markdown in Scrivener and exported to LaTeX; the Pandoc option when compiling works pretty well. But if you write in LaTeX code, i.e.

\Section{Here is a Section}

and then compile to plain text, change the extension to *.tex, you can then run that file through whatever TeX engine you’d like; on Mac TeXshop comes with the MacTeX distribution. Or you can process your file at the command line:

pdflatex myfile.tex

Read More

Except with respect to multiple lists, I’ve had pretty good luck with Scrivener exporting from Markdown => LaTeX. Part of the problem may be that I only know basic Markdown and not the MultiMarkdown flavor.

2 Likes

@IsleofGough

The geometry package may conflict with the Koma-classes; you can set the margins in the document class options. The default paper size for those classes is A4, so if you want something else you have to so specify.

This line will help get you a proper text block:

 \KOMAoptions{DIV=calc}

The KOMA manual is well-done and treats important typographical concepts; it’s worth a look if you don’t have experience in the area.

2 Likes

Thank you. I’ll look into that.

Hi IsleofGough,

My apologies for again coming so late to this thread, but there is an online resource that I did not mention in my last share back in November '22. I have come to rely on the online resource to create LaTeX tables that, with a minimum of cosmetic adjustments, I have learned to use directly in my Scrivener+LaTeX project.

As an online tool, unfortunately, there is a BIG caveat. Whatever is typed into the online tool, is presumably also available to whomever is behind the website, so using the site to arrange proprietary data or information may be a BIG no-go. (As a Little Snitch user, I often wonder who is really behind any web site these days. That could mean anyone or anything in my mind. User beware!)

(FWIIW, the only public data that I can see is available at who.is as to who owns or controls the Tables Generator website is that the site is registered to an entity in Reykjavik, Iceland.)

Given the above caveat, I’ve held my proverbial nose on several occasions, and took the plunge into the Tables Generator website to design my LaTeX tables. I have designed and re-designed many tables that I have incorporated into my project on several occasions. The web site also allows for the design of tables in HTML, Text, Markdown, and MediaWiki, though I’ve never used any of those options.

One nice feature is that the site allows for the saving, by export, as well as the re-import, of tables that are created on the Tables Generator website. I’ve used the export and re-import feature several times to embellish what I created on prior occasions.

Over time, I’ve learned that if, in the future, I am likely to make changes to whatever table I create within Tables Generator, it is generally best if I minimize any changes to said table when I incorporate the table within my Scrivener+LaTeX project. Instead, I’ll default to re-importing a saved copy of the table back into Tables Generator, make whatever changes are needed, then re-export the revised label to LaTeX to then be incorporated back into my project.

HTH,
scrive
:thinking:

2 Likes

If you’re concerned about safety, why not create the table using lorem ipsum placeholder text, or just numbers for the contents. When you export the table to LaTeX, substitute the placeholder text or number with the real text you want to display in your document.

The Russian spies who were kicked out of Ireland will have a hard time reverse engineering a table populated with only numbers to the GPS coordinates of the Syrian Captagon manufacturing facilities.