Inserting in-line Latex equations and compiling to .tex

I’m fairly new to Scrivener and experienced with Latex. I am using Scrivener to organize my graduate thesis writing. My current workflow is Scrivener → compile to .tex using a modified version of the “General Non-fiction (Latex)” template → compile to .pdf (outside of scrivener). I use this workflow so that I can type biblatex commands as I’m writing (ie \parencite{blahblahblah}) and then when compiling to PDF, attach my .bib bibliography file (generated by Zotero) and have the latex compiler replace these commands with the appropriate citation. (If there’s a way to simplify this process and do it within Scrivener, please let me know… :wink:)

Anyways, my question is about how to handle in-line math mode and special characters which Latex doesn’t like. I want to:

  1. Style in-line math (eg c_i = 54) as an “Inline Equation” and have the compiler add $…$ on either side of the equation
  2. Escape $s when used outside of a math situation (eg $7.5 billion) so that they print as literal strings and aren’t interpreted as entering math mode.

I have figured out how to do 1 successfully using the Compile Format Designer/Styles options. I have figured out how to do 2 using the Compile Format Designer/Replacements options. The problem seems to be that when I apply both 1 and 2, all $ characters, including the ones inserted during 1, get escaped, making inline equations totally wonky.

How would you recommend resolving this so that literal $s and inline equations both get compiled correctly? I’m sure it’s fixable, but I’m having a hard time figuring it out. Thank you for your help! :smile:

Welcome to the forum!

This sounds like something that @nontroppo should be able to help you with, especially citations and scholarly formatting. In the meantime, you can search his name in the forums for previous posts. He created a wiki about citation options and has numerous Latex/Pandoc discussions.

I agree @nontroppo or @AmberV are the gurus, but I’d also recommend a stroll through the Markdown and LaTeX forum.

:slight_smile:
Mark

2 Likes

I’ve used that Scrivener LaTeX template very successfully–but not recently. Attached is the Replacements I use that seem to work. It’s been a long time since I wrote my last report (compiling to PDF via LaTeX) so I don’t think I can explain these replacements as right now my brain hurts looking at them. But perhaps of use to you?

2 Likes

If you are okay with writing a .bat file that automates doing that kind of repetitive stuff for you, then what you can do is go into the Processing pane of the Format Designer, while editing, and aim it at your .bat file. In this way, it would be pretty simple to make it so clicking ‘compile’ results in a PDF file, once you have the various files and such assembled in predictable places.

And for Mac users reading this: when you arrive in the Processing pane you’ll find an embedded shell script example that does a simple PDF typesetting routine, and all you have to do is turn the pane on to try it.

I have figured out how to do 1 successfully using the Compile Format Designer/Styles options. I have figured out how to do 2 using the Compile Format Designer/Replacements options. The problem seems to be that when I apply both 1 and 2, all $ characters, including the ones inserted during 1, get escaped, making inline equations totally wonky.

First order, just to ensure you are not making extra work for yourself, the LaTeX project template’s default compile Format actually comes with an “Inline Equation” example, and quite a few other examples as well that you might find of use. It might be easier to fork your modifications from that base rather than starting from scratch, if that is what you are doing. The part that makes it “Memoir” is actually pretty small, and found entirely within the Text Layout pane. It is intentionally extremely vanilla so that it can be used as a starting point for whatever you prefer or require.

All right, so as for escaping some things but not all things, this isn’t really a Scrivener trick so much as a macro-making trick in general, manual search and replace trick, or really any tool where you have a sequencer that executes changes one after the other, rather than a scripting language. You can’t say “if this is in there don’t do that, otherwise do this”, so you have to employ that kind of logic manually with the order in how things are done.

So first, make your Inline Equation prefix and suffix settings wrong, and wrong in a way that won’t be confused with anything else, like SCREQMARKER instead of $. Then, you will want the following two replacements in this order (though they don’t have to be together):

  1. Replace: $
    With: \$
    (No options.)
  2. Replace: SCREQMARKER
    With: $
    (You could add case-sensitive just to make sure that doesn’t accidentally change something else.)
