用Latex代码替换字符串向量中的重音符号 [英] Replace accents in string vector with Latex code
问题描述
定义:
df<-data.frame(name = c( Mexico,Michoacán), dat = c(1,2))
st
> df
name dat
1墨西哥1
2米却肯2
当我使用 xtable
将表打印到 .tex
文件时,重音字符会出现乱码,这不足为奇。 / p>
我想用正确的 Latex
格式替换口音,例如:
> df
name dat
1 M\'{e} xico 1
2 Michoac\'{a} n 2
请注意,在实际数据集中,有许多不同的名称带有不同的重音字母,但都具有相同的重音类型(即前斜线),因此唯一需要更改的内容 \'{。}
中的字母代替点。
在尝试一位读者的建议时,我做了以下操作:
> ; df<-data.frame(name = c( Mexico,Michoacán),dat = c(1,2))
> df
name dat
1墨西哥1
2米却肯州2
> df $ name<-sub(é, \’{e},df $ name,)
> df
name dat
1 M\\’{e} xico 1
2Michoacán2
> capture.output(
+ print(xtable(df)),
+ file = ../paper/rTables.tex,append = FALSE)
当我在记事本中打开 rTables.tex 文件时:
<$由xtable 1.5-6 package在R 2.13.1中生成的p $ p>
%乳胶表
%,2011年7月15日13:19:17
bbegin {table} [ ht]
ginbegin {center}
\begin {tabular} {rlr}
\hline
&姓名& dat \\
\hline
1& M $ \反斜杠$’\ {e\} xico& 1.00 \\
2&米却肯州2.00 \\
\hline
\end {表格}
\end {center}
\end {table}
这不是必需的。
我认为问题在于这种情况要求很多 xtable
尝试将奇怪的字符转换为 LaTeX
。尝试按如下方式覆盖 sanitize.text.function
:
print(xtable (df),sanitize.text.function = function(x){x})
系统输出以下内容:
%x2.1 1.5-6软件包在R 2.13.0中生成的乳胶表
%星期五Jul 15 10:30:00 2011
开头{table} [ht]
开头{center}
开头{tabular} {rlr}
\hline
&姓名& dat \\
\hline
1& M\’{e} xico& 1.00 \\
2&米却肯州2.00 \\
\hline
\end {tabular}
\end {center}
\end {table}
这样做可能会破坏其他 LaTeX
标记,因此请注意。
Define:
df <- data.frame(name=c("México","Michoacán"),dat=c(1,2))
s.t.
> df
name dat
1 México 1
2 Michoacán 2
When I print this table to a .tex
file using xtable
the accented characters get garbled, which is no surprise.
I would like to replace accents with proper Latex
formatting e.g.:
> df
name dat
1 M\'{e}xico 1
2 Michoac\'{a}n 2
Please note in real dataset there are many different names with different accented letters but all with same type of accent (i.e. foward-slash), so the only thing that needs to change in \'{.}
is the letter in place of the dot.
In trying one reader's suggestion i did the following:
> df <- data.frame(name=c("México","Michoacán"),dat=c(1,2))
> df
name dat
1 México 1
2 Michoacán 2
> df$name <- sub("é", "\\\\'{e}", df$name,)
> df
name dat
1 M\\'{e}xico 1
2 Michoacán 2
> capture.output(
+ print(xtable(df)),
+ file = "../paper/rTables.tex", append = FALSE)
When I opened the rTables.tex file in Notepad:
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Fri Jul 15 13:19:17 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlr}
\hline
& name & dat \\
\hline
1 & M$\backslash$'\{e\}xico & 1.00 \\
2 & Michoacán & 2.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
This is not what is needed.
I think the problem is that this case is asking a lot of xtable
's attempts to convert strange characters to LaTeX
. Try overriding sanitize.text.function
as follows:
print(xtable(df),sanitize.text.function=function(x){x})
which on my system outputs this:
% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Fri Jul 15 10:30:00 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlr}
\hline
& name & dat \\
\hline
1 & M\'{e}xico & 1.00 \\
2 & Michoacán & 2.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
It might be that other LaTeX
markup may be broken by doing this, though, so be aware of that.
这篇关于用Latex代码替换字符串向量中的重音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!