使用 markdown 包格式化管道表

使用 markdown 包格式化管道表

我正在使用pipeTablesLaTeXmarkdown包中的选项在 markdown 中呈现表格。我正在寻找一种方法来格式化所述表格,以便它们同时显示垂直线和水平线。

该软件包的作者似乎暗示了解决方案,但我的 TeX 水平不足以理解如何修改它以获得垂直和水平线:

\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

或者,如果有其他解决方案允许我事后格式化所有表格,那么同样有效。

答案1

可以调整代码。但这会产生丑陋的表格……在我看来,使用 booktabs 样式要好得多(正如您提供的链接中指出的那样;例如,您可以检查这个答案)。

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

在此处输入图片描述

如您所见,线路连接不正确。


编辑

您可以通过在前面包含以下代码来格式化标题\makeatother(修改标题样式以满足您的需要):

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

相关内容