# How can I reformat a table? Using Markdown > Pandoc > PDF

I am using Markdown to write a document. It's in lots of MD files which are then combined into 1, then sent to Pandoc as follows:

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


This works fine, but the table format in the PDF looks like this:

I would very much like to have the table look like this:

Or even better, I'd like to be able to do this:

A simplified combined markdown file is here:

---
author:
- Matthew Petty
affiliation: None
date: \today
title: Title of Document
subtitle: Subtitle of Document
- '\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
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

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


I also got Pandoc to create a TEX file, which is here: https://pastebin.com/wFArVtuh

I don't know where this would be set - in Pandoc or Latex? Can anyone help?

• Could you add a small markdown file to your question that allows to create the output that you show using this command line call? This would make it much easier to reproduce the issue and to try and solve it. Besides that, the table actually looks quite nice without the vertical lines in my opinion, maybe you can reconsider changing it. May 5 at 19:04
• @Marijn, I will edit the question to give more details. I like the idea of clean tables with no lines, but I have quite a few tables that have cell contents that overrun, and it can be difficult to see where they start and end. It it were possible, it would be great to be able to perhaps do striped background-colored rows, and no borders. May 6 at 12:06

Pandoc does not support vertical lines in tables, see https://github.com/jgm/pandoc/issues/922 for some discussion.

A workaround is to modify the implementation of longtable from the Pandoc preamble to always print vertical lines. For this the column specification @{}lll@{} needs to be changed into @{}|l|l|l|@{} through Pandoc.

This can be done with the following steps:

1. remove @{} from the start of the specification using \StrGobbleLeft from the xstring package. Note that this counts as two characters, not three.
2. remove @{} from the end using \StrGobbleRight.
3. substitute all occurrences of l with |l using \StrSubstitute.
4. add | at the end and surround by @{} left and right again.

The code that handles the column specification in longtable is a call to \@mkpream (short for make preamble) with argument #2. You can use the functions from the xstring package to change the argument, store it in a new macro, and then call \@mkpream with this new macro, as follows:

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


This code can be added to the longtable macro \LT@array using \patchcmd from the etoolbox package. This gives you vertical lines for the full table. Note that this does not work if you use the array package (then some more modifications are needed). By extension it also doesn't work if you use packages that load array internally, such as colortbl.

The substitution now works only on left-aligned columns, you can add similar substitutions for center and right aligned columns.

Pandoc uses the booktabs package with \toprule, \midrule and \endrule instead of \hline. Booktabs introduces a bit of extra vertical space, so when using vertical lines in the table they leave a gap, see Reducing the gap around the frames of longtable. That question also provides a solution, which is to set \aboverulesep and \belowrulesep to 0. This should be added to the Pandoc preamble.

Then a horizontal line can be added after every row by redefining \LT@tabularcr (the longtable macro for the end of a table row) to include a \hline. For this to work also \midrule should be redefined to do nothing, as that line is now already printed by \LT@tabularcr. You can also consider redefining \toprule and \bottomrule, but if you don't change those you get a thick line effect from the combination of \toprule/\bottomrule and \hline.

If you want the vertical margins of the cells to be a bit larger you can add for example \renewcommand{\arraystretch}{1.3}.

Note that these kinds of modifications disable the normal use of longtable and booktabs, so they should not be used when writing a normal LaTeX document. However, for Pandoc it might be ok, because the LaTeX code produced by Pandoc has a restricted predefined format where the effect of modifications is more predictable.

MWE

---
author:
- Matthew Petty
affiliation: None
date: \today
title: Title of Document
subtitle: Subtitle of Document
- '\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
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

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


Result:

• This is great, but is there a way to make the top row a bold heading, with the thick line under the top row? May 9 at 6:17
• @muteboy for a thick line under the top row you can delete \def\midrule{} from the header-includes. For a bold header it is more difficult from LaTeX, it is the easiest to do this from your Markdown source itself by using **Heading 1** with the Markdown syntax ** for bold. May 9 at 10:21
• I tried to use this hack with the colortbl package but it throws an error : ! Package array Error: Illegal pream-token (\pream): c' used. Do you think it's fixable ? Sep 9 at 13:05
• @ChrisAga regarding "is it fixable": it might, I have tried when I wrote this answer originally but I couldn't make it work back then (and it is already messy enough) so I decided to leave it as it is in the hope that Pandoc will start supporting vertical lines natively at some point. Sep 9 at 13:16
• The thing is that \preamp is not expanded by the array` package and I cannot figure any workaround. Sep 10 at 18:43