Mac copy and paste...
<Replacements>
<Replacement>
    <Replace><![CDATA[$]]></Replace>
    <With><![CDATA[\$]]></With>
</Replacement>
<Replacement>
    <Replace><![CDATA[SCREQMARKER]]></Replace>
    <With><![CDATA[$]]></With>
</Replacement>
</Replacements>

The Inline Equations’ dollar signs don’t actually exist at the point when the Replacement that escaped dollar signs runs, they are only inserted after the second replacement runs.

The other approach is what rms posted above, and that is to use Regular Expressions (which do have a fair bit of logic to them, even though they do not understand their context, such as “I am running in a style prefix”). In the example case of the screenshot above, second row, the stipulation being made is that only dollar signs followed by a digit will be escaped.

Will that work for you? Maybe not, as some inline equations might start with a digit!

3 Likes

@rms-- thanks for posting those examples – I’ve been in the regexs pretty recently so they make sense to me :wink: they’re similar but slightly different from what I’ve been doing in a way that I think is useful.

@AmberV – thank you for writing out this extremely clear answer. Directing scrivener to a .bat file makes sense – I’ll set that up soon.

While, fortunately, I have been working from a template and using the in-built styles, I did forget about the sample PDF and haven’t looked as closely at it as I might have. There is a lot of useful material there…Two follow-up questions arise:

  1. relevant to my question about $ handling: I notice in the example that raw “\” double backslashes are replaced in the rendered document with a $.

Where is this replacement controlled? This behavior is unexpected to me based on what I noticed in the template settings.

  1. What compile settings should be used to replicate the sample PDF? When I tried to use the Book (Memoir) format and compile with MMD to Latex, the output was totally wonky:

Thank you for your help in clarifying these fiddly details!

1 Like

relevant to my question about $ handling: I notice in the example that raw “\” double backslashes are replaced in the rendered document with a $.

Oh, that’s a confusion I should fix with an updated copy of the template, not an actual result. You shouldn’t be seeing \\( turn into $ yourself when compiling the sample source. The text is newer/older than the PDF basically. I tried to use the more modern inline equation markers initially, but I don’t remember why I switched it back to the older dollar sign method—probably compatibility.

When I tried to use the Book (Memoir) format and compile with MMD to Latex, the output was totally wonky:

Yes, that would completely break everything. This template is not designed to be passed through Pandoc or MultiMarkdown (though Pandoc is more forgiving of inline LaTeX with the right settings, I’ve never tried passing this template through it). It generates a .tex file directly, and is intended more for those that prefer to write with LaTeX, or to work in a way where they are having Scrivener do that primary conversion (via styles, section layouts, etc.) rather than learning how to use a Markdown conversion engine instead.

Blockquote This template is not designed to be passed through Pandoc or MultiMarkdown (though Pandoc is more forgiving of inline LaTeX with the right settings, I’ve never tried passing this template through it

Ah, for some reason I missed that this template was also available through the .txt output. That’s the compile type I’ve been using, I don’t know why I thought it wasn’t the right thing to be using. :person_facepalming:

Thanks! I think I know what I need to know. I’m going to start back at the basic non-fiction template again and re-make the customizations I want; I think over the course of learning Scrivener’s functionality, I erased some helpful features from the original template. really appreciate the amount of design that’s gone into this software and how available+helpful support is.

2 Likes

Right, the LaTeX template is a beautiful example of the way you can work with Scrivener. The core points are to make use of styles (like the index terms, inline equations etc.) and section types (see the Templates). The front/back matter can be edited with different versions, and you can see how each piece of the puzzle adds to a flexible workflow. Try to minimise how much raw latex you manually add, and that ensures your project could be quickly converted from LaTeX to another format (editable formats like ODT or DOCX may be common for collaboration or comments from a supervisor).

I think you are well setup, but at least for others, don’t forget that MMD workflows can export to LaTeX and are even more flexible when needing to transfer work, publish online etc.

4 Likes