【问题标题】:Using R, RStudio Knit to PDF, how to pass tilde to prevent line wrapping "Table~\ref{table:data-from-phone}"?使用 R,RStudio Knit to PDF,如何传递波浪号以防止换行“Table~\ref{table:data-from-phone}”?
【发布时间】:2020-11-05 16:25:46
【问题描述】:

在 Latex 中,通常使用“~”来引用标签,以保持标签上附加的数字,例如 Figure 或 Table。

https://tex.stackexchange.com/questions/227285/problems-with-tilde-and-line-breaking/227296 显示了一个例子。

可以在此处找到一个完整的示例:

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/

在 Rmd 文件中,http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.Rmd

我有以下:

---
title: "Untitled"
output:
  pdf_document:
    keep_tex: true
    number_sections: true
    latex_engine: pdflatex
---


\section{My Section Name}
\label{sec:my}

Below you will find Equation~\ref{eq:my-model}.

\begin{equation}
\label{eq:my-model}
    Y_{jt} = \alpha + \beta X_{jt} + \upsilon_{j}  + \varepsilon_{jt} ,
\end{equation}


\subsection{My sub section}
\label{sec:mysub}

For more information, please refer to Section~\ref{sec:my}.

注意“~”波浪号在基本 Latex 用法中的概述。

当我点击 Knit-PDF 时,它会输出一些我没想到的东西:

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.pdf

由于我选择了“keep_tex”,所以我可以查看.TEX文件

http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.tex 可以在这里在线查看:http://md5.mshaffer.com/WSU_STATS419/stackoverflow/tilde/reference-tilde.tex.txt

它似乎已将波浪号替换为文本版本:

\begin{document}
\maketitle

\section{My Section Name}
\label{sec:my}

Below you will find Equation\textasciitilde{}\ref{eq:my-model}.

\begin{equation}
\label{eq:my-model}
    Y_{jt} = \alpha + \beta X_{jt} + \upsilon_{j}  + \varepsilon_{jt} ,
\end{equation}

\subsection{My sub section}
\label{sec:mysub}

For more information, please refer to
Section\textasciitilde{}\ref{sec:my}.

\end{document}

我该如何纠正这种情况?

如何让 Latex 按预期呈现?

如何让我的代字号作为间隔号取回?

【问题讨论】:

  • 我觉得在knitr github 上提出问题是个好点(查看github.com/yihui/knitr/blob/master/R/utils.R 中的escape_latex 函数)。也许 ~gsub 模式应该避免像 \S~\ 这样的东西(写成好像 \ 不是特殊字符。现在我找不到如何在正则表达式中查找 \ ......)
  • 现在我找到了。它应该至少避免\\S~\\ref,但也应该避免(tex.stackexchange.com/a/36924/8639\\S~\\d
  • 您可能需要考虑\usepackage{cleveref} 并使用\Cref{sec:my} 而不是Section~\ref{sec:my}

标签: r knitr pandoc knitr-spin


【解决方案1】:

Unicode 不间断空格应该可以工作。或者 pandoc 甚至可以将 Markdown 中的 HTML 实体解析为正确的内容,参见例如

echo ' foo' | pandoc -t latex

~foo

如果你必须在你的 markdown 中包含一些难以解析的 LaTeX,你可以使用generic raw attributes like:

```{=latex}
my LaTeX
```

【讨论】:

  • 我在问如何让“标准 Latex 语法”在 RMarkdown 中工作。在引擎盖下,“~”在被 pdflatex 处理之前被替换为“\textasciitilde{}”。我如何防止这种替换?您的答案是“创建新的语法和句法”,这与 Latex 的核心不同。当我想要 "\textasciitilde{}" 或 "\sim" 时,我会使用它。例如,\def\IIDdist{\,{\buildrel iid \over \sim}\,}
  • 既然你在写markdown,我假设你想要markdown语法?您似乎误以为您的输入格式是 LaTeX。但实际上它是 Rmd,它基本上是pandoc flavoured markdown,它可以包括原始乳胶……但不打算按照你使用它的方式使用。这就是 ~ 表现得像它的原因,它没有被解析为乳胶而是降价。
  • 那么我如何将一大块“风味降价”挡住成为纯乳胶?
  • @mshaffer 使用 ```{=latex} ,请参阅 pandoc.org/MANUAL.html#generic-raw-attribute
  • 我注意到当我使用input{path/to/latex.file} 时它不会解析乳胶。让我试试这个其他语法。如果我使用通用语法 {=text} 会怎样?
【解决方案2】:
  1. 根据this question and answer,您可以尝试将波浪号替换为“\~”或“\sim”,看看哪个有效。

  2. 由于您保留了 .tex 文件,因此即使 #1 不起作用,您也可以随时搜索并用真正的波浪号替换 \textasciitilde{}

【讨论】:

  • 我希望它表现得像“空格”而不是波浪号。也就是说,在 Latex 中表现得像它应该的那样。
  • 也许这会有所帮助:stackoverflow.com/questions/37665579/… ?
  • 谢谢米哈尔。你只是在强调这一点。 RStudio 正在改变已建立的 Latex 语法。我不想创建“新语法”,我希望“传统语法”能够工作。在幕后,他们将“~”替换为“\textasciitilde{}”。如何防止这种替换?
猜你喜欢
  • 2021-02-18
  • 1970-01-01
  • 2020-03-31
  • 2015-11-25
  • 2019-04-09
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
相关资源
最近更新 更多