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

<p><img src="https://i.stack.imgur.com/wLenC.png" alt="Icons"></p>

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

<pre><code>This is a block of `code`
</code></pre>

<p>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 <strong>8</strong> 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.</p>

<ol>
<li><p>This begins my list of stuff.</p>

<pre><code>Here is a block of code nested within the list
</code></pre>

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

<ol>
<li><p>This is a second-level list item, created by a four-space indentation.</p></li>
<li><p>Another second-level item</p>

<pre><code>A block of code, indicated by indenting 12 spaces.
</code></pre></li>
<li><p>Another second-level item    </p></li>
</ol></li>
<li><p>This is recognized as the second list item because it was preceded by a numeral and <em>not</em> indented.</p></li>
</ol>

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

<p>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.  </p>

<hr>

<h2>Advanced Techniques</h2>

<h3>Syntax highlighting</h3>

<p>If the question is tagged with <a href="https://stats.stackexchange.com/questions/tagged/r" class="post-tag" title="show questions tagged &#39;r&#39;" rel="tag">r</a>, 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 <em>each</em> code block with the magic line</p>

<p><code>&lt;!-- language: lang-R --&gt;</code></p>

<p>You will have to add this HTML comment before <em>every</em> block of <code>R</code> code you want highlighted.  On the other hand, if you will have more than one block of code, and you want <code>R</code> syntax highlighting for all of them, use <code>&lt;!-- language-all: lang-r --&gt;</code> instead (see <a href="https://meta.stackexchange.com/a/184109/187282">here</a> for more information).  </p>

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

<p>Other languages are treated similarly: replace "R" by the name of the language.</p>

<h3>Interactions with MathJax</h3>

<p>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:</p>

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

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

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

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