# Automate Repeated LaTex Code

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.

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

---
# Combining rows and columns
title: "Untitled"
output: pdf_document
- \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}

• 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
• Welcome to TeX.SE! Have you got some csv files with the values or what else? – CarLaTeX May 12 '19 at 8:01

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}

• 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

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).

---
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")



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}}
late after line=\\,
late after first line=\\\midrule,
table foot=\bottomrule,
]{file\ind.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{table}}
\end{document}


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}}
late after line=\\,
late after first line=\\\midrule,
table foot=\bottomrule,
]{\myfile.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{table}}