2

I'm using the pipeTables option in the LaTeX markdown package to render tables in markdown. I'm looking for a way to format said tables so that they display both vertical and horizontal lines.

The author of the package seems to have hinted at a solution, but my level of TeX is not sufficient to understand how to modify it to get both vertical and horizontal lines:

\usepackage[pipeTables,tableCaptions]{markdown}
\makeatletter
\def\markdownLaTeXReadAlignments#1{%
  \addto@hook\markdownLaTeXTableAlignment{|}%
  \advance\markdownLaTeXColumnCounter by 1\relax
  \if#1d%
    \addto@hook\markdownLaTeXTableAlignment{l}%
  \else
    \addto@hook\markdownLaTeXTableAlignment{#1}%
  \fi
  \ifnum\markdownLaTeXColumnCounter<\markdownLaTeXColumnTotal\relax\else
    \addto@hook\markdownLaTeXTableAlignment{|}%
    \expandafter\@gobble
  \fi\markdownLaTeXReadAlignments}
\makeatother

Alternatively, if there are other solutions that allow me to format all tables post-hoc that would work as well.

1 Answers1

3

It's possible to tweak the code. But this produces ugly tables… IMHO it's far better to use the booktabs style (as pointed out in the link you provided; for instance, you can check this answer).

\documentclass{article}
\usepackage{ifthen} % to set up complex condition
\usepackage{float} % to modify position specifier
\usepackage[
pipeTables,
tableCaptions,
]{markdown}
\makeatletter
\renewcommand*{\fps@table}{H} % to set up tables' position specifier to "H"
% Add vertical delimiter
\def\markdownLaTeXReadAlignments#1{%
    \addto@hook\markdownLaTeXTableAlignment{|}%
    \advance\markdownLaTeXColumnCounter by 1\relax
    \if#1d%
    \addto@hook\markdownLaTeXTableAlignment{l}%
    \else
    \addto@hook\markdownLaTeXTableAlignment{#1}%
    \fi
    \ifnum\markdownLaTeXColumnCounter<\markdownLaTeXColumnTotal\relax\else
    \addto@hook\markdownLaTeXTableAlignment{|}%
    \expandafter\@gobble
    \fi\markdownLaTeXReadAlignments}
% Add horizontal delimiter
\def\markdownLaTeXRenderTableRow#1{%
    \markdownLaTeXColumnCounter=0%
    \ifnum\markdownLaTeXRowCounter=0\relax
    \markdownLaTeXReadAlignments#1%
    \markdownLaTeXTable=\expandafter\expandafter\expandafter{%
        \expandafter\the\expandafter\markdownLaTeXTable\expandafter{%
            \the\markdownLaTeXTableAlignment}}%
    \addto@hook\markdownLaTeXTable{\markdownLaTeXTopRule}%
    \else
    \markdownLaTeXRenderTableCell#1%
    \fi
    % modifications here
    \ifthenelse{\(\markdownLaTeXRowCounter>0\relax \AND \markdownLaTeXRowCounter<\markdownLaTeXRowTotal\relax\)}{%
        \addto@hook\markdownLaTeXTable\markdownLaTeXMidRule
    }{}
    %%
    \advance\markdownLaTeXRowCounter by 1\relax
    \ifnum\markdownLaTeXRowCounter>\markdownLaTeXRowTotal\relax
    \markdownInfo{\the\markdownLaTeXTable}
    \markdownInfo{\the\markdownLaTeXTableEnd}
    \the\markdownLaTeXTable
    \the\markdownLaTeXTableEnd
    \expandafter\@gobble
    \fi\markdownLaTeXRenderTableRow}
\makeatother

\begin{filecontents*}{./example.md}

This is a table:

Right Left Default Center
12 12 12 12
123 123 123 123
1 1 1 1

: Demonstration of pipe table syntax. \end{filecontents*}

\begin{document} \markdownInput{./example.md} \end{document}

enter image description here

As you can see, lines are not correctly connected.


Edit

You can format the header by includind the following code before the \makeatother (modify the header style to fit your needs):

\def\headerStyle#1{{\sffamily\bfseries #1}}
\def\markdownLaTeXRenderTableCell#1{%
    \advance\markdownLaTeXColumnCounter by 1\relax
    \ifnum\markdownLaTeXColumnCounter<\markdownLaTeXColumnTotal\relax
        \ifnum\markdownLaTeXRowCounter=1\relax
            \addto@hook\markdownLaTeXTable{\headerStyle{#1}&}%
        \else
            \addto@hook\markdownLaTeXTable{#1&}%
        \fi
    \else
        \ifnum\markdownLaTeXRowCounter=1\relax
        \addto@hook\markdownLaTeXTable{\headerStyle{#1}\\}%
        \else
        \addto@hook\markdownLaTeXTable{#1\\}%
        \fi
    \expandafter\@gobble
    \fi\markdownLaTeXRenderTableCell}
NBur
  • 4,326
  • 10
  • 27