停止编织器剥离电缆中的填充空间

时间:2018-11-27 15:00:51

标签: r-markdown knitr kable kableextra

我有这张表生成的下表:

---
output:
  pdf_document:
    keep_tex: true
---

```{r, include = FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
```

```{r}
df <- data_frame(
  level = c("Level 1", "Level 2", "Level 3", "Level 4"),     
  cat1_1 = c("39.5", "25.1", "28.9", "6.6"),
  cat2_1 = c("37.7", "26.1", "30.0", "6.2"),
  cat3_1 = c("30.3", "23.3", "29.7", "16.7"),
  cat1_2 = c("58.7", "29.1", "9.9", "2.3"),
  cat2_2 = c("56.4", "30.3", "10.7", "2.5"),
  cat3_2 = c("43.6", "31.4", "16.8", "8.1")
)


kable(df, format = "latex", align = "c", booktabs = TRUE, escape = TRUE,
  col.names = c("Level", "Cat1", "Cat2", "Cat3", "Cat4", "Cat5", "Cat6"),
  caption = "Percentage of Respondents in Each Category by Level and Group") %>%
  kable_styling(latex_options = "HOLD_position", full_width = TRUE) %>%
  column_spec(1, width = "12em") %>%
  row_spec(0, bold = TRUE, align = "c") %>%
  add_header_above(c(" " = 1, "Group 1 (%)" = 3,
    "Group 2 (%)" = 3), bold = TRUE)
```

enter image description here

我想要的是使列居中,但在小数点上对齐。我从这里到这里都知道,当前不支持“ S”列对齐,这很好。作为一种变通方法,我打算右对齐,然后在垫的末尾添加多余的空格以将数字推回中心:

---
output:
  pdf_document:
    keep_tex: true
---

```{r, include = FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
```

```{r}
df <- data_frame(
  level = c("Level 1", "Level 2", "Level 3", "Level 4"),     
  cat1_1 = c("39.5", "25.1", "28.9", "6.6"),
  cat2_1 = c("37.7", "26.1", "30.0", "6.2"),
  cat3_1 = c("30.3", "23.3", "29.7", "16.7"),
  cat1_2 = c("58.7", "29.1", "9.9", "2.3"),
  cat2_2 = c("56.4", "30.3", "10.7", "2.5"),
  cat3_2 = c("43.6", "31.4", "16.8", "8.1")
) %>%
  mutate_at(vars(contains("_")), funs(paste0(., paste(rep(" ", 4), collapse = ""))))
df
```

```{r}
kable(df, format = "latex", align = c("l", rep("r", 6)), booktabs = TRUE,
  col.names = c("Level", "Cat1", "Cat2", "Cat3", "Cat4", "Cat5", "Cat6"),
  caption = "Percentage of Respondents in Each Category by Level and Group") %>%
  kable_styling(latex_options = "HOLD_position", full_width = TRUE) %>%
  column_spec(1, width = "7em") %>%
  row_spec(0, bold = TRUE, align = "c") %>%
  add_header_above(c(" " = 1, "Group 1 (%)" = 3,
    "Group 2 (%)" = 3), bold = TRUE)
```

enter image description here

现在,列已成功右对齐,但尚未将其推回中间。查看原始的乳胶,我们可以看到原因:

\begin{table}[H]

\caption{\label{tab:unnamed-chunk-3}Percentage of Respondents in Each Category by Level and Group}
\centering
\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{7em}>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X}
\toprule
\multicolumn{1}{c}{\textbf{ }} & \multicolumn{3}{c}{\textbf{Group 1 (\%)}} & \multicolumn{3}{c}{\textbf{Group 2 (\%)}} \\
\cmidrule(l{3pt}r{3pt}){2-4} \cmidrule(l{3pt}r{3pt}){5-7}
\multicolumn{1}{>{\centering\arraybackslash}p{7em}}{\textbf{Level}} & \multicolumn{1}{c}{\textbf{Cat1}} & \multicolumn{1}{c}{\textbf{Cat2}} & \multicolumn{1}{c}{\textbf{Cat3}} & \multicolumn{1}{c}{\textbf{Cat4}} & \multicolumn{1}{c}{\textbf{Cat5}} & \multicolumn{1}{c}{\textbf{Cat6}}\\
\midrule
Level 1 & 39.5 & 37.7 & 30.3 & 58.7 & 56.4 & 43.6\\
Level 2 & 25.1 & 26.1 & 23.3 & 29.1 & 30.3 & 31.4\\
Level 3 & 28.9 & 30.0 & 29.7 & 9.9 & 10.7 & 16.8\\
Level 4 & 6.6 & 6.2 & 16.7 & 2.3 & 2.5 & 8.1\\
\bottomrule
\end{tabu}
\end{table}

