在R Markdown中的每个顶级标题前添加自定义代码。

huangapple go评论83阅读模式
英文:

Add custom code before each top-level heading in R markdown

问题

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.

<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:

Stack Overflow Link

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

英文:

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:

&lt;br&gt;
&lt;br&gt;
***
&lt;br&gt;
&lt;br&gt;
\newpage

# Heading 1

content

&lt;br&gt;
&lt;br&gt;
***
&lt;br&gt;
&lt;br&gt;
\newpage

# Heading 2

content

&lt;br&gt;
&lt;br&gt;
***
&lt;br&gt;
&lt;br&gt;
\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:

https://stackoverflow.com/questions/33134416/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.

答案1

得分: 1

你可以修补顶级分区命令。例如,如果你的文档使用\section作为顶级分区命令,你可以这样做:

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

# 标题 1

内容

# 标题 2

内容

# 标题 3

内容
英文:

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

huangapple
  • 本文由 发表于 2023年5月21日 14:11:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定