带有Pandoc的Markdown源的HTML和LaTeX输出中带边框的表

时间:2014-12-04 17:32:35

标签: html pdf latex markdown pandoc

这是Markdown for Pandoc中的示例表。

Simple tables look like this:

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

Table:  Demonstration of simple table syntax.

不幸的是添加边框。

我可能将其编码为HTML表格,但在这种情况下,它不适用于LaTeX。

  • 如何创建一个带有LaTeX和HTML输出边框的表?

  • 如果Pandoc无法完成这项工作,是否有类似的工具可以使用?

2 个答案:

答案 0 :(得分:12)

以下CSS在使用Pandoc时将表格添加到HTML输出:

table {
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 24px;
    border-spacing: 0;
    border-bottom: 2px solid black;
    border-top: 2px solid black;
}
table th {
    padding: 3px 10px;
    background-color: white;
    border-top: none;
    border-left: none;
    border-right: none;
    border-bottom: 1px solid black;
}
table td {
    padding: 3px 10px;
    border-top: none;
    border-left: none;
    border-bottom: none;
    border-right: none;
}


/* Add border for the last row of the table.           */
/*      (Might be of use for table footnotes, later).  */
/* tr:last-child td { border-top: 2px solid black; }   */

此CSS来自Marked.app。我相信可以在应用的support website下载。

您可以告诉Pandoc使用带有--css标志的自定义CSS文件。这样的事情应该有效:

pandoc -t html                      \
       --css=/path/to/custom.css    \
       -o /path/to/output/file.html \
        /path/to/markdown/file.md

希望有所帮助。

答案 1 :(得分:1)

你可以用Pandoc做到这一点。但这需要更多的努力。

您必须利用以下事实:

  1. Pandoc可以识别Markdown [1] 中嵌入的原始LaTeX 片段。如果目标输出格式为LaTeX或PDF,则会将这些片段未更改地传递到目标文档中。

    因此,如果您知道如何在LaTeX中编写一个好看的表,请将其与Markdown一起嵌入。

    对于HTML输出,此LaTeX表代码将被忽略。这不是问题,因为......

  2. Pandoc可以识别Markdown [1] 中嵌入的原始HTML 片段。如果目标输出格式为HTML,则会将这些片段未更改地传递到目标文档中。

    因此,如果您知道如何在HML中编写好看的表格,请将其与Markdown一起嵌入。

    对于LaTeX / PDF输出,此HTML表格代码将被忽略。这不是问题,因为......

  3. Pandoc可以识别Markdown [1] .... 中嵌入的原始LaTeX 片段(aaahhh!,我们已经有了。请参见否。 1. 以上......:)


  4. 技巧:在终端窗口中以交互方式使用pandoc

    这是另一个技巧。

    如果您不知道如何开始学习LaTeX,Pandoc可以教你一些。因为您可以交互式使用Pandoc

    对于LaTeX输出:

    它是这样的:

    1. 输入终端:pandoc -t latex
    2. 点击[RETURN]
    3. 什么都没发生。
    4. 开始在终端中键入Markdown代码段(就像您将其键入文本编辑器一样)。说,你输入一张表。
    5. 当你的桌子准备就绪后,再次点击[RETURN]以换新线。
    6. 然后点击[CTRL]+[D]
    7. Voila !, LaTeX代码出现在终端窗口......
    8. 见这里:

      $ pandoc -t latex   [RETURN]
      
        Right     Left     Center     Default
      -------     ------ ----------   -------
           12     12        12            12
          123     123       123          123
            1     1          1             1
      
      Table:  Demonstration of simple table syntax.
      
      ^D
      

      老实说:我没有输入Markdown表。 我作弊了。 我从你的问题中复制了它并将其粘贴到终端中。 您看到的最后一个 [^D] 是我点击[CTRL]+[D]的时候。

      这是终端窗口中出现的内容:

      \begin{longtable}[c]{@{}rlcl@{}}
      \caption{Demonstration of simple table syntax.}\tabularnewline
      \toprule
      Right & Left & Center & Default\tabularnewline
      \midrule
      \endfirsthead
      \toprule
      Right & Left & Center & Default\tabularnewline
      \midrule
      \endhead
      12 & 12 & 12 & 12\tabularnewline
      123 & 123 & 123 & 123\tabularnewline
      1 & 1 & 1 & 1\tabularnewline
      \bottomrule
      \end{longtable}
      

      这是LaTeX从Markdown输入生成的默认LaTeX表代码。

      现在你可以谷歌搜索一些方法(如果你一个LaTeX专家)来pimp该代码,以使表看起来更好。繁重的工作已经完成。 (如果你 是一名LaTeX专家:你自己也不需要做很重的事情,不是吗?)

      对于HTML输出:

      当然,你可以像Pandoc生成它一样输出表格的HTML代码。看:

      $ pandoc -t html   [RETURN]
      
        Right     Left     Center     Default
      -------     ------ ----------   -------
           12     12        12            12
          123     123       123          123
            1     1          1             1
      
      Table:  Demonstration of simple table syntax.
      
      ^D
      
      <table>
      <caption>Demonstration of simple table syntax.</caption>
      <thead>
      <tr class="header">
      <th align="right">Right</th>
      <th align="left">Left</th>
      <th align="center">Center</th>
      <th align="left">Default</th>
      </tr>
      </thead>
      <tbody>
      <tr class="odd">
      <td align="right">12</td>
      <td align="left">12</td>
      <td align="center">12</td>
      <td align="left">12</td>
      </tr>
      <tr class="even">
      <td align="right">123</td>
      <td align="left">123</td>
      <td align="center">123</td>
      <td align="left">123</td>
      </tr>
      <tr class="odd">
      <td align="right">1</td>
      <td align="left">1</td>
      <td align="center">1</td>
      <td align="left">1</td>
      </tr>
      </tbody>
      </table>
      

      不是很好吗?


      [1] 在处理Markdown输入时,您可能需要告诉Pandoc您想要使用它的一些扩展:pandoc --from=markdown+raw_html+raw_tex+...,以防万一从默认设置开始工作......)