4

I need an environment that presents data in a table. The number of right-most columns shall be adjusted at compile-time, and the first column is dynamic width. For this reason I am using \usepackage{tabularx}. However, when I add \usepackage{csquotes} to the list of packages, I get the following error:

Misplaced \noalign. (in macro \hline)

To illustrate this problem, consider the MWE below:

\documentclass{article}
\usepackage{latexsym} % for \Box
\usepackage{tabularx}
\usepackage{multicol} % for headings
% \usepackage{csquotes} % Uncomment and won't work anymore

\newcommand{\extracols}{5}
\newenvironment{mydyntable}{%
    \newcommand{\extraheadings}[1]{% Autogenerated
        $1^{st}$&$2^{nd}$&$3^{rd}$&$4^{th}$&$5^{th}$%
    }
    \newcommand{\myheading}[1]{%
        \xdef\colspan{\the\numexpr2+\extracols\relax}%
        \phantom{.} \\[-2em]\multicolumn{\colspan}{X}{\textbf{##1}}\\
    }
    \newcommand{\myrecord}[2]{% Also autogenerated
        ##1&##2&$\Box$&$\Box$&$\Box$&$\Box$&$\Box$\\
    }

    \tabularx{\textwidth}{ X r *{\extracols}{c}}
        \textbf{First} & \textbf{Second} & \extraheadings{\extracols}
    \\\hline
}{% \end{mydyntable}
    \hline\\ %Breaks here!!!!!
    \endtabularx
}

\begin{document}
\begin{mydyntable}
    \myheading{Fuuu}
    \myrecord{Hello}{World}
\end{mydyntable}
\end{document}

The trivial solution is document that the solution is not compatible with csquotes, but because of reasons both might (and will) be used altogether. Further, most probably the solution will be exported as package, so I'd also like to reduce dependencies on external packages to a minimum. Solutions I've checked include [1], [2], [3], and [4].

Already tried with environ's NewEnviron but didn't work. Also got bad results with \usepackage{tabu}.

A workaround, as the title suggests, is replacing tabularx with tabular*, but then the width of the first column becomes problematic. I would need to make some calculations on the size of each additional column on the right (all about the same size), the two first columns, and the width of the text/line. More or less like this:

\newcommand{\fcwidth}{\linewidth - (
    \widthof{\secondColumnCaption} +
    \extracols * \widthof{\extraColAvgWidth})
}

So far I've tried with \the\numexpr...\relax and the package \usepackage{calc} but with little to no success, and I ran out of ideas. If it helps, for the final implementation I'm using pgfkeys and pgffor to allow key-value customization of the captions and other stuff inside the environment, but my experience with tiks/pgf is close to null.

To get rid of the error I know I can use \tabularnewline but I don't know how to solve the \hline problem and if those hacks won't cause further incompatibility.

Any suggestion is appreciated.

Edit 1: As @moewe pointed out in the comments, the error also occurs with etoolbox

The following MWE is even more minimal and still reproduces the issue

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tabularx}

\newenvironment{mydyntable}
  {\tabularx{\textwidth}{c}}
  {\\\hline\endtabularx}

\begin{document}
\begin{mydyntable}
\end{mydyntable}
\end{document}
  • 2
    The same error occurs with etoolbox instead of csquotes. – moewe Jan 11 at 14:48
  • Thanks @moewe. I can confirm it also happens with etoolbox. Edited. – Kyordhel Jan 11 at 14:53
  • 4
    I don't know why, but the issue seems to be the definition of \AtEndEnvironment in etoolbox. If the relevant \patchcmd\end is removed from etoolbox everything works fine. – moewe Jan 11 at 14:59
5

It works with environ.

I made some adjustments for avoiding negative spacings that don't actually work, at least in the example. I left the superscript ordinals, even if I deem them ugly.

\documentclass{article}
\usepackage{latexsym} % for \Box
\usepackage{tabularx}
\usepackage{multicol} % for headings
\usepackage{csquotes} % Uncomment and won't work anymore
\usepackage{environ}

\newcommand{\extracols}{5}
\newcommand{\colspan}{\the\numexpr2+\extracols\relax}
\NewEnviron{mydyntable}{%
    \newcommand{\extraheadings}[1]{% Autogenerated
        1\textsuperscript{st}&
        2\textsuperscript{nd}&
        3\textsuperscript{rd}&
        4\textsuperscript{th}&
        5\textsuperscript{th}%
    }%
    \newcommand{\myheading}[1]{%
        \multicolumn{\colspan}{X}{\textbf{##1}}\\
    }%
    \newcommand{\myrecord}[2]{% Also autogenerated
        ##1&##2&$\Box$&$\Box$&$\Box$&$\Box$&$\Box$\\
    }%
    \begin{tabularx}{\textwidth}{ X r *{\extracols}{c}}
        \textbf{First} & \textbf{Second} & \extraheadings{\extracols}
    \\\hline
    \BODY
    \hline\\
    \end{tabularx}
}

\begin{document}

\noindent
\begin{mydyntable}
    \myheading{Fuuu}
    \myrecord{Hello}{World}
\end{mydyntable}

\end{document}

enter image description here

If you don't have paragraphs in cells, you can do the same with tabular*:

\documentclass{article}
\usepackage{latexsym} % for \Box
\usepackage{csquotes}

\newcommand{\extracols}{5}
\newcommand{\colspan}{\numexpr2+\extracols\relax}
\newenvironment{mydyntable}{%
  \newcommand{\extraheadings}[1]{% Autogenerated
    1\textsuperscript{st}&
    2\textsuperscript{nd}&
    3\textsuperscript{rd}&
    4\textsuperscript{th}&
    5\textsuperscript{th}%
  }%
  \newcommand{\myheading}[1]{%
    \multicolumn{\colspan}{l}{\textbf{##1}}\\
  }%
  \newcommand{\myrecord}[2]{% Also autogenerated
    ##1&##2&$\Box$&$\Box$&$\Box$&$\Box$&$\Box$\\
  }%
  \begin{tabular*}{\textwidth}{
    l
    @{\hspace{2\tabcolsep}\extracolsep{\fill}}
    r
    @{\hspace{2\tabcolsep}\extracolsep{0pt}}
    *{\extracols}{c}
  }
  \textbf{First} & \textbf{Second} & \extraheadings{\extracols}
  \\\hline}{%
  \hline\\\end{tabular*}}

\begin{document}

\noindent
\begin{mydyntable}
    \myheading{Fuuu}
    \myrecord{Hello}{World}
\end{mydyntable}

\end{document}
  • 1
    Any idea why etoolbox's patch for \AtEndEnvironment is problematic here? And could this be considered an issue in one of the packages involved (etoolbox or tabularx)? – moewe Jan 11 at 16:49
  • 2
    @moewe I guess the problem is with the redefined \end adding tokens with the assumption that every called environment is itself using \end, which is not possible with \tabularx. On the other hand, since tabularx uses essentially the same strategy as \NewEnviron defined environments, this seems the cleanest solution. – egreg Jan 11 at 17:25
  • @egreg Just finished re-coding everything using environ and it works like a charm. I still don't understand why it didn't in my first attempt, but the solution you provided works. Do you think it could be possible to do it without environ (e.g. using tabular and calc)? – Kyordhel Jan 13 at 17:26
  • 1
    @Kyordhel I added a way with tabular* – egreg Jan 13 at 18:58
  • @egreg Awesome!!!!! Thank you so much! I'll try it right away! – Kyordhel Jan 14 at 19:27

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.