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

11,324

Solution 1

The following CSS adds tables to your HTML output when using Pandoc:

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. 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. Something like this should work:

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

Hope that helps.

Solution 2

You can do it with 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]. If the target output format is LaTeX or PDF, it will pass these snippets unchanged into the target document.

    So if you know how to write a good looking table in LaTeX, embed it with your Markdown.

    For HTML output, this LaTeX table code will be ignored however. That's not a problem, because...

  2. Pandoc can recogniz raw HTML snippets embedded in Markdown [1]. If the target output format is HTML, it will pass these snippets unchanged into the target document.

    So if you know how to write a good looking table in HML, embed it with your Markdown.

    For LaTeX/PDF output, this HTML table code will be ignored however. 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...   :)


Trick: Use pandoc interactively in the terminal window

Here is another trick.

If you do not know how to start with learning LaTeX, Pandoc can teach you some. Because you can use Pandoc interactively.

For LaTeX output:

It goes like this:

  1. Type in the terminal: pandoc -t latex.
  2. Hit [RETURN].
  3. Nothing happens.
  4. Start typing a Markdown snippet into the terminal (like you would type it into a text editor). Say, you type a table.
  5. When your table is ready, hit [RETURN] one more time to be on a newline.
  6. Then hit [CTRL]+[D].
  7. Voila!, LaTeX code appears in the terminal window...

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. 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].

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.

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. 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?)

For HTML output:

Of course you can do the very same to output the HTML code of a table as Pandoc would generate it. 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...)

Share:
11,324
antonio
Author by

antonio

I use Emacs and R for financial research Consider to post or follow the virtualization proposal.

Updated on June 03, 2022

Comments

  • antonio
    antonio almost 2 years

    This is a sample table in 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.
    

    It does not add borders unfortunately.

    I might code it as an HTML table, but in this case it will not work in LaTeX.

    • How can I make a table with borders working both with LaTeX and HTML output?

    • If Pandoc can't do the job, is there a similar tool which is able to?

  • antonio
    antonio over 9 years
    "it does not add": sorry it was a typo, while I would like them. Please update your answer accordingly
  • CMCDragonkai
    CMCDragonkai over 8 years
    Is it possible to somehow turn markdown into PDF (in which the markdown contains embedded HTML snippets) in one go?
  • Kurt Pfeifle
    Kurt Pfeifle over 8 years
    @CMCDragonkai: If you use Pandoc, embedded HTML is only passed through to HTML-based output formats (HTML, HTML5, EPUB, EPUB3, DZSlides, Slidy, Slideous, RevealJS,...) and ignored for LaTeX or PDF output. I am not aware of any other Markdown conversion tool which will handle your request.
  • Hi-Angel
    Hi-Angel over 7 years
    A note for occasional reader: it still doesn't show borders inside the table, for that one could just set border-* variables from none to, e.g., 1px.
  • Fuhrmanator
    Fuhrmanator almost 3 years
    to pimp that code in order to make the table looking nicer -- Thanks for showing interactive mode - very cool. However, regarding borders, I can't see how, inside the markdown, one can insert LaTeX snippets to set the vertical borders. It would be in the \begin{longtable}[c]{@{}rlcl@{}} line, right, with \begin{longtable}[c]{@{}|r|l|c|l|@{}} -- the LaTeX part of your answer is finally not much use without that help.