在输入数据的地方,填充空间已被删除。我试图逃脱尾随空格:

df <- data_frame(
  level = c("Level 1", "Level 2", "Level 3", "Level 4"),     
  cat1_1 = c("39.5", "25.1", "28.9", "6.6"),
  cat2_1 = c("37.7", "26.1", "30.0", "6.2"),
  cat3_1 = c("30.3", "23.3", "29.7", "16.7"),
  cat1_2 = c("58.7", "29.1", "9.9", "2.3"),
  cat2_2 = c("56.4", "30.3", "10.7", "2.5"),
  cat3_2 = c("43.6", "31.4", "16.8", "8.1")
) %>%
  mutate_at(vars(contains("_")), funs(paste0(., paste(rep(" ", 4), collapse = "")))) %>%
  mutate_at(vars(contains("_")), str_replace_all, " ", fixed("\\\\ "))

df
#> # A tibble: 4 x 7
#>   level  cat1_1     cat2_1     cat3_1     cat1_2     cat2_2     cat3_2    
#>   <chr>  <chr>      <chr>      <chr>      <chr>      <chr>      <chr>     
#> 1 Level… "39.5\\ \… "37.7\\ \… "30.3\\ \… "58.7\\ \… "56.4\\ \… "43.6\\ \…
#> 2 Level… "25.1\\ \… "26.1\\ \… "23.3\\ \… "29.1\\ \… "30.3\\ \… "31.4\\ \…
#> 3 Level… "28.9\\ \… "30.0\\ \… "29.7\\ \… "9.9\\ \\… "10.7\\ \… "16.8\\ \…
#> 4 Level… "6.6\\ \\… "6.2\\ \\… "16.7\\ \… "2.3\\ \\… "2.5\\ \\… "8.1\\ \\…

但是在尝试编织文档时出现此错误:

! Misplaced \noalign.
\bottomrule ->\noalign 
                       {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
l.202 \end{tabu}

Error: Failed to compile add_space.tex. See add_space.log for more info.

再次,查看原始的乳胶会有所帮助:

\begin{table}[H]

\caption{\label{tab:unnamed-chunk-3}Percentage of Respondents in Each Category by Level and Group}
\centering
\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{7em}>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X}
\toprule
\multicolumn{1}{c}{\textbf{ }} & \multicolumn{3}{c}{\textbf{Group 1 (\%)}} & \multicolumn{3}{c}{\textbf{Group 2 (\%)}} \\
\cmidrule(l{3pt}r{3pt}){2-4} \cmidrule(l{3pt}r{3pt}){5-7}
\multicolumn{1}{>{\centering\arraybackslash}p{7em}}{\textbf{Level}} & \multicolumn{1}{c}{\textbf{Cat1}} & \multicolumn{1}{c}{\textbf{Cat2}} & \multicolumn{1}{c}{\textbf{Cat3}} & \multicolumn{1}{c}{\textbf{Cat4}} & \multicolumn{1}{c}{\textbf{Cat5}} & \multicolumn{1}{c}{\textbf{Cat6}}\\
\midrule
Level 1 & 39.5\ \ \ \ & 37.7\ \ \ \ & 30.3\ \ \ \ & 58.7\ \ \ \ & 56.4\ \ \ \ & 43.6\ \ \ \\\
Level 2 & 25.1\ \ \ \ & 26.1\ \ \ \ & 23.3\ \ \ \ & 29.1\ \ \ \ & 30.3\ \ \ \ & 31.4\ \ \ \\\
Level 3 & 28.9\ \ \ \ & 30.0\ \ \ \ & 29.7\ \ \ \ & 9.9\ \ \ \ & 10.7\ \ \ \ & 16.8\ \ \ \\\
Level 4 & 6.6\ \ \ \ & 6.2\ \ \ \ & 16.7\ \ \ \ & 2.3\ \ \ \ & 2.5\ \ \ \ & 8.1\ \ \ \\\
\bottomrule
\end{tabu}
\end{table}

看来最后的尾随空格仍在被截断,导致行结尾为\\\。编织pdf文档时,是否可以保持填充空间?还是有一种更好的方法来实现居中的,十进制对齐的列?

0 个答案:

没有答案