7
2
Write a function or program which, given a number of lines and columns, generates a corresponding tabular
environment for LaTeX with no data in it.
Table format and tabular
Environment
Here is an example of a table with 2 rows and 4 columns:
As you can see, we do not count the first row nor do we count the first column: those are for the labels of the table.
The top left corner is also "missing", in that there is no border around it.
The corresponding LaTeX's code to generate this table is the following:
\begin{tabular}{|c|c|c|c|c|}
\cline{2-5}
\multicolumn{1}{c|}{} & & & & \\
\hline
& & & & \\
\hline
& & & & \\
\hline
\end{tabular}
- There are as many
c
in the first line as there are total columns (that is, 1 more than the number of columns given as input). Thesec
are separated by|
(and there are|
on both the extreme left and extreme right too). - The first horizontal separator is generated by
\cline{2-X}
where X is the index of the last column (starting from 1). - The first element of the first row is always
\multicolumn{1}{c|}{}
(this removes the borders of the top left corner). - Each row has
C
&
,C
being the number of columns given as input. Each line ends with\\
. - Each row is followed by a
\hline
.
Formatting details
- You must indent everything between the
begin{tabular}{...}
and\end{tabular}
tags with 4 spaces. - The
&
's are separated with exactly one space on each row. The first one of the row does not have an extra space before it (since it already has 4 spaces before it). Note that this is not true for the first row, where the first&
has a space before it because of the\multicolumn
element. \\
is separated from the last&
by a space.- Each
\hline
and row description is on its own new line.
Input
Input is a couple of two integers representing the number of columns and the number of rows. Note that this is the number of columns and rows excluding the very first row and very first column.
You may assume that the row and column inputs will always be at least 1, and less than 1024.
Those two numbers can be taken whichever way is most appropriate in your language (two args, a list, etc.).
Output
The output is a string of the LaTeX code that generates a table with the appropriate number of rows and columns. This can be returned from a function or outputted to STDOUT.
A trailing new line is allowed after the \end{tabular}
. Trailing spaces are not allowed at the end of any line.
Test cases
Input: (1,1)
Output:
\begin{tabular}{|c|c|}
\cline{2-2}
\multicolumn{1}{c|}{} & \\
\hline
& \\
\hline
\end{tabular}
Input: 2 rows, 4 columns
Output:
\begin{tabular}{|c|c|c|c|c|}
\cline{2-5}
\multicolumn{1}{c|}{} & & & & \\
\hline
& & & & \\
\hline
& & & & \\
\hline
\end{tabular}
Scoring
This is code-golf, so the shortest code in bytes \emph{wins}
.
1You forgot one space between the return and your raw string in your first code block. – Denker – 2016-02-23T09:54:01.960
1Also you can go with
g=' '*4
instead ofg=' '
to save one byte. – Denker – 2016-02-23T09:56:30.630@DenkerAffe, thanks for finding that error. My minifier has a bug! – Logic Knight – 2016-02-23T10:01:31.113