英文:
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:
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:
<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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论