我有一个字符串,它是一个 LaTeX 表。我试图找到第 n 列(假设是第三列)并将所有内容包装在里面,比如\emph{}
不匹配分隔的美元符号。
我正在寻找第一&...&
列,即第二列。然后找到下一个&...&
,这是第二个分组,绝非巧合的是表中的第三列。
我的虚拟示例有效,但有点不同,因为它在两个&...&
. 我将在稍后阶段解决一些小问题 - 我需要使用后向和前向引用&
将呼叫置于呼叫之外。\emph{}
xy <- "This is &more or less& a match and here is &another one&.\nSecond line with &occurrance 1& and &occurrance 2&"
gsub("(&.*?&)|(.*?&)(.*)(&.*?&)", "\\1\\2\\3\\\\emph{\\4}", xy, perl = TRUE)
[1] "This is &more or less& a match and here is \\emph{&another one&}.\nSecond line with &occurrance 1& and \\emph{&occurrance 2&}"
当我将它提升到带有 LaTeX 表的读取集时(砰!),它有点不同。两个 之间没有字符&...&
,这意味着一个&
与两列相邻。考虑到这一点,我删除了(.*)
. 无论我尝试什么,我都无法让它工作。有小费吗?
library(xtable)
data(tli)
tli.table <- xtable(tli[1:5,])
x <- print.xtable(tli.table, print.results = FALSE, include.rownames = FALSE)
cat(x)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Thu Jul 26 14:13:39 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllr}
\hline
grade & sex & disadvg & ethnicty & tlimth \\
\hline
6 & M & YES & HISPANIC & 43 \\
7 & M & NO & BLACK & 88 \\
5 & F & YES & HISPANIC & 34 \\
3 & M & YES & HISPANIC & 65 \\
8 & M & YES & WHITE & 75 \\
\hline
\end{tabular}
\end{center}
\end{table}
gsub("(&.*?&)(&.*?&)", "\\1\\\\emph{\\2}", x, perl = TRUE)