1

You can do this (preferred):

<!-- language-all: lang-arduino -->

or this:

<!-- language-all: lang-cpp -->

to the top of every question or answer to set syntax highlighting for that entire question or answer, or this just for the code block that follows:

Code block 1: 
<!-- language: lang-arduino -->

    void setup()
    {
        Serial.begin(115200); 
        // some more code
    }

Code block 2:
<!-- language: lang-bash -->

    # random examples
    arduino-cli help core
    grep 'some regular expression' /some/file
      

but shouldn't this be turned on by default for the entire Arduino Stack Exchange instead?

Samples:

Here's what the above 2 code blocks look like, just for demonstration purposes:

Code block 1:

void setup()
{
    Serial.begin(115200); 
    // some more code
}

Code block 2:

# random examples
arduino-cli help core
grep 'some regular expression' /some/file
      

References:

  1. Syntax highlighting language hints

2 Answers 2

2

I completely agree that Syntax colouring makes the code much more readable, particularly for people who are new to coding.

I usually add the C++ language directive just above the code block to activate syntax-highlighting for the code:

<!-- language-all: lang-C++ -->

For serial monitor output, error messages and the like, you do not want syntax highlighting (but you do want it to display in the grey "code" box). You can disable the highlighting by placing this above the code block:

<!-- language-all: lang-none -->

Alternatively you could use backticks to indicate the start and end of a code block:

```cpp  
void setup()
{
    Serial.begin(115200); 
    // some more code
}
```  

will look like this

void setup()
{
    Serial.begin(115200); 
    // some more code
}

For sections where you do not want syntax highlighting, you can disable the highlighting by placing a normal code block.


The advantage of the tag is that both the question and the answer get syntax hylighting without needing to add any directives.

1

Yes, but what language? Your examples shows two: C++ and bash.

Some code blocks show output from the terminal monitor which should not (generally) be coloured at all.

If you tag your question with the C++ tag that it gets the C++ (ie. Arduino) colouring automatically.

5
  • I was just thinking we by default do Arduino (C++) syntax highlighting, and require manually setting it for anything else. Alternatively, I suppose we could just tag every question with Arduino code in it with the C++ tag? Or, what is preferred? Aug 6, 2020 at 7:16
  • Some people even post (ugh.....) python.
    – Majenko
    Aug 6, 2020 at 8:45
  • 1
    One thing that I have often wondered about... do the question tags affect the syntax highlighting in answers...?
    – Majenko
    Aug 6, 2020 at 8:46
  • 1
    It appears to, yes. See here as an example. Neither the question nor the answer have the special "language" comments, but both are syntax-highlighted (and the question has the C++ tag).
    – Nick Gammon Mod
    Aug 6, 2020 at 22:20
  • 1
    What is preferred? - I usually tag questions with code in them with the C++ tag, but have been ticked off in the past because the "question is not about C++". Perhaps, but if it has C++ code in it I think the tag is reasonable. After all, people who want to (or are able to) answer questions about C++ syntax may well search for such tags.
    – Nick Gammon Mod
    Aug 6, 2020 at 22:22

You must log in to answer this question.

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