First Paragraph Layout in Markdown


I’ve changed course slightly from my earlier questions. I’ve moved to a different “publication” where now I need to go from MMD to Word and try to match the custom styles in that document.

I can almost do that with Scrivener … I’ve set it up to handle my custom styles for output to word and it works with one exception.

The first paragraph of each “section layout” should have custom style: custom-style=“Initial Section Text”

Then every paragraph after should have a custom style: {custom-style=“Initial Section Text Indent”}

And that’s for every section layout.

I created a default body style that gives me something like this for each section.

::: {custom-style="Initial Section Text"}
lorem ipsums

::: {custom-style="Initial Section Text Indent"}
Lorem ipsums

::: {custom-style="Initial Section Text  Indent"}
more lorem's and ipsums

The question is of course, how do I configure the compiler and style formatting to give have a different prefix fo the fist paragraph of the section.

I think I’ve seen a way, but I can’t find it now.

Also please forgive me for asking two questions at once, but where can i find the settings that Scrivener uses to all pandoc? I think I need to add the option to use a “–reference-doc” when Scrivener invokes pandoc.

As the name implies this is related to first paragraph indents, but the format I’m required to create requires that be done with these custom styles.

Thanks for your help and input.


This leads directly to what is probably what you want for the solution on the main question as well. What you want to do is run pandoc -v on the command line to find where your data directory is, and then put the ‘reference.docx’ file into this folder at its root level. To generate a copy of the reference.docx file itself, use the following command in that folder:

pandoc -o reference.docx --print-default-data-file reference.docx

What that will do is change the default DOCX output without any further configuration. You would only need to go down the --reference-doc path if you wanted to have different layouts for different projects. That would involve a slightly different compiler setup, but it can be fully automated if need be.

Open the reference.docx file in your preferred word processor and customise the stylesheet to your liking. You will note that Pandoc already has a baked-in approach for handling first paragraphs. Unless you have an unusual indent policy, you should not need to force the issue with custom classes in the source. Just throw an indent on the “Text Body” style, and probably nuke the spacing settings for that and “First Paragraph”.

Oh Great!

Thanks again for a quick useful answer.

I will do just what you suggested.

But I do have an unusual indent policy. The people I’m submitting to insist I match their styles, and they didn’t define “First Paragraph” and “Text Body”. However, maybe they don’t notice. So I will try it exactly as you suggest.

But I would like to know if there is a way to do this in scrivener compiler styles.

Hopefully they don’t care if the style names are slightly different, but if they do care, I think it is possible to really get in there and change almost everything. Pandoc is nothing if not malleable—essentially all of its core document conversion behaviour is built using a template and scripting system (Lua) that is identical to user level access. Overriding how DOCX works should be possible. I just have no idea how difficult it would be (I did once create a BBCode format, but that’s considerably less complex than adjusting how the whole .docx zip package works).

Honestly there may be a break-even point where just renaming the styles in Word post-compile every time is more efficient. :slight_smile:

But I would like to know if there is a way to do this in scrivener compiler styles.

It has no “first paragraph” code that is accessible to the Markdown-based settings, nope. That probably would not be too hard to do given it does have that for its rich text-based stuff, but it has never been a high demand thing for MD output since 99% of the time the best answer for that is to leave it up to the stylesheet/preamble/document template/etc.

As for whether it is impossible, that answer is rarely true. If you’re willing to roll up your sleeves and do some scripting, the Processing compile format pane (available to plain “MultiMarkdown”) is where you would pass custom Pandoc command-line arguments—but you will also note you can switch it over to embedded script mode, and that point you can do Ruby, Python, whatever, and once you are doing that, there are very few limitations with what “Scrivener” can do. So a simple little pre-processor script that looks for a special section break marker you insert with the compile settings, that removes the marker and then wraps the first paragraph in syntax, wouldn’t be too hard. Conclude the script with any Pandoc invocation and you’d have one-click output that generates a .docx.

In short the “MultiMarkdown” compile setting is perfectly capable of becoming a custom file type generator.

Ok that’s perfect … more or less. I needed to know that the Processing compile format pane was available to plain “MultiMarkdown”. I had seen it before but couldn’t get back to it :slight_smile:

For now I’m going the route of using the style names. Good to know what’s possible.

Thanks :slight_smile:

Note, I think it should be fairly trivial to make a Word macro to “rename” the default Pandoc style names for first and subsequent pargraphs, the only wrinkle may be that “Body Text” is IIRC not a normal style but a kind of built-in one, but if so another way would be to make the macro search and replace the style. That way you just run a macro and “First Paragraph” and “Body Text” get replaced with the styles you have been asked to use…

Thanks for the suggestion. I have never created a word macro so I didn’t even think about that. But, yes I could do that, and I will looking into it.

Hey there, I actually have word macro that does a bunch of document clean up including applying a first paragraph style - thought I’d share here . This does a bunch of different things - applies the theme, declutters style gallery (as a result of transfering documents from libreoffice), deleting empty paragraphs, finds and replace “LOnormal style” with “normal style” AND applies “first paragraph” style after any paragraph that is styled as a heading or a block quote. If you want the macro to apply a certain theme, you will have to copy paste the full path where I’ve indicated.

You should be able to edit this macro to suit your needs: copy-paste into Word’s VBA editor, cut the parts that are not relevant and swap out the style names as necessary. Let me know if you have any questions.

Sub document_cleanup()
    Dim docproject As Document
    Dim parStyle As Style
    Dim i As Integer
    Const strTheme As String = "INSERT FULL PATH IF THEME HERE"       
    Set docproject = ActiveDocument             
    ' Apply themes and quick style
    With docproject
        .ApplyDocumentTheme strTheme
        .ApplyQuickStyleSet2 "INSERT QUICK STYLE SET NAME"
    End With
    ' Hide headings from quickstyles
    For i = 3 To 9
        docproject.Styles("Heading " & i).Visibility = True
    Next i
    ' Delete empty paragraphs
    For i = (docproject.Paragraphs.Count - 1) To 1 Step -1
        If docproject.Paragraphs(i).Range.Text = vbCr Then
        End If
    Next i
    ' Fix paragraph style formatting
    For i = 1 To (docproject.Paragraphs.Count)
        Set parStyle = docproject.Paragraphs(i).Style
        ' Replace LO-Normal with Normal
        If parStyle = "LO-Normal" Then
            docproject.Paragraphs(i).Style = "Normal"
        End If
        ' Apply first paragraph style after heading or quote
        If i <> docproject.Paragraphs.Count Then
            If docproject.Paragraphs(i + 1).Style <> parStyle Then
                If parStyle = "Heading 1" _
                        Or parStyle = "Heading 2" _
                        Or parStyle = "Quote" _
                        Or parStyle = "Interview" Then
                    docproject.Paragraphs(i + 1).Style = "First Paragraph"
                End If
            End If
        End If
    Next i
End Sub

Thank-you very much!

As the deadline approached to get these papers out, I had to put Scrivener aside a time to just get the papers done. This meant a lot of fiddling and frustration in Word (and LibreOffice). (Luckily for me my coauthor took a lot of that on.) So I am only now your helpful reply.

This script is EXACTLY what I need.

Now the reviewers have come back, and I/we need to make a lot of revisions. I aim to stick to Scrivener (wish me luck) and only go to Word for the final output. This script will be so useful for that!


1 Like