Pandoc无法通过Markdown(和R Markdown)呈现有效的LaTeX宏定义 [英] Pandoc fails to render valid LaTeX macro definitions from markdown (and R markdown)

查看:153
本文介绍了Pandoc无法通过Markdown(和R Markdown)呈现有效的LaTeX宏定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我具有包含LaTeX宏定义的字符对象.这 挑战是在R Markdown文档中使用这些对象,以便 将.Rmd文件转换为时,正确显示了宏定义. LaTeX(然后转换为PDF).这是一个挑战,因为Pandoc(v2.9.1和2.9.2) 无法正确呈现某些宏生成代码,即使 代码是有效的LaTeX.

In R, I have character objects that contain LaTeX macro definitions. The challenge is to use these objects in R Markdown documents, so that the macro definitions are rendered correctly when the .Rmd files are converted to LaTeX (and then to PDF). It is a challenge because Pandoc (v2.9.1 and 2.9.2) fails to render some macro-generating code correctly, even when that code is valid LaTeX.

这是一个最小的示例.首先,请考虑以下Rmd文件:

Here is a minimal example. First, consider this Rmd file:

---
title: "Rendering LaTeX Macros from R Objects"
output: 
  pdf_document:
    keep_md: true
    keep_tex: true
---

```{r}
withoutBraces <- "\\newcommand\\withoutBraces{This is a sentence.}"
withBraces    <- "\\newcommand{withBraces}{This is a sentence.}"
```

```{r, results = "asis"}
writeLines(withoutBraces)
writeLines(withBraces)
```

从RStudio编织此.Rmd文件会生成一个.tex文件,其中包括 以下输出:

Knitting this .Rmd file from RStudio produces a .tex file that includes the following output:

\newcommand\withoutBraces{This is a sentence.}

但是

\textbackslash newcommand\{withBraces\}\{This is a sentence.\}

换句话说,\withoutBraces命令在.tex中正确呈现 文档,但\withBraces命令不是.检查 揭示了编织过程的rmarkdown::render()部分很好,在 产生一个毫无问题的.md文件的感觉.问题出在 pandoc:将.md文件转换为.tex文件时,\withBraces 命令无法正确呈现.

In other words, the \withoutBraces command is rendered correctly in the .tex document, but the \withBraces command is not. Inspection reveals that the rmarkdown::render() part of the knitting process is fine, in the sense that it produces an unproblematic .md file. The problem lies with pandoc: when it converts the .md file to a .tex file, the \withBraces command doesn't render correctly.

如果我编写的是.md文件而不是.Rmd文件,则可以使用通用原始文件" 我的代码块中的"attributes"属性来获取\withoutBraces宏定义 如 @ mb21 .但是我没有办法 在使用R Markdown文件时执行此操作.有什么我可以做的吗 在编织时获取\withoutBraces定义以正确呈现 .Rmd文件转换为LaTeX和PDF?

If I were writing .md files instead of .Rmd files, I could use "generic raw attributes" in my code chunks to get the \withoutBraces macro definition to render correctly, as in this example from @mb21. But I don't see a way to do that when working with R Markdown files. Is there anything that I can do to get the \withoutBraces definition to render correctly when I am knitting an .Rmd file to LaTeX and PDF?

推荐答案

问题出在我的LaTeX格式错误上,而不是pandoc的任何问题.我写过

The problem lay with a LaTeX formatting error on my part, not in any problem with pandoc. I had written

withBraces    <- "\\newcommand{withBraces}{This is a sentence.}"

我应该写什么时候

withBraces    <- "\\newcommand{\\withBraces}{This is a sentence.}"

当我使用第二个字符串而不是第一个字符串时,转换为LaTeX没问题.

When I use the second string instead of the first, there is no problem with the conversion to LaTeX.

这篇关于Pandoc无法通过Markdown(和R Markdown)呈现有效的LaTeX宏定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