how to add grid to table with pander?

守給你的承諾、 提交于 2019-12-10 09:31:21

问题


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>


来源:https://stackoverflow.com/questions/34853128/how-to-add-grid-to-table-with-pander

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!