0

I have a table that looks like this with codes below that produces it. However I wanted to create 50 of these tables, off course with the values that changes.

enter image description here

How can I do it without writing the code like 50 times ?

---
# Combining rows and columns 
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{caption}
- \usepackage{multirow}
- \usepackage{setspace}
---

\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
\hline
 start & Jay & Arm & Eem\\
\hline
 X & 10 & 9 & 8 \\
 Y & 3  & 6 & 10 \\
 Z & 1  & 6 & 5  \\
\hline
\end{tabular}
  • 1
    Welcome to TeX-SE! pgfplotstable allows you to load data files and use their data to typeset tables. – user121799 May 12 '19 at 8:00
  • 1
    Welcome to TeX.SE! Have you got some csv files with the values or what else? – CarLaTeX May 12 '19 at 8:01
1

If the tables are identical, create your own environment. The arguments of the \newenvironment macro specifies the name of the environment, what happens when it starts, and what happens when it ends.

\documentclass ...
...
\newenvironment{mytable}
{\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
   \hline
   start & Jay & Arm & Eem\\
   \hline}
{\hline
   \end{tabular}}
...
\begin{document}
...
\begin{mytable}
 X & 10 & 9 & 8 \\
 Y & 3  & 6 & 10 \\
 Z & 1  & 6 & 5  \\
\end{mytable}
...
\end{document}
|improve this answer|||||
  • Can we use data frame. with the data frame we can alter with different data and then use it to create the table. Loop through the LaTex code and the data frame to create the same table with different results. – user188593 May 12 '19 at 14:17
1

Odd question since you are clearly using Rmarkdown. Of course, then you can take advantage of markdown simplicity to make the tables manually, and of course, you can use use a R funtion as kable ok knitr package or xtable of the xtable package to load automatically R data frame as latex tables (you can also do that with packages stargazer, Hmisc, etc. but these two are more than enough, imho).

mwe

---
title: "Untitled"
output: pdf_document
---

```{r,echo=F}
df<- data.frame(start=c("X","Y","Z"),Jay=c(10,30,1),Arm=c(9,6,6),Eem=c(8,5,10))
```

:With Rmarkdown

|start  |  Jay|  Arm|  Eem|
|:------|----:|----:|----:|
|X      |   10|    9|    8|
|Y      |    3|    6|   10|
|Z      |    1|    6|    5| 


:With Rmarkdown (simplest)

start       Jay   Arm    Eem
-------  ------ ----- ------
X            10     9      8 
Y             3     6     10 
Z             1     6      5  


```{r,echo=F}
knitr::kable(df,caption="with knitr")
```

\belowcaptionskip1em
```{r,echo=F,results='asis',message=FALSE}
require(xtable)
options(xtable.comment = F)
print(xtable(df,digits=0, caption="with xtable"),include.rownames = F,booktabs=T, caption.placement="top")
```
|improve this answer|||||
1

You didn't give enough information.

In case you have 50 csv files with your data, you can use a \foreach with a \csvreader.

My example has only three files for convenience, but you can have as many as you like.

\documentclass{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage{pgffor}
\usepackage{caption}

% filecontents is only to create the files, you don't need the following code if you already have them
\usepackage{filecontents}
\begin{filecontents*}{file1.csv}
start, Jay, Arm, Eem
 X, 10, 9, 8 
 Y, 3, 6, 10 
 Z, 1, 6, 5
\end{filecontents*} 
\begin{filecontents*}{file2.csv}
start2, Barbara, Bea, Bill
 X, 2, 2, 2 
 Y, 2, 2, 2 
 Z, 2, 2, 2
\end{filecontents*} 
\begin{filecontents*}{file3.csv}
start3, Carla, Chuck, Colin
 X, 3, 3, 3 
 Y, 3, 3, 3 
 Z, 3, 3, 3
\end{filecontents*} 
\begin{document}
\foreach \ind in {1,...,3}% you can have 50 instead of 3 here
{\begin{table}[htb]
    \centering
    \caption{A table from \texttt{file\ind.csv}}
    \csvreader[tabular=cccc,
        head=false,
        table head=\toprule,
        late after line=\\,
        late after first line=\\\midrule,
        table foot=\bottomrule,
        ]{file\ind.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{table}}
\end{document}

enter image description here

If the files are not numbered as I supposed in my MWE, you can put the file name list in the \foreach like this:

\foreach \myfile in {pippo,pluto,paperino}% put the list or your files here
{\begin{table}[htb]
    \centering
    \caption{A table from \texttt{\myfile.csv}}
    \csvreader[tabular=cccc,
        head=false,
        table head=\toprule,
        late after line=\\,
        late after first line=\\\midrule,
        table foot=\bottomrule,
        ]{\myfile.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{table}}

The code could also be changed to adapt your needs in case your files don't have the headings or so on.

|improve this answer|||||
0

Welcome to TeX-SE !

I would suggest you two methods :

  • You can create a template table and save it. So that, each time that you want to make a table, just copy and paste the empty table and insert the desired values.

  • If there are some calculations to do and you have the possibility to use Excel,take a look at Excel2LaTeX. It is simple yet useful.

There is almost certainly a way to make some automation program (with dialog boxes for example) and it'll be great if someone came up with an easy way to do this but depending on your needs/knowledge you'll choose which method suits you the best.

|improve this answer|||||
  • 1
    Reasonable suggestion however .xla is often a security / compatability issue or restriction (I and many others can not run them, at all) the simplest way is to fill cells and either export as csv or simlpy formatted plain TeX(t) using extra columns for "&" and "\\" (that would suit your first suggestion of cut and paste) – user170109 May 12 '19 at 12:55

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.