5

I've been breaking my head over this, obviously must be missing something. I'm trying to get a table built from a csv with the first columns acting as a header as long is it stays the same, pretty much as in this answer: https://tex.stackexchange.com/a/225358/45330 However, I (really) want to use tabularx.

The problem is that when I set it up like this unless I add a break to a new line before the first multicolumn I get a Misplaced \omit \multispan -> \omit error,

So the following does compile, but gives an unwanted first row (I've simplified the code as much as possible, it does not look like it needs tabularx here, but in the full version it does) :

\documentclass[10pt, a4paper]{article}

\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage[explicit]{titlesec}
\usepackage[english]{babel}
\usepackage{ltablex}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{filecontents}

\begin{filecontents*}[overwrite]{documents.csv}
document;detail;description
First;Purpose;Example
First;Reason;None in particular
First;Author;That one guy
Second;Purpose;Another example
Second;Reason;None of you business
Second;Author;That other dude
Second;Second Author;A racoon
\end{filecontents*}

\begin{document}

\rowcolors{2}{blue}{white}
\begin{tabularx}{\textwidth}{c|c}
\csvreader[
  separator=semicolon,
  head to column names,
  before line = {
    \ifthenelse{\equal{\document}{\currentdocument}}{%
      \\\hline %
    }{%
      \xdef\currentdocument{\document}%
      \\\hline\multicolumn{2}{c}{\cellcolor{teal}\currentdocument}\\\hline %
    }
  },
  before first line = {
    \xdef\currentdocument{\document}
    \\        % <--------------------------------------------------------- THE HEADACHE LINE
    \multicolumn{2}{c}{\cellcolor{teal}\currentdocument}\\\hline
  },
]{documents.csv}{}{\detail & \description}
\end{tabularx}

\end{document}

The output looks like this (notice the first empty line):

enter image description here

Now, what I would think should work is to remove the \\ in the before first line part. However, when I do so, this gives me the misplace \omit error

I am clearly missing something, but for the life of my I can't find it.

1 Answer 1

6

The problem is that \multicolumn is only allowed before a cell has started, i.e., directly after \\ or &. Between those all that is allowed are commands that expand and as part of the expansion vanish without side-effects and so \xdef starts your cell making the following \multicolumn invalid. That could probably be fixed with

  \noalign{\xdef ....}%

but in your particular case that doesn't help because \csvreader is already starting the cell the way it is implemented. So the only we that I can see without a totally different implementation is something like this:


\newcommand\hiddenamp{&}

...
  before first line = {
  \xdef\currentdocument{\document}
     \hiddenamp\multicolumn{1}{c}{}\\[-12pt] 
  \multicolumn{2}{c}{\cellcolor{teal}\currentdocument}\\\hline
  },

basically accept that you get an extra row, make sure that it doesn't show by hiding the | and by backing up afterwards. The \hiddenampis needed to hide the & from the csvreader code.

One more comment: using \document as you do is rather dangerous. You \xdef it and even though that seems to work in your example, depending on what your column in the csv contains it might not (replace "First" by First \copyright" for example). As it is only meant as a means to signal your state, it is not a good choice and definitely not with \xdef. Given that \document could contain arbitrary fragile stuff maybe \protected@xdef is better (but then you need to wrap your whole definition in \makeatletter ... \makeatother or use someting sneaky such as \csname protected@xdef\encsname).

3
  • I love this answer. You didn't just posted a bunch of latex magic, but clearly explained the issue and the solution. Thanks, this is really helpful! One question though on the xdef. How would you do that in this case? May 28, 2020 at 7:18
  • @SeeDoubleYou updated my comment to include an answer to that (and an example where it fails without) May 28, 2020 at 9:14
  • Awesome, thanks again Frank! May 28, 2020 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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