LaTeX Table Generator
Python 2, 197 bytes
This solution uses string formatting to reduce repetition. Each string component is assembled, then placed within the template. It is tricky to use so many backslashes in Python strings. I used the raw string r'\\'
and multi-line '''blah \n blah'''
string techniques here.
def a(d,e):
c='c|'*e;b='& '*e+r'\\';g=' '*4+b+'\n \\hline\n';f=g*d;h='{tabular}'
return r'''\begin%s{|c|%s}
\cline{2-%s}
\multicolumn{1}{c|}{} %s
\hline
%s\end%s'''%(h,c,e+1,b,f,h)
Before the minifier, it looks like this:
def latextable(height, width):
columns = 'c|' * width;
linespec = '& ' * width + r'\\';
line = ' '*4 + linespec + '\n \\hline\n';
block = line * height;
mode = '{tabular}'
return r'''\begin%s{|c|%s}
\cline{2-%s}
\multicolumn{1}{c|}{} %s
\hline
%s\end%s''' % (mode, columns, width+1, linespec, block, mode)
print latextable(1, 1)
print latextable(2, 4)
The output tests:
\begin{tabular}{|c|c|}
\cline{2-2}
\multicolumn{1}{c|}{} & \\
\hline
& \\
\hline
\end{tabular}
\begin{tabular}{|c|c|c|c|c|}
\cline{2-5}
\multicolumn{1}{c|}{} & & & & \\
\hline
& & & & \\
\hline
& & & & \\
\hline
\end{tabular}
As a side note, I wrote a latex macro processor like this to format my thesis. I think I spent longer on developing that macro processor than on writing the words...
JavaScript (ES6), 172 170 bytes
a=>b=>`\\begin{tabular}{${'|c'[r='repeat'](++b)}|}
\\cline{2-${b--}}
\\multicolumn{1}{c|}{} ${('& '[r](b)+`\\\\
\\hline
`)[r](a+1).trim()}
\\end{tabular}`
Called as F(a)(b)
(credit to Patrick Roberts for finding this method)
Would have been much messier if not for template strings. Originally came in at 200, when I had a separate repeat for the multicolumn &
's, but cut it down a good amount by combining them and using trim
to get rid of the final unneeded indentation.