1

\tikzmark always works both before and after a single cell or a \multirow, and also after a \multicolumn.

But before a \multicolumn it gives the error:

! Misplaced \omit.
\multispan ->\omit 
                   \@multispan 
l.55        \tikzmark{cmc}\multicolumn{2}{c}{A and B}
                                                 & C\tikzmark{dmc} \\
I expect to see \omit only after tab marks or the \cr of
an alignment. Proceed, and I'll ignore this case.

Here is a MWE:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{tikzmark, calc}
\usepackage{array}
\usepackage{multirow}
\renewcommand{\arraystretch}{1.4}
\begin{document}
    All these ones work:

    \begin{tabular}{ccc}
        \tikzmark{a}A & B & \tikzmark{b}C \\ 
        D & E & F \\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
        \draw (pic cs:a) -- (pic cs:b);
    \end{tikzpicture}

    \begin{tabular}{ccc}
        A\tikzmark{c} & B & C\tikzmark{d} \\ 
        D & E & F \\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
    \draw (pic cs:c) -- (pic cs:d);
    \end{tikzpicture}

    \begin{tabular}{ccc}
        \tikzmark{amr}\multirow{2}{*}{A and D} & B & \tikzmark{bmr}C \\ 
        & E & F\\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
        \draw (pic cs:amr) -- (pic cs:bmr);
    \end{tikzpicture}

    \begin{tabular}{ccc}
        \multirow{2}{*}{A and D}\tikzmark{cmr} & B & C\tikzmark{dmr} \\ 
        & E & F\\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
        \draw (pic cs:cmr) -- (pic cs:dmr);
    \end{tikzpicture}

    \begin{tabular}{ccc}
        \multicolumn{2}{c}{A and B}\tikzmark{amc} & C\tikzmark{bmc} \\ 
        D & E & F \\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
        \draw (pic cs:amc) -- (pic cs:bmc);
    \end{tikzpicture}

    But this one doesn't work:

    \begin{tabular}{ccc}
        \tikzmark{cmc}\multicolumn{2}{c}{A and B} & C\tikzmark{dmc} \\ 
        D & E & F \\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
    \draw (pic cs:cmc) -- (pic cs:dmc);
    \end{tikzpicture}

\end{document}
CarLaTeX
  • 62,716
  • 2
    \multicolumn must be the first command in a row there. I think you need some \noalign sorcery. Our Black Sorcerers (David Carlisle and egreg) will find a way ;-) –  Apr 21 '17 at 13:00
  • @ChristianHupfer it doesn't work even if there is another column before the \multicolumn: must it always be the first command in a cell? – CarLaTeX Apr 21 '17 at 13:06
  • Different problem/question, but the same issue, in my point of view. Perhaps Ulrike's answer solves it? https://tex.stackexchange.com/questions/313647/macro-expansion-with-colortbl-and-tabular –  Apr 21 '17 at 13:17
  • 1
    yes, \multicolumn needs to be first otherwise the cell formatting from the column spec is applied, and then it is too late. You can not even have \relax before it. – David Carlisle Apr 21 '17 at 16:28

1 Answers1

4

You should be putting the \tikzmark inside the \multicolumn since it counts as text (\null). \multirow OTOH just overlays text over the next few rows assuming a constant row spacing. It is roughly the same as \smash{\begin{tabular}[t]...\end{tabular}}.

Note, if the \multicolumn is wider than the two columns, the \hfills aren't needed.

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{tikzmark, calc}
\usepackage{array}
\usepackage{multirow}
\renewcommand{\arraystretch}{1.4}
\begin{document}
    \begin{tabular}{ccc}
        \multicolumn{2}{c}{\tikzmark{cmc}\hfill A and B\hfill\null} & C\tikzmark{dmc} \\ 
        D wider & E & F \\
    \end{tabular}
    \begin{tikzpicture}[overlay,remember picture]
    \draw (pic cs:cmc) -- (pic cs:dmc);
    \end{tikzpicture}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thank you, John! The table was just an example to show the error, of course! However, what you did is more or less what I wanted to do to answer to another question https://tex.stackexchange.com/q/365620/101651 :):):) (I found out the error while I was building the answer...) – CarLaTeX Apr 21 '17 at 15:51