At the beginning I would like to explain briefly what I would like to achieve in scrivener. I would like to draw a picture in the text and then mark this picture and give it a style. By default, Scrivener creates a \ begin {figure} environment. However, I would like to have the choice between figure, figure * and marginfigure.
In the replacement or style section of the compiler menu, I have configured a style for it. There I have to prefix “\ begin {marginfigue}” and as suffix later “\ end {marginfigure}” will be entered. Now when I compile the text I get “\ textbackslash {} begin \ {figure } {[} htbp {]} \ includegraphics {IMG_3579.jpg}”
In the end I would like to be able to add a caption as well as a label. How can I accomplish this?
It would help a lot to know what kind of technology you use to generate LaTeX code out of Scrivener, since that isn’t something it does natively. But from the description you provided, it sounds like you use perhaps a form of Markdown? If you are using MultiMarkdown or Pandoc, then you need to “escape” your raw LaTeX code, otherwise they will do it for you, assuming you aim to be writing a book about LaTeX, where you would want to be able to talk about these codes.
Otherwise you have the right idea I think, though if you are using Pandoc, I’d consider designing an output filter for yourself that adjusts how it generates LaTeX code at the source. I’m not sure if your approach will result in a figure inside of a figure environment or not.
That’s up to the system you are using to generate LaTeX code. With MMD (and I believe any CommonMark compatible parser) that would be:
[code]
It’s worth noting Scrivener uses this notation by default, though lacking an input for “label”, it uses the graphic’s filename as the ID:
Oh yes, iam using Scrivener with scrivomatic. So iam compiling my text to .md and scrivomatic generates afterwards the .tex file. As an Template i use a Tufte-book based template i made from scratch. I setting up the preamble and inside the \begin{document} I put $body$ as a placeholder for the text out of scrivener.
i also arranged \sidenotes or \marginnotes as styles inside scrivener and this works great. With the prefix and the suffix i am able to select a text in scrivener and set the style to sidenote for example, it looks now different in scrivener and after the compilation it looks nice in latex.
But this doesn’t work with figures.
When I finish the figure-problem the next steps will be tables but this could wait.
Okay, sounds like you know the basics, intermediate and advanced level stuff already then.
I think a custom Pandoc writer is still going to be the best answer if you want a special image type that doesn’t work like default images do. Scrivener just prints Markdown syntax for an image. There isn’t too much flexibility at that point. A figure looks like this coming out of Scrivener:
There is no \begin{figure} here, or any of that. The only way to change how that turns into LaTeX is to modify the thing that can do that: Pandoc. A few things to keep in mind:
As with all raw LaTeX it needs to be in a proper pass-thru{=latex}
If you put a suffix after an image, that might get in the way of captions, if you use a caption style on the line following the image to denote them (because the image ends before the caption line does, meaning the suffix gets inserted in between the two, breaking their adjacency). You will need to use the alternate format, which is to place the caption on the same line as the image, with one space between the image and it, with caption text wrapped in square brackets.
If you need regular figures along with the occasional margin figure, you’ll need to find some way to “tell” Pandoc that is what you want. There are lots of approaches one could take here. I don’t know too much about scrivomatic myself, maybe it has a built-in way of doing some of this, but if I needed two different kinds of figures, I’d probably use a file naming convention, since Scrivener will print that into the Markdown ID. I could then sneak in before Pandoc/MMD runs, looking for that filename convention and turning those images into fully prepared raw LaTeX syntax. Markdown would then handle the rest of the ordinary figures. That particular approach might be where a custom writer helps. If at the point where images are turned into LaTeX, you insert some Lua logic that looks for a keyword in the filename, then that is where you could write out the variant image.
Have fun with that one. I came up with a solution for inserting my own custom tabulary lines into the output, for the user manual project. I think Pandoc has a little more flexibility here, but with MMD the only tools you have at your disposal for table formatting are what a pipe table itself is capable of. So in my case I’m storing the tabulary in a custom metadata field associated with the binder item holding the table. The compile settings insert a LaTeX comment in front of each table, and my script goes through looking for these custom lines, modifying the tables it encounters that have matching captions associated with the tabulary line. It’s not pretty, and at that level I’m actually essentially running regular expressions on the final .tex file, but it’s the best I could come up with at the time
Here is the online documentation on the matter. I used the instructions found in the sample file to create a simple BBCode generator for the forum here, it is pretty easy to learn.
Thanks that was a good starting point. I get this PP-Preprocessor to live. It makes my markdownfile with self-defined Macros to an markdownfile which includes the needed latex inserts. But now I have the problem to include this inside of my compiling state.
it would be great, that I do not have to manage everything by console. Is there an opportunity to preprocess the compiling state?
Than It looks like this. Type your text in scrivener with mmd and your own macros. Compile this text and inside this compile processes there will be the preprocessor which enables the magical stuff so that in the end it appears a good looking .tex file?
Certainly! That’s what the Processing compile format pane is for. You can manually set up Pandoc with however many command-line options you need and have it all executed automatically when you compile. You would want to look into how to do this correctly with scrivomatic, if you started with a template designed for that, it may already be set up with some stuff there for citeproc and whatnot.
Great, now it works. Just typing my own macros in scrivener, hit the compile button and enjoy the good looking pdf file
thanks a lot.
The only issue left is that, the defined “chapters” in scrivener will be written as sections in latex. How can I manage this?
the Chapters in the markdown file which is the base of the compilation to tex shows up like this #introduction# for the chapters, and ## xxx ## for the sections and so on.