44

Is it possible to highlight or emphasize a part of code that's inside backticks or:

a code block

?

Would be useful for pointing to what's changed, but the FAQ entry doesn't say anything about this.

3

4 Answers 4

53

It's possible with HTML

1) A code block

Emphasis on this
<pre><code>Emphasis on <b>this</b>
</code></pre>

2) Inline code: Emphasis on this

Inline code: <code>Emphasis on <b>this</b></code>

Depending on the code, you'd need HTML entities to render it ok.

6
  • 5
    Hey, that's great! It even seems to work with syntax highlighting.
    – otus
    Commented Jun 13, 2014 at 14:53
  • The workaround works well for small code blocks but not (anymore?) for multiple lines of (PowerShell) code. I get a red comment: Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon. and I am not able to submit it due to: Your edit couldn't be submitted. Please see the error above.
    – iRon
    Commented Jun 29, 2017 at 8:23
  • ...unless of cause I also keep the code formatting which consequently causes redundant indents and an empty line at the top.
    – iRon
    Commented Jun 29, 2017 at 8:31
  • Although this may have worked in the past, it doesn't appear to work today.
    – Grismar
    Commented Dec 1, 2021 at 0:10
  • @Grismar, I'm seeing it all right (¿?)
    – brasofilo
    Commented Dec 1, 2021 at 4:18
  • In that case, maybe it's dependent on theme settings or something of the sort. When I do it, the code formatting seems to override the bold, and it all shows as normal.
    – Grismar
    Commented Dec 2, 2021 at 1:00
13

No you can't do that. If you use backticks or four spaces to show code then exactly what you type gets rendered.

So if you try to bold something:

I want this bold

you'd actually get this:

I want **this** bold

The only way to emphasise something would be to use comments to point out what's at fault as this won't mess with the syntax highlighting:

if (parameter == null) then
{
    parameter.Method(); // <-- It's this that goes wrong
}
4
  • 1
    And put it in a comment in case it messes with the syntax highlighting (because it's obviously a syntax error).
    – BoltClock Mod
    Commented Jun 13, 2014 at 9:38
  • 4
    for small errors I've occasionally put a comment line underneath with // ^^^ pointing to the specific characters
    – OGHaza
    Commented Jun 13, 2014 at 9:50
  • 1
    @OGHaza - that would work too. I just wanted a simple illustration.
    – ChrisF Mod
    Commented Jun 13, 2014 at 9:51
  • Ok, thanks. Too bad, but I suppose using a comment works all right.
    – otus
    Commented Jun 13, 2014 at 9:55
5

If you build on @ChrisF's solution you can have a full code block if you format something like this

<!-- language: lang-html -->

<pre><code>&lt;div&gt;
    &lt;input name="someInput" value=<b>"highlight"</b>&gt;
&lt;/div&gt;
</code></pre>

Which would produce:

<div>
    <input name="someInput" value="highlight">
</div>

More information can be found in this answer

-1

Though this is not completely answering the question still it is possible at least to emphasize or make bold the whole text inside of backticks without the need for HTML markup simply by surrounding the backticks by * or **. Check the code of this answer to get the picture.

0

You must log in to answer this question.

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