捕获R输出并替换为LaTeX代码
我试图从一些R代码中捕获输出,并用latex代码替换它 如果运行此代码:捕获R输出并替换为LaTeX代码,r,string,stargazer,R,String,Stargazer,我试图从一些R代码中捕获输出,并用latex代码替换它 如果运行此代码: library(stargazer) x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table.")) x 我需要用以下内容替换第5行: " \\caption[short caption]{The main caption of the table.} " 我如何
library(stargazer)
x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table."))
x
我需要用以下内容替换第5行:
" \\caption[short caption]{The main caption of the table.} "
我如何才能做到这一点?试试:
x <- sub("\\caption{The main caption of the table.}",
"\\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)
x这与您的想法略有不同,但您可以使用xtable
,它有一个标题.宽度
参数,例如:
print.xtable(xtable(mtcars[1:5, 1:3],
caption="The main caption of the table"),
caption.width="10cm",
caption.placement="top")
输出不会精确地移植到您想要的内容,但是如果您更喜欢更简洁的代码,也许您可以根据自己的目的来改变它;从?print.xtable
:
如果caption.width不为NULL且type=“latex”,则标题将放置在指定宽度的“parbox”中。默认值为空
以下是输出:
[1] ""
[2] "% Table created by stargazer v.5.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu"
[3] "% Date and time: Sat, Jun 27, 2015 - 11:36:07"
[4] "\\begin{table}[!htbp] \\centering "
[5] " \\caption{The main caption of the table.} "
[6] " \\label{} "
[7] "\\begin{tabular}{@{\\extracolsep{5pt}} cccc} "
[8] "\\\\[-1.8ex]\\hline "
[9] "\\hline \\\\[-1.8ex] "
[10] " & mpg & cyl & disp \\\\ "
[11] "\\hline \\\\[-1.8ex] "
[12] "Mazda RX4 & $21$ & $6$ & $160$ \\\\ "
[13] "Mazda RX4 Wag & $21$ & $6$ & $160$ \\\\ "
[14] "Datsun 710 & $22.800$ & $4$ & $108$ \\\\ "
[15] "Hornet 4 Drive & $21.400$ & $6$ & $258$ \\\\ "
[16] "Hornet Sportabout & $18.700$ & $8$ & $360$ \\\\ "
[17] "\\hline \\\\[-1.8ex] "
[18] "\\end{tabular} "
[19] "\\end{table} "
% latex table generated in R 3.1.3 by xtable 1.7-4 package
% Tue Jun 30 14:52:06 2015
\begin{table}[ht]
\centering
\parbox{5cm}{\caption{The main caption of the table}}
\begin{tabular}{rrrr}
\hline
& mpg & cyl & disp \\
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 \\
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 \\
Datsun 710 & 22.80 & 4.00 & 108.00 \\
Hornet 4 Drive & 21.40 & 6.00 & 258.00 \\
Hornet Sportabout & 18.70 & 8.00 & 360.00 \\
\hline
\end{tabular}
\end{table}
您还必须处理其他选项(例如,digits
),以获得与stargazer
匹配的表的其余部分的精确格式,这取决于您所考虑的具体格式。x@stevenbaupré是否要添加此作为答案?