使用正则表达式查找(并替换)LaTeX 表中的第 n 列



我有一个字符串,它是一个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)
假设第

1 列是n <- 1(而不是n <- 0(,您应该用于替换第 n 列的正则表达式应该是:

(?m)^(?=[^&nr]*&)((?:[^&]*&){n-1})\s*([^&]*?)\s*(&|\\)
                                ↑
                                └ replace this n-1 with real number

然后必须\1\\emph{\2}\3替换字符串。

因此,您的替换代码是:

input <- "% latex table generated in R 2.15.1 by xtable 1.7-0 packagen% Thu Jul 26 17:49:09 2012n\begin{table}[ht]n\begin{center}n\begin{tabular}{rlllr}n  \hlinengrade & sex & disadvg & ethnicty & tlimth \\ n  \hlinen  6 & M & YES & HISPANIC &  43 \\ n    7 & M & NO & BLACK &  88 \\ n    5 & F & YES & HISPANIC &  34 \\ n    3 & M & YES & HISPANIC &  65 \\ n    8 & M & YES & WHITE &  75 \\ n   \hlinen\end{tabular}n\end{center}n\end{table}n"
n <- 1
regex <- paste(c('(?m)^(?=[^&nr]*&)((?:[^&]*&){', n-1, '})\s*([^&]*?)\s*(&|\\)'), collapse='')
cat(gsub(regex, "\1\\emph{\2}\3", input, perl = TRUE))
另一种

方法是在调用 xtable 之前将列包装在 emph{}

data(tli)
tli[, 4] <- paste0("\\emph{", tli[, 4], "}")

然后你的脚本就像你一样:

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.0 by xtable 1.7-0 package
% Thu Jul 26 16:08:58 2012
begin{table}[ht]
begin{center}
begin{tabular}{rlllr}
  hline
grade & sex & disadvg & ethnicty & tlimth \ 
  hline
  6 & M & YES & $backslash$$backslash$emph{HISPANIC} &  43 \ 
    7 & M & NO & $backslash$$backslash$emph{BLACK} &  88 \ 
    5 & F & YES & $backslash$$backslash$emph{HISPANIC} &  34 \ 
    3 & M & YES & $backslash$$backslash$emph{HISPANIC} &  65 \ 
    8 & M & YES & $backslash$$backslash$emph{WHITE} &  75 \ 
   hline
end{tabular}
end{center}
end{table}

最新更新