使用Pandoc Grid Table扩展转Markdown为PDF异常,求问题排查
Pandoc转换Grid Tables到PDF格式异常问题
我尝试在Markdown文档中使用Grid Tables,编写的内容如下:
# Grid tables ## First example +-------------------+-------------------+ | Grid Tables | Are Beautiful | +===================+===================+ | | In code and docs | | Easy to read | | | | | +-------------------+-------------------+ | Exceptionally flexible and powerful | +-------+-------+-------+-------+-------+ | Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | +-------+-------+-------+-------+-------+ ## Second +---------------------+----------+ | Property | Earth | +=============+=======+==========+ | | min | -89.2 °C | | Temperature +-------+----------+ | 1961-1990 | mean | 14 °C | | +-------+----------+ | | min | 56.7 °C | +-------------+-------+----------+ ## End *The end.*
执行以下Pandoc命令转换为PDF:
pandoc -s -o ./test.pdf --from=markdown+grid_tables ./test.md
但转换后的PDF结果不符合预期(表格布局错乱、单元格合并逻辑失效),请问问题出在哪?
问题根源:Grid Tables语法不符合Pandoc规范
你的命令本身没有问题,但编写的Grid Tables存在多处语法错误,导致Pandoc无法正确解析:
- 第一个表格列数不统一:前半部分是2列结构,突然切换到5列分隔线,且跨列单元格
| Exceptionally flexible and powerful |未明确匹配列数,Pandoc要求同一表格内列数必须完全一致,跨列需通过对应空单元格或匹配分隔线实现。 - 第二个表格分隔线与单元格列数冲突:表头分隔线是3列结构,但表头单元格仅为2列,同时跨行单元格的嵌套写法不符合Pandoc Grid Tables的规则,正确的嵌套表头需要严格对齐分隔线与单元格位置。
修正后的示例代码
第一个表格(统一列数+跨列实现)
+-------------------+-------------------+-------------------+-------------------+-------------------+ | Grid Tables | Are Beautiful | | | | +===================+===================+===================+===================+===================+ | | In code and docs | | | | | Easy to read | | | | | | | | | | | +-------------------+-------------------+-------------------+-------------------+-------------------+ | Exceptionally flexible and powerful | | | | | +-------------------+-------------------+-------------------+-------------------+-------------------+ | Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | +-------------------+-------------------+-------------------+-------------------+-------------------+
第二个表格(正确嵌套表头)
+---------------------+---------------+ | Property | Earth | + +-------+-------+ | | min | max | +=====================+=======+=======+ | Temperature 1961-1990 | -89.2 °C | 56.7 °C | | +-------+-------+ | | mean | 14 °C | +---------------------+-------+-------+
使用修正后的Markdown内容执行原命令,即可得到符合预期的PDF表格。
内容的提问来源于stack exchange,提问作者dr_begemot
相关产品推荐
相关产品推荐

