Adapt pandoc.table column width
Asked Answered
V

1

6

The below document gives the following HTML output (only HTML required)

column widths with pandoc

The first columns are way to wide, and Reduce cell width and font size of table using pandoc.table() does not help here.

How can I force the first 2 columns to waste less space?

---
output: html_document
---

```{r,echo=FALSE, results="asis"}
library(pander)
mytab = data.frame(col1=1:2, col2=2001:2002, col3="This is a lengthy test that should wrap, and wrap again, and again and again and again")
pandoc.table(mytab)
```
Violate answered 17/11, 2014 at 11:33 Comment(0)
W
12

pandoc.table supports specifying the width of columns via the split.cells argument, which can take a simple number or a vector of (relative) numbers/percentages, Quick demo:

> pandoc.table(mytab, split.cells = c(1,1,58))

----------------------------------------------------------------------
 col1   col2                            col3                          
------ ------ --------------------------------------------------------
  1     2001  This is a lengthy test that should wrap, and wrap again,
                           and again and again and again              

  2     2002  This is a lengthy test that should wrap, and wrap again,
                           and again and again and again              
----------------------------------------------------------------------

This results in the following HTML after converting the above markdown to HTML with pandoc:

<table>
<col width="9%" />
<col width="9%" />
<col width="77%" />
<thead>
<tr class="header">
<th align="center">col1</th>
<th align="center">col2</th>
<th align="center">col3</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center">1</td>
<td align="center">2001</td>
<td align="center">This is a lengthy test that should wrap, and wrap again, and again and again and again</td>
</tr>
<tr class="even">
<td align="center">2</td>
<td align="center">2002</td>
<td align="center">This is a lengthy test that should wrap, and wrap again, and again and again and again</td>
</tr>
</tbody>
</table>
Wain answered 17/11, 2014 at 14:38 Comment(2)
Thanks. I should have read more carefully: split.cells "Can be also supplied as a vector"Violate
This solution causes the third column to capture space from the first and second but does not really address cell width directly. Suppose that you had more columns and that pander was splitting the table as a result. This solution would not allow you to compress the columns with extra space while keeping the long text column wrapped in order to get all columns on the same line.Nakashima

© 2022 - 2024 — McMap. All rights reserved.