【发布时间】:2020-02-20 21:37:01
【问题描述】:
尝试对 bookdown 文档的 LaTeX(pdf_book 输出)进行后处理,以折叠 biblatex 引文,以便稍后使用\usepackage[sortcites]{biblatex} 按时间顺序对它们进行排序。因此,我需要在\\autocites 之后找到}{ 并将其替换为,。我正在尝试gsub(),但找不到正确的咒语。
# example input
testcase <- "text \\autocites[cf.~][]{foxMapping2000}{wattPattern1947}{runkleGap1990} text {keep}{separate}"
# desired output
"text \\autocites[cf.~][]{foxMapping2000,wattPattern1947,runkleGap1990} text {keep}{separate}"
一个简单的方法是替换所有}{
> gsub('\\}\\{', ',', testcase, perl=TRUE)
[1] "text \\autocites[cf.~][]{foxMapping2000,wattPattern1947,runkleGap1990} text {keep,separate}"
但这也会崩溃{keep}{separate}。
然后我试图通过使用不同的组来替换以\\autocites 开头的“单词”(没有空格的字符串)中的}{,但失败了:
> gsub('(\\\\autocites)([^ \f\n\r\t\v}{}]+)((\\}\\{})+)', '\\1\\2\\3', testcase, perl=TRUE)
[1] "text \\autocites[cf.~][]{foxMapping2000}{wattPattern1947}{runkleGap1990} some text {keep}{separate}"
附录:
实际文档包含比上面的测试用例更多的行/元素。并非所有元素都包含\\autocites,在极少数情况下,一个元素包含多个\\autocites。我最初并不认为这是相关的。更真实的测试用例:
testcase2 <- c("some text",
"text \\autocites[cf.~][]{foxMapping2000}{wattPattern1947}{runkleGap1990} text {keep}{separate}",
"text \\autocites[cf.~][]{foxMapping2000}{wattPattern1947}{runkleGap1990} text {keep}{separate} \\autocites[cf.~][]{foxMapping2000}{wattPattern1947}")
【问题讨论】:
-
额外的“\\autocites”段也应该同样编辑?
-
是的,所有 '}{' 都需要转换为 ',' 直到所有 '\\autocites'-strings 的空格
-
那么解胶就不行了,如果能解决你的问题,我建议接受Wiktor的解决方案。
标签: r regex gsub capture-group