I am a new member of this group. How can I write the R code while commenting or posting? I read also the previously asked questions related to this but don't get my answer.


1 Answer 1


When you are writing a question or an answer, a set of icons is visible above the text field.


One icon is a pair of curly braces, $\{\}$. To add a block of code, highlight the block and click the curly braces.

This is a block of `code`

You will see that the curly braces are just adding four spaces at the beginning of each line. When you have a block of code within a list you will need 8 spaces (four to keep the list indentation intact and four more for the code highlighting), which you will have to add manually. Deeper indentations can be handled similarly.

  1. This begins my list of stuff.

    Here is a block of code nested within the list

    This is a continuation of the first list item, indicated by indenting it four spaces.

    1. This is a second-level list item, created by a four-space indentation.

    2. Another second-level item

      A block of code, indicated by indenting 12 spaces.
    3. Another second-level item

  2. This is recognized as the second list item because it was preceded by a numeral and not indented.

If you want to mark a small part within a sentence as code, surround it with backticks (`). For example, this is formatted as code within this sentence.

If you need to add backticks within your code, they will be fine within blocks of code (as seen above), but to add them inline, use the escape character \ immediately before each.

Advanced Techniques

Syntax highlighting

If the question is tagged with , then the code will be syntax highlighted (although not very well...). When the question does not have this tag and you want the code highlighting, then preface each code block with the magic line

<!-- language: lang-R -->

You will have to add this HTML comment before every block of R code you want highlighted. On the other hand, if you will have more than one block of code, and you want R syntax highlighting for all of them, use <!-- language-all: lang-r --> instead (see here for more information).

This should not be indented. If all works correctly, this line will be invisible and the code will be highlighted (perhaps after a slight delay).

Other languages are treated similarly: replace "R" by the name of the language.

Interactions with MathJax

The MathJax delimiter for $\TeX$ markup is the dollar sign \$. Unfortunately, MathJax attempts to process text within code blocks. When it encounters one dollar sign it looks for the second--and creates indentation problems in the code:

x <- list(a=1, b=2)
y <- x$a
z <- x$b # This line is *not* indented originally: MathJax is doing it!

One work-around is to append a little \$-containing comment after each offending line, as in

x <- list(a=1, b=2)
y <- x$a # $ This dollar sign terminates the MathJax processing.
z <- x$b # This line is now indented correctly.

The other work-around is to de-indent the offending line by 4 spaces.

  • $\begingroup$ Boy, my answer sure is informative now... $\endgroup$ Commented Feb 13, 2015 at 18:15
  • 1
    $\begingroup$ I've often wondered what was causing that indentation issue!! If bounties were available on Meta, I'd give a +50 for that. $\endgroup$
    – Silverfish
    Commented Feb 13, 2015 at 19:05
  • $\begingroup$ @Silverfish, FTR I have considerably more rep than I need. At any rate, that part of 'my' answer was written by whuber, who is also the person who figured this out originally (see here). $\endgroup$ Commented Feb 13, 2015 at 23:04
  • $\begingroup$ Well, many thanks to @whuber then. I've quite often spent time re-re-rewritting my R code until finally, magically, the indentation returned to normal. Now I understand what's going on, a considerable frustration will be eased! $\endgroup$
    – Silverfish
    Commented Feb 13, 2015 at 23:11
  • $\begingroup$ Thanx @gung it realy work.How can I do the same in comments? as I asked in the second part of my question. $\endgroup$ Commented Feb 16, 2015 at 4:54
  • $\begingroup$ @sumairasarwar, you use the backticks as I discussed; you cannot have code blocks in comments. $\endgroup$ Commented Feb 16, 2015 at 14:33
  • $\begingroup$ ok thanx @gung. $\endgroup$ Commented Feb 17, 2015 at 7:29
  • $\begingroup$ @sumairasarwar, you're welcome. If this has resolved your question, you may want to accept it by clicking the check mark to its left. $\endgroup$ Commented Feb 17, 2015 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .