18

I am writing a tutorial on GitHub about Markdown using Markdown and I want to write ``` but rendered as inline code block like this.

3
  • 1
    So what's the answer then? I've tried ````, `````````, ``` but nothing seems to work, (even if the 3rd one seems to work here) Oct 19, 2015 at 23:21
  • Actually one backtick before and after the word should suffice. Are you really writing that markdown in a file on Github? Whats the extension of the file? How do you view it (on Github, some other tool)? Oct 19, 2015 at 23:26
  • "one backtick before and after the word": So you suggest `````. This has a weird effect. It renders 4 plain backticks (i.e. not in code block) while the 5th backtick acts as an opening backtick and all the following text is inside a code block till it finds another backtick later on. The file is an .md. I view it on GitHub and on MDwiki which renders GFM. Oct 19, 2015 at 23:32

3 Answers 3

22

The Syntax Rules are very clear about this:

To include a literal backtick character within a code span, you can use multiple backticks as the opening and closing delimiters:

``There is a literal backtick (`) here.``

which will produce this:

<p><code>There is a literal backtick (`) here.</code></p>

The backtick delimiters surrounding a code span may include spaces -- one after the opening, one before the closing. This allows you to place literal backtick characters at the beginning or end of a code span:

A single backtick in a code span: `` ` ``

A backtick-delimited string in a code span: `` `foo` ``

will produce:

<p>A single backtick in a code span: <code>`</code></p>

<p>A backtick-delimited string in a code span: <code>`foo`</code></p>

The one thing the rules don't mention, but works in the reference implementation is that the opening and closing backtick deliminators only need to be a different number of backticks than those in the code span. So if you what to have two or more consecutive backticks in a code span, then you can delimit with one backtick to open and one backtick to close the code span. The trick is using the spaces (as explained above) when the code span starts or ends with backticks.

In fact, many implementations get this right:

foo ` ``` ` bar

becomes

<p>foo <code>```</code> bar</p>

Apparently (as pointed out in a comment) a few implementations specifically require that the number of backticks in the deliminator be greater (not just different) than the number of backticks in the codespan. Putting that together with the whitespace rule, this should work on most implementations:

foo ```` ``` ```` bar

However, if neither method works with the Markdown implementation you are using, I would suggest filing a bug with the developers of that implementation. In the meantime, you can use raw HTML to force it to work:

foo <code>```</code> bar

becomes

<p>foo <code>```</code> bar</p>
2
  • Thank you for this extensive answer full of useful info. Unfortunatelly, from the examples you propose only pure html <p><code>```</code></p> works on GitHub but not on MDwiki's GFM (because it doesn't render html) and therefore I was about to file a bug. HOWEVER, reading your answer again and again I realised that you DID give the right answer even without fully realizing it: the opening and closing backtick deliminators only need to be a different number of backticks than those in the code span. So I'm gonna post it as an answer but it's only fair to tick yours as the accepted one. Oct 22, 2015 at 13:45
  • Ah, interesting. Apparently in the Markdown implementation you are using, the number of backticks in the deliminator needs to be greater than the number of backticks in the code span. It hadn't occurred to me that some implementations might include this added restriction (most don't). I''' update my answer to include that as well.
    – Waylan
    Oct 22, 2015 at 15:57
15

Waylan's answer pointed me to the right direction where it says:

the opening and closing backtick deliminators only need to be a different number of backticks than those in the code span

So I've found that using 4 backticks as opening and closing deliminators does the trick:

So, this: ```` ``` ````

renders as: ```

2

Looking at this page, it looks like one can render the meta code (chunk code) for RMarkdown/Knitr by prefixing each triple backtick with `r ''`.

So, in your RMarkdown document where you might want to finally render an example of how to set up a code chunk, you might type the following (notice the 4-space indent to render it as code):

    `r ''````{r mychunk, echo=TRUE, eval=TRUE}
    1+1
    `r ''````

and the output would be

```{r mychunk, echo=TRUE, eval=TRUE}
1+1
```

in your document (PDF, HTML, etc.). Notice it is not run, just printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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