0

I am trying to make a new command that creates a multicolumn table based on two parameters given: My MWE is the following:

\documentclass[landscape]{article}
\usepackage{array}
\usepackage{xifthen}
\usepackage{xstring}
\usepackage{multicol}

\newcommand\dummy[2]{\multicolumn{\the \numexpr #2 - #1 + 1}{c}{hola}}


\begin{document}

This is an example of \dummy{4}{5}

\end{document}

However, I have the following errors:

 Misplaced \omit. [This is a example of \dummy{4}{5}]
 Misplaced \span. [This is a example of \dummy{4}{5}]
 Misplaced \omit. [This is a example of \dummy{4}{5}]

Can anyone help me with these please?

Thank you in advance.

3
  • The \multicolumn must be inside a tabular environment with at least the same number of columns as the \multicolumn. Apr 2, 2018 at 19:51
  • Might seem dumb, but can you give the MWE with the proposed modification? How do you know forehand how many columns would be if it depends of the parameters?
    – user151562
    Apr 2, 2018 at 19:56
  • Please, read some basic information about tables in LaTeX (environment tabular, ...) and clarify your question. Neither creates \multicolumn a table nor are the parameters of \dummyand the purpose of \dummy explained. Apr 2, 2018 at 20:10

1 Answer 1

1

The \omit and \span commands that appear in the error are TeX primitives for controlling alignment environments, like tabular, and you cannot use these commands outside an alignment environment.

The \multicolumn command uses \omit and \span internally to make its thing so, consequently, you cannot use the \multicolumn outside an alignment.

To fix the issue you can wrap your \dummy command in a tabular environment:

\documentclass{article}

\newcommand\dummy[2]{\multicolumn{\numexpr(#2)-(#1)+1\relax}{c}{hola}}

\begin{document}

This is an example of
\begin{tabular}{cc}
  \dummy{4}{5}
\end{tabular}

\end{document}

Notice that I used two columns ({cc}) after the \begin{tabular}, because #2 - #1 + 1 = 5 - 4 + 1 = 2. You'll need at least the same number of columns for the tabular as the number of columns given to \multicolumn.


Also, as egreg noted, the \the isn't necessary there. \the is used for printing the contents of a box, dimen, count (and the list goes on). In this case, the value returned by \numexpr is enough.


Also², as Heiko Oberdiek noted, enclosing #1 and #2 in parentheses make the code more robust in case there are expressions in the arguments to \dummy.

3
  • 1
    I guess that \the can be omitted.
    – egreg
    Apr 2, 2018 at 20:18
  • @egreg Indeed, I didn't notice that. Thanks :) Apr 2, 2018 at 20:25
  • 1
    If #1 and/or #2 contain formulas, \numexpr(#2)-(#1)+1\relax is robuster. Apr 2, 2018 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .