@@ -2383,16 +2383,26 @@ parseTableRow envname prefsufs = do
23832383 cells <- mapM (\ts -> setInput ts >> parseTableCell) rawcells
23842384 setInput oldInput
23852385 spaces
2386− return $ Row nullAttr cells
2386+ -- Because of table normalization performed by Text.Pandoc.Builder.table,
2387+ -- we need to remove empty cells
2388+ return $ Row nullAttr $ filter (\c -> c /= emptyCell) cells
23872389
23882390parseTableCell :: PandocMonad m => LP m Cell
23892391parseTableCell = do
23902392 spaces
23912393 updateState $ \st -> st{ sInTableCell = True }
2392− cell' <- parseMultiCell <|> parseSimpleCell
2394+ cell' <- ( multicolumnCell
2395+ <|> multirowCell
2396+ <|> parseSimpleCell
2397+ <|> parseEmptyCell
2398+ )
23932399 updateState $ \st -> st{ sInTableCell = False }
23942400 spaces
23952401 return cell'
2402+ where
2403+ -- The parsing of empty cells is important in LaTeX, especially when dealing
2404+ -- with multirow/multicolumn. See #6603.
2405+ parseEmptyCell = optional spaces >> return emptyCell <* optional spaces
23962406
23972407cellAlignment :: PandocMonad m => LP m Alignment
23982408cellAlignment = skipMany (symbol '|') *> alignment <* skipMany (symbol '|')
@@ -2411,32 +2421,42 @@ plainify bs = case toList bs of
24112421 [Para ils] -> plain (fromList ils)
24122422 _ -> bs
24132423
2414−parseMultiCell :: PandocMonad m => LP m Cell
2415−parseMultiCell = (controlSeq "multirow" >> parseMultirowCell)
2416− <|> (controlSeq "multicolumn" >> parseMulticolCell)
2417− where
2418− parseMultirowCell = parseMultiXCell RowSpan (const $ ColSpan 1)
2419− parseMulticolCell = parseMultiXCell (const $ RowSpan 1) ColSpan
2420−
2421− parseMultiXCell rowspanf colspanf = do
2422− span' <- fmap (fromMaybe 1 . safeRead . untokenize) braced
2423− alignment <- symbol '{' *> cellAlignment <* symbol '}'
2424−
2425− -- Two possible contents: either a nested \multirow/\multicol, or content.
2426− -- E.g. \multirow{1}{c}{\multicol{1}{c}{content}}
2427− let singleCell = do
2428− content <- plainify <$> blocks
2429− return $ cell alignment (rowspanf span') (colspanf span') content
2430−
2431− let nestedCell = do
2432− (Cell _ _ (RowSpan rs) (ColSpan cs) bs) <- parseMultiCell
2433− return $ cell
2434− alignment
2435− (RowSpan $ max span' rs)
2436− (ColSpan $ max span' cs)
2437− (fromList bs)
2438−
2439− symbol '{' *> (nestedCell <|> singleCell) <* symbol '}'
2424+multirowCell :: PandocMonad m => LP m Cell
2425+multirowCell = controlSeq "multirow" >> do
2426+ -- Full prototype for \multirow macro is:
2427+ -- \multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}
2428+ -- However, everything except `nrows` and `text` make
2429+ -- sense in the context of the Pandoc AST
2430+ _ <- optional $ symbol '[' *> cellAlignment <* symbol ']' -- vertical position
2431+ nrows <- fmap (fromMaybe 1 . safeRead . untokenize) braced
2432+ _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- bigstrut-related
2433+ _ <- symbol '{' *> manyTill anyTok (symbol '}') -- Cell width
2434+ _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']') -- Length used for fine-tuning
2435+ content <- symbol '{' *> (plainify <$> blocks) <* symbol '}'
2436+ return $ cell AlignDefault (RowSpan nrows) (ColSpan 1) content
2437+
2438+multicolumnCell :: PandocMonad m => LP m Cell
2439+multicolumnCell = controlSeq "multicolumn" >> do
2440+ span' <- fmap (fromMaybe 1 . safeRead . untokenize) braced
2441+ alignment <- symbol '{' *> cellAlignment <* symbol '}'
2442+
2443+ let singleCell = do
2444+ content <- plainify <$> blocks
2445+ return $ cell alignment (RowSpan 1) (ColSpan span') content
2446+
2447+ -- Two possible contents: either a \multirow cell, or content.
2448+ -- E.g. \multicol{1}{c}{\multirow{2}{1em}{content}}
2449+ -- Note that a \multirow cell can be nested in a \multicolumn,
2450+ -- but not the other way around. See #6603
2451+ let nestedCell = do
2452+ (Cell _ _ (RowSpan rs) _ bs) <- multirowCell
2453+ return $ cell
2454+ alignment
2455+ (RowSpan $ rs)
2456+ (ColSpan $ span')
2457+ (fromList bs)
2458+
2459+ symbol '{' *> (nestedCell <|> singleCell) <* symbol '}'
24402460
24412461-- Parse a simple cell, i.e. not multirow/multicol
24422462parseSimpleCell :: PandocMonad m => LP m Cell