如何填充表格的一部分

如何填充表格的一部分

我正在使用 LateX 通过 knitR 编写报告。报告每月生成一次,行名始终相同。生成行名的代码如下。我们将此部分称为模板。

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l}
\cline{1-2}
\multirow{2}{*}{\textit{\textbf{Outstanding Loan Portfolio}}} & \textbf{USD} &   \\ \cline{2-2}
                                                              & \#           &   \\ \cline{1-2}
\multirow{2}{*}{\textit{\textbf{Repossessed Loan Portfolio}}} & \textbf{USD} &   \\ \cline{2-2}
                                                              & \textbf{\#}  &   \\ \cline{1-2}
\textit{\textbf{Repossession ratio eop}}                      &              &   \\ \cline{1-2}
\textit{\textbf{Repossession ratio}}                          &              &   \\ \cline{1-2}
\multirow{3}{*}{\textit{\textbf{PAR1}}}                       & \textbf{USD} &   \\ \cline{2-2}
                                                              & \textbf{\%}  &   \\ \cline{2-2}
                                                              & \textbf{\#}  &   \\ \cline{1-2}
\multirow{3}{*}{\textit{\textbf{PAR30}}}                      & \textbf{USD} &   \\ \cline{2-2}
                                                              & \textbf{\%}  &   \\ \cline{2-2}
                                                              & \textbf{\#}  &   \\ \cline{1-2}
\multirow{3}{*}{\textit{\textbf{PAR60}}}                      & \textbf{USD} &   \\ \cline{2-2}
                                                              & \textbf{\%}  &   \\ \cline{2-2}
                                                              & \#           &   \\ \cline{1-2}
\end{tabular}
\end{table}

我想向该模板添加列并用变量填充列。

我想到两种方法是:

(i) 我可以读取原始数据并使用 tex 变量来填充表格。当我必须向上面的模板添加约 10 列数据时,这似乎不太可行。

(ii) 在单独的表中读取原始数据,然后将该表堆叠在原始模板旁边。

例如,如果原始数据如下所示

raw.data <- matrix(rexp(150, rate=.1), ncol=10)

创建一个表格的最佳方法是什么,其中模板(15x2 表格)位于 raw.data(15x10)旁边,就像这样

[template rawdata]

任何帮助都非常感谢!自从写完硕士论文后我就没用过 LateX,所以相当生疏 :)

答案1

pgfplotstable是您应对这一挑战的好朋友。下面是针对您的问题的 MWE。它利用rawdata.dat要打印的数据。对于此示例,我将其填充raw.data <- matrix(rexp(150, rate=.1), ncol=10)并嵌入到代码中。

结果: 向原始数据 pgfplotstable 添加列

代码:

\documentclass[border={5mm 5mm 5mm 5mm}]{standalone}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\begin{filecontents*}{rawdata.dat}
26.075830  9.4189737 12.717323  1.8988949  7.9996209 11.5243905 10.9160107  1.389448  2.8652920  1.42878541
11.166478 25.5374925  6.662897  2.9245013 13.5448818 12.0201447  1.2621673 19.217686  3.8883882  3.00943306
22.315490  9.8708633 10.965354  0.8984676  1.1459839  3.8454883 15.4547797  9.402878 11.7026024  5.47339668
11.874096  0.4823746 15.911913  6.1727927 15.1162651 34.2306360 10.3890439  2.205675  6.7049603 22.56286746
 3.320455 25.8995833  4.139036  2.2546034 12.6317242  6.8379476  8.2967574 22.880100  2.6998974  8.76554784
10.742752  1.1641779  2.921571  3.5143500  1.7125639  2.0757867  6.2450910 15.189848  0.5610844  2.13485778
15.162232  3.2194334  3.368766  8.5433378 15.0155505 10.7678579  2.2994006  7.680461 15.1313005  1.29770248
 3.142244  0.8621933  3.589560  1.3426351  7.6149833  0.9031139  8.3498388 10.337712 21.8489050  2.59146735
19.911731  5.1214246  1.928850  8.3029705  6.6282564 15.6325715 24.4529297 10.528238  4.5189110  6.80297890
 7.091692 15.0789856 14.061697  2.6505780 19.6282942  3.4523669  0.4425993 14.456910 10.4345100  2.15112929
 2.943245  1.1240063  3.323045  1.1879055  0.3924865 11.9174661 12.1291137 32.115301 12.6307365  1.16558044
 3.455690 37.7236906  2.911425 22.8336110 11.2459661 21.0664441 10.5629633 26.626076 17.7553881  1.63495051
 1.543219  2.6222850 33.359461  0.3653793  0.9214781 37.7775433 25.1749934  7.062679  6.1265833 30.66471547
 3.166661 14.8145643  1.911507 21.1688017  3.7155312 28.5108371  2.8972420  9.858501 18.9646412  0.03856634
30.285687  0.5059537 10.184070 32.2728897  5.3188059  0.8016338  2.3232517  8.902755  2.6835381  1.61584320
\end{filecontents*}
\begin{document}

\pgfplotstabletypeset[
    header=true,
        every head row/.style={before row={\toprule},after row={\midrule}},
    every last row/.style={after row=\bottomrule},
    create on use/firstcol/.style={
        create col/set list={Outstanding Loan Portfolio,,Repossessed Loan Portfolio,,Repossession ratio eop,Repossession ratio,PAR1,,,PAR30,,,PAR60}
    },
        create on use/secondcol/.style={
        create col/set list={USD,\#,USD,\#,,,USD,\%,\#,USD,\%,\#,USD,\%,\#}
    },
    columns/firstcol/.style={column name={\textbf{Element}},string type,column type=r},
    columns/secondcol/.style={column name={\textbf{Unit}},string type,column type=r},
    columns={firstcol,secondcol,0,1,2,3,4,5,6,7,8,9}
]{rawdata.dat}

\end{document}

笔记:

我没有弄清楚如何自动适应 rawdata.dat 提供的列数。因此,必须在此处手动输入列号:columns={firstcol,secondcol,0,...,9}

资源:

手动将文字列添加到从文件加载的表中

pgfplotstable 文档

答案2

这是一个简单但有效的解决方案,正如您所说,您每月都会使用此模板

\documentclass[landscape]{article}
\usepackage{multirow}

\begin{document}
<<data, echo=FALSE>>=
    raw.data <- matrix(rexp(150, rate=.1), ncol=10)
@

<<solution-naive, echo = FALSE>>=
    format.row <- function(row , f = function(x) {formatC(x , format = "f" , digits = 2)}) {
        paste0(sapply(row , f) , collapse = " & ")
    }
@
\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l| l|l|l|l|l|l|l|l|l|l|}
                                                                                                                     \cline{1-12}
\multirow{2}{*}{\textit{\textbf{Outstanding Loan Portfolio}}} & \textbf{USD} & \Sexpr{format.row(raw.data[1,])}   \\ \cline{2-12}
                                                              & \#           & \Sexpr{format.row(raw.data[2,])}   \\ \cline{1-12}
\multirow{2}{*}{\textit{\textbf{Repossessed Loan Portfolio}}} & \textbf{USD} & \Sexpr{format.row(raw.data[3,])}   \\ \cline{2-12}
                                                              & \textbf{\#}  & \Sexpr{format.row(raw.data[4,])}   \\ \cline{1-12}
\textit{\textbf{Repossession ratio eop}}                      &              & \Sexpr{format.row(raw.data[5,])}   \\ \cline{1-12}
\textit{\textbf{Repossession ratio}}                          &              & \Sexpr{format.row(raw.data[6,])}   \\ \cline{1-12}
\multirow{3}{*}{\textit{\textbf{PAR1}}}                       & \textbf{USD} & \Sexpr{format.row(raw.data[7,])}   \\ \cline{2-12}
                                                              & \textbf{\%}  & \Sexpr{format.row(raw.data[8,])}   \\ \cline{2-12}
                                                              & \textbf{\#}  & \Sexpr{format.row(raw.data[9,])}   \\ \cline{1-12}
\multirow{3}{*}{\textit{\textbf{PAR30}}}                      & \textbf{USD} & \Sexpr{format.row(raw.data[10,])}  \\ \cline{2-12}
                                                              & \textbf{\%}  & \Sexpr{format.row(raw.data[11,])}  \\ \cline{2-12}
                                                              & \textbf{\#}  & \Sexpr{format.row(raw.data[12,])}  \\ \cline{1-12}
\multirow{3}{*}{\textit{\textbf{PAR60}}}                      & \textbf{USD} & \Sexpr{format.row(raw.data[13,])}  \\ \cline{2-12}
                                                              & \textbf{\%}  & \Sexpr{format.row(raw.data[14,])}  \\ \cline{2-12}
                                                              & \#           & \Sexpr{format.row(raw.data[15,])}  \\ \cline{1-12}
\end{tabular}
\end{table}

\end{document}

您可以format.row根据自己的需要进行修改

相关内容