【问题标题】:How to place a table in a PDF via rmarkdown or format the header text more precisely?如何通过 rmarkdown 在 PDF 中放置表格或更精确地格式化标题文本?
【发布时间】:2022-01-17 11:47:42
【问题描述】:

我希望下面的 RMarkdown 代码(在帖子末尾)输出带有以下标题选项之一的页面。主要问题是如何实现格式化,因为我知道如何获取日期和页码。

我已经用谷歌搜索了将表格放入标题中,但没有找到任何东西。更精确地格式化数据也是一个选项(即 Header Option 2),但我还没有找到任何关于如何执行此操作的信息。我已经玩过格式化,但没有任何值得放在这里的东西不在下面的 RMarkdown 代码中。

问题:是否可以实施以下选项之一?以及如何做到这一点。

标题选项 1:使用表格,包括文本格式(如果可能,包括 : 字符,但这不是绝对必要的)。

标题选项 2:没有表格的格式类似,或者它甚至可以有一个框。

用于创建多页示例 PDF 的代码这是我想修改标题以包含表格解决方案或仅包含不带表格的格式化解决方案的项目。

---
title: "R Markdown Example With Numbered Sections"
output:
  bookdown::pdf_document2:
    toc: true
    toc_depth: 6
    number_sections: true
    includes:
        in_header: header.tex
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \pagestyle{fancy}
- \fancyhead[RO,RE]{Page \thepage\ of \pageref{LastPage}}
- \fancyhead[LO,LE]{Header Message}
- \fancyfoot[LE,LO]{Footer Messge on the Left}
- \fancyfoot[LE,RO]{Footer Messge on the Right}
---

\thispagestyle{fancy}

# Example R Rarkdown : Numbered Sections

## R Markdown

### Description

Some description text

\newpage

#### Details

Details go here.

\newpage

## Plots

Plots go here

\newpage

Last page

【问题讨论】:

  • 这可能会有所帮助:tex.stackexchange.com/questions/215450/…
  • @Peter 谢谢,我会仔细研究一下。对我来说不明显的是如何将该解决方案融入 R Markdown YAML
  • 有可能,我做了一些非常相似的事情,尽管基于 pdf_documnent 而不是 bookdown 版本。前段时间,我对latex只有基础知识,发现它相当繁琐,花了一段时间,但可行,基本上你只需将latex代码添加到yaml header之后的markdown文档中。我现在没有时间详细研究这个。
  • @Peter 不用担心,你所说的在 YAML 标头之后添加它有帮助。我会看看它是否有效并更新这篇文章。
  • 你最终可能会得到一堆定义标题表和文本的乳胶代码行:\thispagestyle{fancy};也许从 tex.stackexchange 链接复制代码,然后逐步修改该代码以实现您的需要。

标签: r latex r-markdown knitr pandoc


【解决方案1】:

这是一种方式,毫无疑问还有其他方式......

  1. 创建新的精美页眉样式
  2. 定义表格样式标题(并删除一些默认行为)
  3. 删除头部规则
  4. 由于某种原因,第一页不喜欢页眉,所以在第一页上强制使用\thispagestyle(...) 的页眉
  5. 将后续页面样式定义为表头。

根据需要编辑外观...

---
title: "R Markdown Example With Numbered Sections"
output:
  bookdown::pdf_document2:  
    toc: true
    toc_depth: 4
    number_sections: true


header-includes:
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \usepackage{array}
- \usepackage{lipsum}




---


\fancypagestyle{tableHeader}{
    
    \fancyhf{}
    
    \setlength\headheight{90pt}

\fancyhead[C]{
\centering{
\begin{minipage}{1.1\textwidth}
  \renewcommand{\arraystretch}{2}
    \begin{tabular}{|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
      \hline
      \multicolumn{4}{|c|}{\textbf{\LARGE{Title}}} \\ 
      \hline
      \textbf{\Large{Field 1:}}  & \textbf{\Large{Field 2:}}  & \textbf{\Large{Date:}}  & \textbf{\Large{Page:}}\\
      \textbf{\Large{Information}}  & \textbf{\Large{Information}}  & \textbf{\Large{`r Sys.Date()`}}  & \textbf{\Large{{\thepage} of \pageref{LastPage}}}\\ 
      \hline
     \end{tabular}
  \end{minipage}
  }}
}

\renewcommand{\headrulewidth}{0pt}

\thispagestyle{tableHeader}

\pagestyle{tableHeader}






# Section 1

\lipsum[3]

## sub section 


\newpage

\lipsum[2]

【讨论】:

  • 谢谢,这很有帮助。我确实希望在 RMarkdown 中做事更容易,而不必像我最近添加的那样添加尽可能多的 TeX,但至少它是一个解决方案。
  • 当我的 toc 级别为 2 并且 TOC 超过一页时发生了一件奇怪的事情,标题表从标题页消失但没有从任何其他页面消失。我不得不删除并添加回部分,直到我发现它坏了。要么有更少的部分使问题消失,要么将toc_depth 设置为 1。
  • 我遇到的另一个问题是内容中的表格(而不是标题)有时会在页面末尾被截断。似乎 R Markdown,当一个人做一些不寻常的事情时,变得有点脆弱。
  • 我怀疑这与文档格式有关,第一页或首页希望与文档正文完全分开。我怀疑如果您创建了一个单独的首页样式并在第二页上使用表格标题开始文档,那么目录可能会在不丢失标题的情况下运行多个页面。在 yaml toc: false 中尝试,并在文档开头使用\newpage \tableofcontents 启动文档(在花哨的标头代码之后)。
猜你喜欢
  • 2017-04-09
  • 1970-01-01
  • 2017-04-03
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 2017-05-29
相关资源
最近更新 更多