165

On GitHub I want to build a table containing pieces of code in Markdown. It works fine except when I put a pipe char (i.e. | ) between the backtick (i.e. ` ) chars.

Here is what I want:

      a     |  r  
------------|-----
 `a += x;`  |  r1
 `a |= y;`  |  r2

The problem is that the vertical bar in the code statement of the second line is interpreted as a column delimiter. Then the table rendering looks pretty ugly. How could I avoid that?

Note that I already tried to use the | HTML code, but it produces a |= y;.

5 Answers 5

242

As of March 2017 using escaped pipes is much easier: \| See other answers.

If you remove the backticks (`), using the | hack works

      a     |  r  
------------|-----
 `a += x;`  |  r1
 a |= y;  |  r2

and produces the following output

enter image description here

Alternatively, you can replace the backticks (`) with a <code></code> markup which fixes the issues more nicely by preserving the rendering

      a     |  r  
------------|-----
 `a += x;`  |  r1
<code>a &#124;= y;</code>  |  r2

generating the following output

enter image description here

9
  • 8
    Yeah, worked using <code></code> markup! Thank you very much. Commented Jun 26, 2013 at 13:07
  • 1
    Bitbucket doesn't allow <code>-blocks :-/ is there an other possibility?
    – Sergej
    Commented Feb 3, 2016 at 8:39
  • 4
    On GitHub, the markdown escape \| works. People jump to HTML too fast without trying escaping the markdown.
    – jnm2
    Commented Jul 14, 2017 at 21:19
  • 1
    As of March 2019, in Gitlab Wikis, you still need to use "&#124;" inside the markup code.
    – knb
    Commented Mar 22, 2019 at 13:52
  • Still not supported by Bitbucket see bitbucket.org/site/master/issues/17106/… Commented Jul 12, 2019 at 15:57
36

As of mid-2017, the pipe may simply be escaped with a backslash, like so: \|

This works both inside and outside of backticks.

The HTML code may now be used again, too, but only outside of backticks.

Previous answer:

As of March 2017, the accepted answer stopped working because GitHub changed their markdown parser. Using another unicode symbol that resembles a pipe seems to be the only option right now, e.g.:

ǀ (U+01C0, Latin letter dental click)

∣ (U+2223, Symbol divides)

⎮ (U+23AE, Integral Extension)

1
  • 3
    The accepted answer appears to work fine for me in GitHub, both in a Gist and in the pull request I submitted.
    – Iiridayn
    Commented Jul 14, 2017 at 19:44
20

You can escape the | in a table in GFM with a \ like so:

|      a     |  r  
|------------|-----
| `a += x;`  |  r1
| `a \|= y;` |  r2

See https://github.github.com/gfm/#example-191 or https://github.com/dotnet/csharplang/pull/743 for an example.

2
  • 1
    Thanks. This works on Github. Github MarkDown seems to recognize the string \| within a table and act accordingly. Commented Nov 26, 2018 at 12:09
  • 1
    in github, this requires a leading | in 1st column: this works: ``` | a | r | ------------|----- | a += x; | r1 | a \|= y; | r2 ``` (sorry for the formatting due to comment 1-line formatting rules) Commented Mar 10, 2021 at 3:25
3

this works fine in github markdown:

|       a     |  r  
| ------------|-----
|  `a += x;`  |  r1
|  `a \|= y;` |  r2

very similar to https://stackoverflow.com/a/45122039/1426932 but with added | in first column (it didn't render well in comments so I'm adding an answer here).

note that outside a table cell, a \|= y; will render the backslash, but inside a table cell, it won't.

0

For anyone looking for an RMarkdown / pandoc based solution with LaTeX output, the simplest solution seems to be to define a new command outside of the table, like so:

\newcommand{\pipe}{|}

|     a      |         r                  |
|------------|----------------------------|
| $a\pipe_1$ | first element of $a\pipe$  |
| $a\pipe_2$ | second element of $a\pipe$ |

Not the answer you're looking for? Browse other questions tagged or ask your own question.