# Repair a pandoc-generated LaTeX table with R

Pandoc is a great piece of software but it is not always kind to HTML tables when converting to LaTeX. Especially tables containing <tr> elements with rowspan or <td> elements with colspan attributes end up as sequences of lines of text, not embedded in a table environment like longtableand devoid of both line endings (\\) and column separators (&).

First, add LaTeX table line endings (\\) wherever necessary , i.e. wherever you want them to be, based on their position in the original html table.

Then select the whole block of text, and copy it to the clipboard (with Ctrl-C or Command-C) and execute the following chunk of R code:

library(clipr) # necessary only on first use
data <- data[data != ""]
data <- str_replace_all(data,"([^\\\\])\$","\\1 & ") %>%
stri_replace_all_fixed("\\\\","\\\\\n") %>%
paste(.,collapse=" ")
write_clip(data)