6

I'm using LateX through knitR in order to write a report. The report is generated once a month & the row names are always the same. The code to generate the row names is below. Let's call this piece the template.

\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 want to add columns to this template & populate the columns with variables.

The two way I have in mind are:

(i) I could read in the raw data & use tex variables to populate the table. This doesn't seem so feasible when I have to to add ~ 10 columns of data to the template above.

(ii) Read in the raw data in a separate table & then stack this table next to my original template.

So if for example the raw data looks like this

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

What would be the best way to create a table with the template (15x2 table) positioned next to the raw.data (15x10), like this

[template rawdata]

Any help is appreciated! I haven't used LateX since writing my master's thesis so quite rusty :)

1

pgfplotstable is your friend for this challenge. Below is a MWE for your problem. It utilizes a rawdata.dat for the data to be printed. For this example, I filled it with raw.data <- matrix(rexp(150, rate=.1), ncol=10) and embedded it in the code.

Result: adding columns to raw data pgfplotstable

Code:

\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}

Notes:

I didn't figure out how to automatically adapt to the number of columns provided by rawdata.dat. Therefore, one has to manually enter the column numbers here: columns={firstcol,secondcol,0,...,9}.

Resources:

Manually add a literal column to a table loaded from a file

pgfplotstable Documentation

  • thanks for the solution. It looks great. is it possible to centre the elements in multirows in the "template"? e.g. PAR1 should be in the middle of 3 rows if possible. – Octave1 Dec 14 '16 at 16:24
  • Sure. you can simply move it down one row. For example you can use ` create col/set list={Outstanding Loan Portfolio,,Repossessed Loan Portfolio,,Repossession ratio eop,Repossession ratio,,PAR1,,,PAR30,,,PAR60}` – Flinston Dec 16 '16 at 13:38
  • Thanks again, super helpful. pgfplotstable is a great fit for this problem. I must apologise as I accidentally gave the bounty to the other poster due to a misclick! – Octave1 Dec 16 '16 at 14:07
1
+100

This is a naive, but efficient solution, as you say you're using this template on a monthly basis

\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}

You can modify format.row to suit your needs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.