Setting up numbering and heading formats in Pandoc and Typst

I’m not much of a coder, but this intrigues me. How would you do that?

As a work around I’m just converting the numbering to Uppercase after compiling.

How? What’s the command?

Depending on what type of file you are converting to with Markdown, you generally won’t have much actual coding involved, fortunately. LaTeX can get pretty intense if you can’t find a good stylesheet out there that does what you want (thankfully, with decades of volunteers contributing designs and such, you often will). CSS is pretty simple, but can be bit more like coding for setting up counters and incrementing them (though honestly it’s not much different than Scrivener’s placeholders once you transpose the syntax being used; for example, counter-reset: counterName instead of <$rst_counterName>, which is honestly even easier to understand than Scrivener).

I don’t actually have anything readily accessible at the moment that would demonstrate part/chapter/section numbering in CSS, but I do have this write-up on building custom enumeration. If you scroll down to the bottom of that post you’ll see how counters can be named, used and incremented, as well as how text can be inserted with the content attribute.

But for DOCX? You’d just be using the word processor’s native style tools. Here’s a brief description of how to do that, and plug the settings into Scrivener. That’s a good thread to skim as well, as it touches on some of these very concepts. Even if you do decide to stick with Scrivener’s numbering, as they did, stylesheets can still handle the uppercase problem themselves. I just figured I’d mention that (in LibreOffice anyway) you can keep the output more agile, rather than having the letters “T”, “W” and “O” printed verbatim into the text itself. I’ve personally never cared for “hard coding” documents like that! :slight_smile:

2 Likes

I’ve tried messing with LaTeX before, but always found it complicated. Pandoc seems easier since you can copy-paste commands…

Nobody will ever disagree with that statement! :laughing: I do use it quite a bit (the user manual PDF is a LaTeX production), but mainly because I’ve been writing with it since the late '90s (and back then that meant typing it all out by hand in a coding editor), so it’s kind of the devil that I know. Typst is an upcoming alternatives for those that don’t need all of the wild stuff LaTeX can do, and you’ll find some good templates and guides in the Markdown area of the forum. I’ve given serious thought to transitioning to it gradually. It’s still maturing, but it looks to be reaching a point where it can handle the kind of design freedom I am accustomed to.

I don’t know if that’s easier than DOCX though—I guess it would boil down to experience. For me, trying to use a word processor is a million times more frustrating than learning an entirely new programming language, never mind a typesetting markup system.

3 Likes

Have you tried LyX? That’s what I messed with a while ago.

I make 90% of my .docx files in Scrivener, but fine-tune it in Word (because Pandoc can’t produce PDFs without LaTeX) to produce a PDF. If LaTeX were easier to use I could’ve done my dissertation faster xD

1 Like

Have you tried LyX? That’s what I messed with a while ago.

Oh yes! In fact my very first NaNoWriMo attempt was written entirely in LyX. I still do keep a copy around, but since most of my .tex files are produced by either MultiMarkdown or Pandoc these days, I don’t have much use for it. I rarely ever “see” the LaTeX itself. If Markdown isn’t generating it, then Scrivener is, via its styles and section layout systems. What little I do use are overrides here and there, to cut to a new page to avoid an awkward paragraph break, or force a word wrap where the justification algorithm struggled. So the compiled .tex is often 100% ready to go to PDF, and I don’t need something in the middle, like LyX to do any extensive revision.

But yeah, definitely check out the Pandoc DOCX reference file workflow. Once you get the wiring set up your compile format you can forget about the command line, and do all of your formatting tweaks to the .docx file used to generate your output. I bet you could do away with almost all of the post compile fine-tuning.

1 Like

I’d be down to trying LaTeX again, but I’m scared about disk space requirements hehe.

On my experiments, I discovered you could make PDFs directly from Pandoc (using PanWriter + LaTeX), but since I don’t really know how to get Pandoc to do a TOC, I stopped using it.

Also, @AmberV: does this letter case bug exist on Mac? While I do have a Mac, it’d be a pain to get it out to test (poor thing is dying). Just want to confirm :slight_smile:

