简体   繁体   English

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

[英]Table with borders in HTML and LaTeX output from Markdown source with Pandoc

This is a sample table in Markdown for Pandoc.这是 Pandoc 的 Markdown 示例表。

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.

It does not add borders unfortunately.不幸的是,它没有添加边框。

I might code it as an HTML table, but in this case it will not work in LaTeX.我可能会将其编码为 HTML 表格,但在这种情况下,它在 LaTeX 中不起作用。

  • How can I make a table with borders working both with LaTeX and HTML output?如何制作一个带有 LaTeX 和 HTML 输出的边框的表格?

  • If Pandoc can't do the job, is there a similar tool which is able to?如果 Pandoc 不能完成这项工作,是否有类似的工具能够做到?

The following CSS adds tables to your HTML output when using Pandoc:使用 Pandoc 时,以下 CSS 将表格添加到您的 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; }   */

This CSS is from Marked.app.这个 CSS 来自 Marked.app。 I believe it's available to download on the support website for the app.我相信它可以在应用程序的支持网站上下载。

You can tell Pandoc to use a custom CSS file with the --css flag.您可以告诉 Pandoc 使用带有--css标志的自定义 CSS 文件。 Something like this should work:像这样的东西应该工作:

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

Hope that helps.希望有帮助。

NOTE: without --standalone the CSS is not included.注意:如果没有--standalone ,则不包括 CSS。

You can do it with Pandoc.你可以用 Pandoc 做到这一点。 But it takes a little bit of more effort.但这需要更多的努力。

You have to take advantage of the following facts:您必须利用以下事实:

  1. Pandoc can recognize raw LaTeX snippets embedded in Markdown [1] . Pandoc 可以识别嵌入在 Markdown [1]中的原始 LaTeX片段。 If the target output format is LaTeX or PDF, it will pass these snippets unchanged into the target document.如果目标输出格式是 LaTeX 或 PDF,它会将这些片段原封不动地传递到目标文档中。

    So if you know how to write a good looking table in LaTeX, embed it with your Markdown.因此,如果您知道如何在 LaTeX 中编写漂亮的表格,请将其嵌入到您的 Markdown 中。

    For HTML output, this LaTeX table code will be ignored however.但是,对于 HTML 输出,此 LaTeX 表格代码将被忽略。 That's not a problem, because...这不是问题,因为...

  2. Pandoc can recogniz raw HTML snippets embedded in Markdown [1] . Pandoc 可以识别嵌入在 Markdown [1]中的原始 HTML片段。 If the target output format is HTML, it will pass these snippets unchanged into the target document.如果目标输出格式是 HTML,它会将这些片段原封不动地传递到目标文档中。

    So if you know how to write a good looking table in HML, embed it with your Markdown.因此,如果您知道如何在 HML 中编写漂亮的表格,请将其嵌入到您的 Markdown 中。

    For LaTeX/PDF output, this HTML table code will be ignored however.但是,对于 LaTeX/PDF 输出,此 HTML 表格代码将被忽略。 That's not a problem, because...这不是问题,因为...

  3. Pandoc can recognize raw LaTeX snippets embedded in Markdown [1] .... (aaahhh!, we had that already. See no. 1. above... :) Pandoc 可以识别嵌入在 Markdown [1]中的原始 LaTeX片段 .... (aaahhh!,我们已经有了。见上面的第1. ... :)


Trick: Use pandoc interactively in the terminal window技巧:在终端窗口中交互使用pandoc

Here is another trick.这是另一个技巧。

If you do not know how to start with learning LaTeX, Pandoc can teach you some.如果你不知道如何开始学习 LaTeX,Pandoc 可以教你一些。 Because you can use Pandoc interactively .因为您可以交互式地使用 Pandoc。

For LaTeX output:对于 LaTeX 输出:

It goes like this:它是这样的:

  1. Type in the terminal: pandoc -t latex .在终端中输入: pandoc -t latex
  2. Hit [RETURN] .点击[RETURN]
  3. Nothing happens.什么都没发生。
  4. Start typing a Markdown snippet into the terminal (like you would type it into a text editor).开始在终端中输入 Markdown 片段(就像在文本编辑器中输入一样)。 Say, you type a table.比如说,你键入一个表格。
  5. When your table is ready, hit [RETURN] one more time to be on a newline.当你的桌子准备好后,再按一次[RETURN]以换行。
  6. Then hit [CTRL]+[D] .然后点击[CTRL]+[D]
  7. Voila!, LaTeX code appears in the terminal window...瞧!,LaTeX 代码出现在终端窗口中......

See here:看这里:

$ 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

To be very honest with you: I didn't type the Markdown table.老实说:我没有输入 Markdown 表。 I cheated.我作弊了。 I copied it from your question and pasted it into the terminal.我从您的问题中复制了它并将其粘贴到终端中。 The last [^D] you see is when I hit [CTRL]+[D] .您看到的最后一个[^D]是当我点击[CTRL]+[D]时。

This is what appeared in the terminal window then:这就是出现在终端窗口中的内容:

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

This is the default LaTeX table code generated by LaTeX from the Markdown input.这是 LaTeX 从 Markdown 输入生成的默认 LaTeX 表格代码。

Now you can google for some methods (if you are not a LaTeX expert yet) to pimp that code in order to make the table looking nicer.现在你可以用谷歌搜索一些方法(如果你还不是 LaTeX 专家)来拉皮条代码,以使表格看起来更好。 The heavy-lifting is already done.繁重的工作已经完成。 (And if you are a LaTeX expert: it still is nice to not need to do the heavy-lifting yourself, isn't it?) (如果您LaTeX 专家:不需要自己做繁重的工作仍然很好,不是吗?)

For HTML output:对于 HTML 输出:

Of course you can do the very same to output the HTML code of a table as Pandoc would generate it.当然,您也可以像 Pandoc 生成的那样输出表格的 HTML 代码。 Look:看:

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

Isn't that nice?这不是很好吗?


[1] You may need to tell Pandoc that you want to use some of its extensions when processing the Markdown input: pandoc --from=markdown+raw_html+raw_tex+... , just in case it doesn't work from its default settings...) [1]你可能需要告诉 Pandoc 在处理 Markdown 输入时你想使用它的一些扩展: pandoc --from=markdown+raw_html+raw_tex+... ,以防它在默认设置下不起作用...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM