【问题标题】:Adapt pandoc.table column width调整 pandoc.table 列宽
【发布时间】:2014-11-17 11:33:41
【问题描述】:

以下文档给出了以下 HTML 输出(仅需要 HTML)

第一列太宽了,Reduce cell width and font size of table using pandoc.table() 在这里没有帮助。

如何强制前 2 列浪费更少的空间?

---
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)
```

【问题讨论】:

    标签: r knitr pandoc r-markdown pander


    【解决方案1】:

    pandoc.table 通过split.cells 参数支持specifying the width of columns,它可以采用简单的数字或(相对)数字/百分比的向量,快速演示:

    > 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              
    ----------------------------------------------------------------------
    

    使用pandoc 将上述markdown 转换为HTML 后,会产生以下HTML:

    <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>
    

    【讨论】:

    • 谢谢。我应该更仔细阅读:split.cells“也可以作为向量提供”
    • 此解决方案导致第三列从第一列和第二列捕获空间,但并没有真正直接解决单元格宽度。假设您有更多列,并且结果该 pander 正在拆分表。此解决方案不允许您压缩具有额外空间的列,同时保留长文本列以使所有列位于同一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2023-03-29
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多