Including Latex code when compiling MMD --> Latex in Scrivener 3

In Scrivener 3, how does one include verbatim Latex code and pass it through the compile process of MMD → Latex? In the old Scrivener, I used to include code like:

However it looks like this is being entirely stripped out in the compile process in Scrivener 3 – it is just omitted from the .tex document.

Select the code and use Format → Preserve Formatting (give it a shortcut fore ease of use).



I get something like:


I just noticed that the code I used in my question above didn’t make it in. What used to work is:


Note sure if that clarifies the question

My apologies. I’m not actually a LaTeX user, and I thought that would pass it through as it does my MD codes. I hope AmberV, nontrppo or one of the other LaTeX gurus will be along to help you soon.

1 Like

Using HTML comments is an obsolete method at this point, MultiMarkdown hasn’t used that for some years.

The easiest answer, if you are using one of our build-in LaTeX oriented compile Formats, is to create a style called “Raw LaTeX” in your project, with whatever formatting you want, and style the verbatim text.

If you aren’t using one of those, then double-click on one of them (like “Article (Memoir)”) to duplicate and edit it, and check the Styles pane to see where this “magic” is coming from. If the compiler finds text marked as “Raw LaTeX” in the project, it will enclose the sequence in the necessary syntax to pass-thru the text verbatim.

So you can copy that convention into your own format—or this is more just a one-off thing that doesn’t need so much mechanism to it, just type in what you see in the prefix & suffix fields:


As you might guess, this new system is a lot more powerful since you can declare the output type that should use the pass-thru. If you compile a document with the above to HTML or ODT, it will be completely ignored in the output.

thanks, just to clarify, I need to create this style under Format → Style → New Style from selection? As opposed to something that I am doing in the compile window?

Precisely. What I do is mark the verbatim text bright red, and then use that menu command to create a character style called “Raw LaTeX”, and go through an apply it to anything else that needs it. Use whatever aesthetic you prefer though.

You don’t need to mess with compile settings unless, like I say, you are creating your own compile Format (the list of things in the left sidebar) from scratch, or using one of the basic ones that isn’t aimed at LaTeX use (such as “Basic MultiMarkdown” or “Default”).

Just keep in mind that the Styles list in the compile format looks for matching names of styles used in the project. That is how we magically add syntax to text that we mark. It’s a very powerful capability worth becoming familiar with as it can mean reducing clutter in the writing space. If you flip through some of the example styles in those formats, you’ll see what I mean—for example the indexing styles can add the indexing syntax around words you highlight.

I am using a custom compile format (among other things, I’m working on project started in the old scrivener). I added my ‘Raw Latex’ style to the styles list but on first try I am still getting the same problem. I see the box ‘treat as raw markup’ – does that need to be ticked? (Trying it now, anyway…)

Did you copy the prefix and suffix fields from one of the other formats? That is necessary otherwise it won’t do anything.

The “raw syntax” checkbox doesn’t do anything unless you’re using the checkbox that converts RTF style formatting to MMD (it is thus a way to type Markdown into such documents as conversion otherwise escapes MMD punctuation).

right, I didn’t understand that part of your first comment but now I see that…I think I have it now

1 Like

On to the next (related) thing – this works for inclusion of tables and figures, which pretty much by definition go in their own paragraph. But I was also using HTML comments to use table and figure references, such as:

As shown in table <!--\ref{tab:mytable}-->

These go within a paragraph and indeed within a sentence. When I select just this text to mark it as my ‘raw LaTex’ style, the whole paragraph acquires this style. Is ‘style’ inherently defined for whole paragraphs? Is there way to define this style for just a short chunk of a pragraph?

In that case you’ll probably want a pair of styles, one for whole blocks and another for inline spans (with distinctive names, like “Raw LaTeX Span” or “Raw LaTeX Block”).

You can create a character style by using the same “New Style from Selection” menu command, and making sure to set the Formatting dropdown to “Save Character Attributes” in the style configuration window. It will have a different icon in the style menus and lists, just an ‘a’. Then of course create a compile style matching it (it is important to match character styles with character styles and vice versa).

That aside, I did not realise you were looking to implement whole blocks with the first one. There are two different forms of this Markdown syntax that you can use, the one demonstrated below is safer for large blocks of multiple lines, and the other I already showed you is better for inline markings that won’t span newlines:

\caption{The caption of the image}

So the special bits here are the triple-ticks on their own lines at the beginning and end, and of course the {=latex} marker on the end of the first. (131.7 KB)

The attached project has a couple of working examples, including compile settings, for your reference. Really the only tricky thing here is to make sure that your Prefix and Suffix fields have a newline after and before, respectively, for the block method shown above. For the syntax to be valid they must fall at the start of a line.

By the way, you are aware that MultiMarkdown already does some of the things you are describing? It can generate figures, tables and you can cross-reference to them using more natural syntax to do so. I get if you want custom syntax though.

One last thing:

As shown in table <!--\ref{tab:mytable}-->

This is an example of where maybe using a style just for that is a good idea as this is the kind of thing you’re probably going to be typing in a lot of. We could cut down what you need to type to: “tab:mytable” into the editor, while the compiler, via the prefix and suffix fields, inserts this around the text:

Prefix: `\ref{
Suffix: }`{=latex}

Update (for posterity): figured out that this can be done by applying character style, rather than paragraph style, to the in-line text. One thing I am still confused about @AmberV is the best way to create a new character style. When you create a new style it seems to be a paragraph style by default. I got around this by choosing Style → redefine style from selection and redefining one of the existing character styles, but this seems like a bit of a hack.

When you create a new style it seems to be a paragraph style by default.

It’s entirely a matter of what you set it to via the Formatting dropdown in the style setup panel upon creation. You can’t convert between para and character styles, which may be the source of confusion. Once you create a style, that dropdown becomes more limited.

Aha, I see, ‘save character attributes’