No it does not, it will follow whatever instructions you give it (in any of the three dropdowns in this tab).

On my experiments, I discovered you could make PDFs directly from Pandoc (using PanWriter + LaTeX), but since I don’t really know how to get Pandoc to do a TOC, I stopped using it.

I’ve never tried that, so I don’t have any specific tips, but in general there is a --table-of-contents command line argument that, if the target format supports it, will add them. That’s what we use in the ToC tab for Pandoc-DOCX output, along with the commands that adjust depth.

1 Like

Um, Pandoc supports 12 different PDF engines, and at least 6 of them are NOT LaTeX or TeX variants :laughing: — personally I’ve switched to Typst recently, a mostly wonderful experience, but I would consider a HTML->PDF workflow otherwise. I do still use LaTeX too. Another option is to use LibreOffice and the soffice command like soffice --headless --convert-to pdf input_file.odt after Pandoc runs and which can be automated from Scrivener (in my case using a script run via pandocomatic)…

https://pandoc.org/MANUAL.html#option--pdf-engine

3 Likes

So this is why I got a near perfect TOC using that option. Scrivener is so clever :slight_smile:

I’d love to hear more about that :eyes:

Typst + Scrivener is described here:

I made a sample workflow that generates Typst directly (no Pandoc/MMD intermediate) or via Pandoc. I personally prefer to use Pandoc to generate Typst output (perhaps unsurprising :melting_face:). You can customise the Typst templates that Pandoc uses, with Typst code being much more intelligible than TeX. There is also a free online editor so you can play with styling visually, really cool! Typst is still relatively new so new versions can bring some breaking changes, something to be aware of…

3 Likes

Is TYPYST easy to use?

Well relative to LaTeX, absolutely. BUT this is still what is effectively a programming language for layout, that means if you have a complex layout, then code is needed, and you will need to learn how to use it. The online community (a discord) are pretty friendly and helpful. These two tutorials will show you how to layout a document from scratch:

Once a layout is defined, you turn it into a template and it can be used for future documents. For use with Pandoc, and if you want to customise, you use Pandoc’s template language to tweak Typst code. This is extremely flexible (you can specify authors, addresses etc. as Scrivener metadata and Pandoc will convert to the appropriate code for each output), but adds a layer of code.

As an example for my Pandoc typst template: dotpandoc/templates/custom.typst at master · iandol/dotpandoc · GitHub – I want to specify line spacing in the Scrivener metadata and get pandoc to specify it:

$if(linestretch)$
 #set par(justify: true, spacing: 2em, leading: $linestretch$em)
$else$
 #set par(justify: true, spacing: 2em, leading: 1.5em)
$endif$

The $...$ code is Pandoc’s template, the #set par is Typst. If I have linespacing: 1.25 in my Scrivener metadata, then the Typst becomes:

#set par(justify: true, spacing: 2em, leading: 1.25em)

This allows you to tweak typst using Scrivener’s editor (I keep my document metadata as a frontmatter in the Binder), very powerful but you need to learn how to use it… There are examples available, and you can mostly hack your way with only a partial understanding (at least that’s how I do it).

I’d really give the online version a try out, looking at the premade layout templates and the many extensions available: https://typst.app

TLDR: as a layout language it is great but it is still a programmatic layout tool, thus unless you find a premade template you are happy with, requires you to learn it.

3 Likes

Hi @eliamartin65

Is Typst easy? My answer would be:

In principle, yes; in practice it depends on how much experience you have or how you get on with coding if your needs differ from the examples given in the documentation.

Basically, I agree with @nontroppo, though I would also add that the Typst forum is also a very friendly and helpful resource (and somewhat easier to navigate for me… it too is a ‘Discourse’ forum). Let me say at this point that I have received a lot of help and advice from @nontroppo for which I am very grateful.

