how to add grid to table with pander?

2019-05-07 11:35发布

问题:

I am working with R, knitr and pander, and I can't find the option that would allow me to print a grid on a table with pander. I tried :

pander(tableToPrint, style='grid') 

but it doesn't do anything, neither with this option or with 'multiline', 'rmarkdown'... I always get an horizontal line, then the column names, another horizontal line, all my data and an horizontal line. I would like to have an horizontal line between every line. Another option would be to alternate row colors.

回答1:

There is an old answer that gives the trick to do that in latex, but in needs a bit of adaptation to pandoc. One needs to add this to the latex code:

\catcode`@=11
\let \savecr \@tabularcr
\def\@tabularcr{\savecr\hline}
\catcode`@=12

However, it doesn't work if you add it through header-includes, probably because it is too late in the template. So the solution is to create a new template pandoc -D latex > template.tex (or use your usual template) and to add the previous code at the top, just after \documentclass. Then:

---
title: "Untitled"
output:
  pdf_document:
    keep_tex: yes
    template: "template.tex"
---

Produces lines at every row. The problem is, then, that the top and bottom line are doubled.

This is impossible for a docx output. Pandoc's docx template allow very little customization, and only of paragraph style.

For the sake of completeness, the html solution is even easier:

---
title: "Untitled"
output: html_document
---
<style>
th, td {
    border-bottom: 2px solid black;
}
</style>