I have a command to make a formatted header entry in a table. Most table header entries span 1 column, but others span multiple columns, so I defined the command as follows with an optional width argument. However, this does not compile. It gives a Misplaced \omit error.
\documentclass{article}
\newcommand{\hform}[2][1]{\multicolumn{#1}{|c|}{\textbf{#2}}}
\begin{document}
\begin{tabular}{|c|c|}
\hline
\hform[2]{Title}\\\hline
\hform{Col 1} & \hform{Col 2} \\\hline
blah & blah\\
blah & blah\\
\hline
\end{tabular}
\end{document}
Error message:
! Misplaced \omit.
\multispan ->\omit
\@multispan
l.13 \hform[2]{Title}
\\\hline
?
When I delete the optional argument and make it mandatory however, it does compile.
\documentclass{article}
\newcommand{\hform}[2]{\multicolumn{#1}{|c|}{\textbf{#2}}}
\begin{document}
\begin{tabular}{|c|c|}
\hline
\hform{2}{Title}\\\hline
\hform{1}{Col 1} & \hform{1}{Col 2} \\\hline
blah & blah\\
blah & blah\\
\hline
\end{tabular}
\end{document}
Why is this? What am I doing wrong? On my machine this is a (fairly) minimal working example to produce this problem.