【问题标题】:Cancel new line preventions in R's glue package取消 R 的胶水包中的新线预防
【发布时间】:2019-02-12 23:23:00
【问题描述】:

在胶水包中,您“可以在行尾使用 \\ 来防止添加换行符”。在 LaTeX 中 \\ 是换行符。

我正在寻找比当前解决方案更好的解决方案

glue_data(iris,
"\\midrule
\\textbf{{{mean(Petal.Length)}} & 820 &  100\\% \\\\
~other & 902 \\\\"
)

实际输出:

\midrule
\textbf{3.758} & 820 & 100\% \~other & 902 \\

预期输出:

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\

我目前的丑陋和容易出错的修复:

glue_data(iris,
"\\midrule
\\textbf{{{mean(iris$Petal.Length)}} & 820 &  100\\% \\\\\\
\n~other & 902 \\\\"
)

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\

【问题讨论】:

  • glue_data 似乎不适用于 LaTeX。为什么不使用其他方式将计算值插入 LaTeX(例如 knitr Rnw 文件)或其他用于宏替换的函数?
  • 好点@user2554330。我喜欢胶水的整体语法,并决定尝试这个项目。无论如何,我已经找到了对我当前解决方案的改进,它将换行符作为变量传递,因此胶水不会解释它们:lineb <- '\\\\' 然后... & 100\\% {lineb}
  • 您应该添加您的{lineb} 解决方案作为您自己问题的答案。它看起来很聪明。
  • 在 `\` 之后但在换行符之前的空格似乎也可以完成这项工作。

标签: r r-markdown knitr r-glue


【解决方案1】:

我发现一个更好的方法是将 LaTeX 换行符添加为一个变量,以防止它们被 glue 解释:

lineb <- '\\\\'
glue_data(
  iris,
  "\\midrule
  \\textbf{{{mean(Petal.Length)}} & 820 &  100\\% {lineb}
  ~other & 902 \\\\"
)

输出

\midrule
\textbf{3.758} & 820 &  100\% \\
~other & 902 \\

【讨论】:

    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    相关资源
    最近更新 更多