如何重新格式化表格?使用 Markdown > Pandoc > PDF

如何重新格式化表格?使用 Markdown > Pandoc > PDF

我正在使用 Markdown 撰写文档。它包含许多 MD 文件,然后合并为 1 个,然后按如下方式发送到 Pandoc:

pandoc -s combined.markdown --from markdown+table_captions+auto_identifiers --filter mermaid-filter.cmd --pdf-engine=xelatex -o output.pdf

这可以正常工作,但是 PDF 中的表格格式如下所示: Markdown-Pandoc-PDF表格格式

我非常希望表格看起来像这样:

所需的表格格式

或者更好的是,我希望能够做到这一点:

另一种所需的表格格式

简化的组合 markdown 文件如下:

---
author:
- Matthew Petty
affiliation: None
date: \today
title: Title of Document
subtitle: Subtitle of Document
header-includes:
    - '\newcommand{\projectNumberCode}{CODE }'
    - '\newcommand{\projectName}{Project Name }'
    - '\newcommand{\coreSystemName}{Core Name }'
    - '\newcommand{\bt}[1]{\fcolorbox{gray}{lightgray}{#1}}'
    - '\defaultfontfeatures{Extension = .otf}'
    - '\usepackage{fontawesome}'
    - '\usepackage{tocloft}'
    - '\usepackage{graphicx}'
    - '\usepackage{hyperref}'
    - '\usepackage{float}'
    - '\usepackage{glossaries}'
    - '\setglossarystyle{altlistgroup}'
    - '\usepackage{xparse}'
    - '\usepackage{lscape}'
    - '\makenoidxglossaries'
documentclass: article
fontsize: 10pt
secnumdepth: 4
classoptions:
    - a4paper
    - portrait
mainfont: Arial.ttf
geometry:
- top=2cm
- left=1cm
- right=1cm
- bottom=2cm
linkcolor: Blue
numbersections: true
---

\pagebreak

\tableofcontents

\setcounter{table}{0}

\listoftables

\pagebreak

# Introduction

## Purpose

This document is testing how to format tables in Markdown/Latex

Table: Test Table

| Heading 1                                                                                                               | Heading 2          | Heading 3                                                                                                                                   |
| :---------------------------------------------------------------------------------------------------------------------- | :----------------- | :------------------------------------------------------------------------------------------------------------------------------------------ |
| This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. | This is Contents 2 | This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. |
| This is Contents 1                                                                                                      | This is Contents 2 | This is Contents 3                                                                                                                          |
| This is Contents 1                                                                                                      | This is Contents 2 | This is Contents 3                                                                                                                          |
| This is Contents 1                                                                                                      | This is Contents 2 | This is Contents 3                                                                                                                          |
| This is Contents 1                                                                                                      | This is Contents 2 | This is Contents 3                                                                                                                          |
| This is Contents 1                                                                                                      | This is Contents 2 | This is Contents 3                                                                                                                          |


\begin{center}END OF DOCUMENT\end{center}

\hrulefill

我还使用 Pandoc 创建了一个 TEX 文件,如下所示:https://pastebin.com/wFArVtuh

我不知道这个应该在哪里设置 - 在 Pandoc 还是 Latex?有人能帮忙吗?

答案1

Pandoc 不支持表格中的垂直线,请参阅https://github.com/jgm/pandoc/issues/922进行一些讨论。

longtable一个解决方法是将Pandoc 序言中的实现修改为总是打印垂直线。为此,@{}lll@{}需要通过 Pandoc 更改列规范@{}|l|l|l|@{}

可以通过以下步骤完成:

  1. 从包中@{}使用规范的开头删除。请注意,这算作两个字符,而不是三个。\StrGobbleLeftxstring
  2. @{}使用 从末尾删除\StrGobbleRight
  3. l|l替换所有出现的\StrSubstitute
  4. |在末尾添加并@{}再次用左、右包围。

处理列规范的代码longtable是调用\@mkpream(简称做序言) 带有参数#2。您可以使用包中的函数xstring来更改参数,将其存储在新宏中,然后\@mkpream使用此新宏进行调用,如下所示:

