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

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

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.

  1. <br>
  2. <br>
  3. ***
  4. <br>
  5. <br>
  6. \newpage
  7. # Heading 1
  8. content
  9. <br>
  10. <br>
  11. ***
  12. <br>
  13. <br>
  14. \newpage
  15. # Heading 2
  16. content
  17. <br>
  18. <br>
  19. ***
  20. <br>
  21. <br>
  22. \newpage
  23. # Heading 3
  24. 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:

  1. &lt;br&gt;
  2. &lt;br&gt;
  3. ***
  4. &lt;br&gt;
  5. &lt;br&gt;
  6. \newpage
  7. # Heading 1
  8. content
  9. &lt;br&gt;
  10. &lt;br&gt;
  11. ***
  12. &lt;br&gt;
  13. &lt;br&gt;
  14. \newpage
  15. # Heading 2
  16. content
  17. &lt;br&gt;
  18. &lt;br&gt;
  19. ***
  20. &lt;br&gt;
  21. &lt;br&gt;
  22. \newpage
  23. # Heading 3
  24. 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作为顶级分区命令,你可以这样做:

  1. ---
  2. output: pdf_document
  3. header-includes:
  4. - \pretocmd{\section}{*** \newpage}{}{}
  5. ---
  6. # 标题 1
  7. 内容
  8. # 标题 2
  9. 内容
  10. # 标题 3
  11. 内容
英文:

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:

  1. ---
  2. output: pdf_document
  3. header-includes:
  4. - \pretocmd{\section}{*** \newpage}{}{}
  5. ---
  6. # Heading 1
  7. content
  8. # Heading 2
  9. content
  10. # Heading 3
  11. 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:

确定