0

I have an R Markdown document in which every top level heading needs the same five lines of code before it to make the formatting nice. I want to know if I can set these five lines in a style.css or latex document (or otherwise externally to the R Markdown document) rather than repeat them every time.

An example of what my R Markdown looks like at the moment - some whitespace and a line, and a \newpage so it prints nicely:

<br>
<br>
***
<br>
<br>
\newpage

# Heading 1

content

<br>
<br>
***
<br>
<br>
\newpage

# Heading 2

content

<br>
<br>
***
<br>
<br>
\newpage

# Heading 3

content

It seems like bad practice (and it's annoying) to keep repeating those five lines. It also means I'm mixing up formatting and content, which I'd prefer to avoid.

I already use a style.css file for other aspects of formatting the HTML output. I'm wondering if I can use it to do this too? Failing that, perhaps in a latex document?

Either solution would have the obvious advantages of both cleaning up my R Markdown, as well as making it simpler to adjust the formatting if I want to. (I use the same style.css for multiple related documents).

The closest I have found is this:

Level 4 Heading issue in R Markdown

but I know nothin about latex documents and HTML, and am hoping there's a css solution too.

roblanf
  • 1,741
  • 3
  • 18
  • 24
  • With a functional CSS setup, you can prepend HTML to, say, level 1 headings like so: `h1::before{content: "

    "}` ... if you insist on using linebreaks instead of proper CSS like `h1{margin-top:2em}`, that is ;-)
    – I_O May 21 '23 at 07:57

1 Answers1

1

You can patch the top-level sectioning command. For example if your document uses \section as top level sectioning, you could do something like this:

---
output: pdf_document
header-includes: 
  - \pretocmd{\section}{*** \newpage}{}{}
---

# Heading 1

content

# Heading 2

content

# Heading 3

content