I am elderly and ex-humanities with virtually no experience of programming or coding, but pretty much computer literate in most ‘traditional’ apps. I realised that in my workflow using Scrivener + Nisus Writer Pro (Mac-only word processor) I had pretty much ended up using a Markdown-like workflow, in that I was using styles in Scrivener to tag paragraphs and stretches of text, which macros in NWP would transform into final formatting. So I started looking into Markdown.

I now use Markdown for emails and other purposes, but having come across @nontroppo’s thread referenced above, it seemed to me that Pandoc, Quarto or Typst might let me style the way I wanted. I downloaded the project template, installed Pandoc, Quarto and Typst and started experimenting.

I soon felt that Typst was the route that I would find easiest to follow, however I also found that although I could compile @nontroppo’s project without problem, as soon as I started entering my own text to replace his, compiling was failing; in particular my tables were not rendering but coming out as text with all the code marks and fences.

Trying to work out why that was happening was beyond me as it involved Scrivener’s MMD compile routine + Pandoc coding at some point + typst, so I tried the Typst web app. The web app is really great. I use a dedicated browser for it with three tabs: the project I am working on; the documentation; the forum; and sometimes a fourth to the repository of packages. I’m now nearing the point where I will have a couple of templates (one general, the other for linguistics articles/essays) which I will set about installing as front-matter in Scrivener pure-Typst project templates. Funnily enough, in the web app, the tables were the least of my problems!

All that said, if your project is in the sciences/maths/computer sciences areas, much of the discussion and threads as well as many of the examples will be more relevant than for the humanities. There is also the (universal) problem that the documentation is written by people who are fully-conversant with the app/language so are not-necessarily cognisant of the problems a complete newcomer like me will experience. Would it help if I passed you some of my experience?

:slight_smile:
Mark

5 Likes

My project is in the fiction category (alternate history project), but I think I could fiddle around. I also own a Mac and NWP (though they’re old…)

1 Like

In that case, the most coding needed in Typst would be to set up the cover, title page, dedications etc. Chapter and scene headings would be pretty straightforward, and for the main text, all you’d need to set up would be paragraph first-line-indent + no-inter-paragraph-spacing or inter-paragraph-spacing + no-first-line-indent a mere two straightforward lines of code.
However, there is at least one existing fiction book template, which sets almost everything up for you. I haven’t explored it, so there might be some settings you could want to adjust; for instance the default paper size is apparently iso-B4, which is roughly paperback, I think.

:slight_smile:
Mark

P.S. For years I thought about the possibility of writing a novel with the premise that Harold won the Battle of Hastings. It would be fascinating working out how different society would have become and also what the language would be like, given that there wouldn’t have been the classic creole-creating impact of Norman French speaking overlords interacting with Anglo-Saxon speaking villeins and serfs, which is in some part responsible for English as we know it. But the research involved was more than I could contemplate.

2 Likes

@xiamenese & @nontroppo: I’m messing around with the Typyst web UI and it’s ridiculously easy. But any tips on how to make a compile setting in Scrivener that sets everything up for me (font, outline, headings, etc)?

Right, the web UI does make basic tweaking easy. For more complex layout you do need to start coding things. For Scrivener, there are several routes to take, but most of these involve using Scrivener styles and section types. Working with Scrivener styles allows you to make rules that translate into Typst code. For example, Typst allows you to rotate text, and you can make a rotate style (you won’t see text rotated in Scrivener, you are basically creating a semantic box to hold text in).

In the compiler you then add the correct prefix and suffix in which your text will be placed. For a Pandoc workflow this would be:

Prefix:

`#rotate(5deg)[

Suffix:

]`{=typst}

This compiles to a raw typst container that pandoc will convert to yield this typst code:

#rotate(5deg)[This text should be angled 5° in Typst.]

Screenshot_2025-05-09-23-01-35

This technique injects raw typst into your compiled document. In addition as I mentioned above you can use a custom Typst template, this allows you to take the code you may have found in the Typst app and translates it to work with Scrivener (see my template example above). Post a new issue in the markdown forum and we can shepherd a working solution…

2 Likes

I have a workflow that’s easy to do in Scrivener but complicated to do in Typyst. Think you could help me out? Open to discussing in PMs if you like.