How to escape a pipe char in a code statement in a markdown table?
Asked Answered
T

5

162

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

Turino answered 26/6, 2013 at 12:24 Comment(0)
F
239

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

Flatways answered 26/6, 2013 at 12:43 Comment(9)
Yeah, worked using <code></code> markup! Thank you very much.Turino
Bitbucket doesn't allow <code>-blocks :-/ is there an other possibility?Fabrienne
On GitHub, the markdown escape \| works. People jump to HTML too fast without trying escaping the markdown.Gunfight
As of March 2019, in Gitlab Wikis, you still need to use "&#124;" inside the markup code.Hallowed
Still not supported by Bitbucket see bitbucket.org/site/master/issues/17106/…Beluga
Not supported in PyCharm :(Typal
mdbook only supports the <code></code> solution in 2022.Neoclassicism
In PyCharm 2022.2.4 Pro, I just used <code>a &#124;&#124; b</code> to do a a || b in a md table and it works fine in text mode and preview mode now.Bromal
I would be grateful to anyone who could explain me why GFM or other markdown specs require it to be escaped. Shouldn't code points have no special meaning to a markdown parser, when nested in a code block? Would it add too much complexity to track and check this state in a markdown parser?Cohberg
B
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)

Biernat answered 28/3, 2017 at 13:29 Comment(1)
The accepted answer appears to work fine for me in GitHub, both in a Gist and in the pull request I submitted.Nutria
N
19

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.

Nutria answered 15/7, 2017 at 19:31 Comment(2)
Thanks. This works on Github. Github MarkDown seems to recognize the string \| within a table and act accordingly.Colleen
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)Gerrard
G
3

this works fine in github markdown:

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

very similar to https://mcmap.net/q/149880/-how-to-escape-a-pipe-char-in-a-code-statement-in-a-markdown-table 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.

Gerrard answered 10/3, 2021 at 3:28 Comment(0)
A
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$ |

Apologete answered 2/10, 2023 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.