\StrGobbleLeft{#2}{2}[\pream]
\StrGobbleRight{\pream}{2}[\pream]
\StrSubstitute{\pream}{l}{|l}[\pream]
\@mkpream{@{}\pream|@{}}

可以使用包中的代码将此代码添加到longtable宏中。这将为您提供整个表格的垂直线。请注意,如果您使用包,则此方法不起作用(然后需要进行一些修改)。如果您使用内部加载的包(例如),则该方法也不起作用。\LT@array\patchcmdetoolboxarrayarraycolortbl

替换现在仅适用于左对齐的列,您可以为中心和右对齐的列添加类似的替换。

Pandoc 使用booktabs带有 的软件包\toprule\midrule\endrule不是\hline。Booktabs 引入了一些额外的垂直空间,因此在表格中使用垂直线时会留下空隙,请参阅减少长桌框架周围的间隙。那个问题也提供了一个解决方案,就是将\aboverulesep和设置\belowrulesep为0。这个应该加到Pandoc序言里。

\LT@tabularcr然后,可以通过重新定义(longtable表格行末尾的宏)来包含 ,在每一行后添加一条水平线\hline。要使此方法有效,\midrule还应重新定义为不执行任何操作,因为该行现在已由 打印\LT@tabularcr。您还可以考虑重新定义\toprule\bottomrule,但如果不更改它们,您将从\toprule/\bottomrule和的组合中获得粗线效果\hline

如果您希望单元格的垂直边距稍微大一些,您可以添加例如\renewcommand{\arraystretch}{1.3}

longtable请注意,这些类型的修改会禁用和的正常使用booktabs,因此在编写普通 LaTeX 文档时不应使用它们。但是,对于 Pandoc 来说,这可能是可以的,因为 Pandoc 生成的 LaTeX 代码具有受限制的预定义格式,其中修改的影响更可预测。

平均能量损失

---
author:
- Matthew Petty
affiliation: None
date: \today
title: Title of Document
subtitle: Subtitle of Document
header-includes:
    - '\newcommand{\projectNumberCode}{CODE }'
    - '\newcommand{\projectName}{Project Name }'
    - '\newcommand{\coreSystemName}{Core Name }'
    - '\newcommand{\bt}[1]{\fcolorbox{gray}{lightgray}{#1}}'
    - '\defaultfontfeatures{Extension = .otf}'
    - '\usepackage{fontawesome}'
    - '\usepackage{tocloft}'
    - '\usepackage{graphicx}'
    - '\usepackage{hyperref}'
    - '\usepackage{float}'
    - '\usepackage{glossaries}'
    - '\setglossarystyle{altlistgroup}'
    - '\usepackage{xparse}'
    - '\usepackage{lscape}'
    - '\makenoidxglossaries'
    - '\usepackage{etoolbox}'
    - '\usepackage{xstring}'
    - '\setlength{\aboverulesep}{0pt}'
    - '\setlength{\belowrulesep}{0pt}'
    - '\renewcommand{\arraystretch}{1.3}'
    - '\makeatletter'
    - '\patchcmd{\LT@array}{\@mkpream{#2}}{\StrGobbleLeft{#2}{2}[\pream]\StrGobbleRight{\pream}{2}[\pream]\StrSubstitute{\pream}{l}{|l}[\pream]\@mkpream{@{}\pream|@{}}}{}{}'
    - '\def\midrule{}'
    - '\apptocmd{\LT@tabularcr}{\hline}{}{}'
    - '\makeatother'
documentclass: article
fontsize: 10pt
secnumdepth: 4
classoptions:
    - a4paper
    - portrait
geometry:
- top=2cm
- left=1cm
- right=1cm
- bottom=2cm
linkcolor: Blue
numbersections: true
---

\pagebreak

\tableofcontents

\setcounter{table}{0}

\listoftables

\pagebreak

# Introduction

## Purpose

This document is testing how to format tables in Markdown/Latex

Table: Test Table

| Heading 1                                                                                                               | Heading 2                  | Heading 3                                                                                                                             |
| :---------------------------------------------------------------------------------------------------------------------- | :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------ |
| This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. | This is Contents 2         | This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3.               |
| This is Contents 1                                                                                                      | This is Contents 2         | This is Contents 3                                                                                                                    |
| This is Contents 1                                                                                                      | This is Contents 2         | This is Contents 3                                                                                                                    |
| This is Contents 1                                                                                                      | This is Contents 2         | This is Contents 3                                                                                                                    |
| This is Contents 1                                                                                                      | This is Contents 2         | This is Contents 3                                                                                                                    |
| This is Contents 1                                                                                                      | This is Contents 2         | This is Contents 3                                                                                                                    |


\begin{center}END OF DOCUMENT\end{center}

\hrulefill

结果:

在此处输入图片描述

答案2

假设您使用 Rmarkdown,您可以直接插入 LaTeX 代码,例如:

```{=tex}
\begin{tabular}{c}
Foo \\
\end{tabular}
```

另一种可能性是借助 R 包和/或 LaTeX 包制作自定义表格,例如,使用xtable(R)和tabulary(LaTeX),Rmarkdown 中的 MWE 可以是:

姆韦

---
output:
  pdf_document: default
header-includes: \usepackage{tabulary}
---

Document is testing how to format tables in Markdown/\LaTeX

```{r, results='asis',echo=F,warning=FALSE}

# To make fake contents in a data frame. 
a <-    "This is long.content. This is long.content.  
         This is long.content. This is long.content."
b <- "This is medium content."
c <- "Some content. "
df <- data.frame(A=c( a,b,c,b,c), B=c(b,c,c,c,c), C=c(b,a,b,c,c))
colnames(df) <- c("Header 1","Header 2","Header 3")

# To make the a jailed table as asked with the data frame.

library(xtable)
options(xtable.comment = FALSE)
print(xtable(df, align="l|L|L|L|"), hline.after =-1:5, 
include.rownames=FALSE,  tabular.environment = "tabulary", 
width = "\\linewidth")

```

但无论如何:),千万不要在表格中到处都是垂直和水平线。你可以用小空格来改变大多数规则,这样表格不仅更优雅,而且更易读。

相关内容