Some discussion about formatting code blocks in posts


#1

@ScruffR,

Thanks for the link to formatting, good stuff there even for folks who have been posting for a while.

One question, the instructions say you need to wrap code samples in three elipses and “cpp” but as you can see below, the cpp part does not seem to make a difference. Has the system changed since the original post was made or am I doing this wrong?

Serial.println(“With cpp”);
Serial.println(“Without cpp”);

Thanks, Chip


Bummer...Failed to call
Forum Tips and Tricks
Electron reading digital pin state help request (novice)
#2

These are no ellipses (commonly written with three consecutive dots ...) but three consecutive accent grave

 ``` (as opposed to ...)

So done correctly your code would look like this

  Serial.println("With ```cpp");

and

  Serial.println("With only ```");

or

  Serial.println("With ```text");

But you are actually using the correct symbols (accent grave) - the problem tho’ is that you also have extra text following the tag (i.e. a blank).
The special format tags need to be all alone on their line without any extra characters (visible or not) leading or trailing.


#3

@ScruffR,

Thank you for the explanation and for correcting my misuse of eclipses. I guess I was doing it wrong. :wink:

So, the corrected example looks like this:

Serial.println("With cpp");
Serial.println("Without  cpp");

It seems like the line without “cpp” highlighted Serial and the Text so, is this better?

Chip


#4

Along with cpp you can also use other language hints (e.g.html, java, …) and depending on whichever you use code highliting might reflect the actual types of expressions better than others.
If you don’t provide a hint to the rendering engine, the highlighting may look better for one statement but get confusing for another.
So it’s up to preference and as a rule of thumb, after submitting the post checking how it turned out should become a habit and if you are not satisfied, you can always edit/tweak your own posts to better reflect your intent.

For example

// with cpp
int i = 10;
Serial.printlnf("some literal with number %d", i);
if (someCondition == true) 
  someCondition = false;

or

// with text
int i = 10;
Serial.printlnf("some literal with number %d", i);
if (someCondition == true) 
  someCondition = false;

or

// without but due to the content the engine makes a good educated guess 
//   but you never know beforehand ;-)
int i = 10;
Serial.printlnf("some literal with number %d", i);
if (someCondition == true) 
  someCondition = false;

(note Serial is now rendered differently than before with only one line provided)


I have now split this discussion off the original topic since it didn’t contribute to the original problem of
Electron reading digital pin state help request (novice)


#5

Good to know adding cpp provides extra formatting features.

I thought it was just simplified to just needing at the top and at the bottom.

I usually always had to try a few times to get the added cpp to work for some reason.


#6

Probably due to this


#7

Weird, I never put the cpp on it’s own line any time I got it to work.


#8

I wasn’t saying - or at least didn’t mean - to put cpp on its own line but the grave accents and the cpp
Like

 ```cpp  <-- nothing before and after these six characters (these six form the opening tag)
 ...
 ```     <-- nothing before and after these three characters (these three form the closing tag)

#9

OK, yes that clarification makes more sense.

But yes I always tried to add the cpp like the top line and it always took multiple attempts to get it work correctly for some reason. Not much to mess up, it’s pretty simple.

Then I saw that just using the 3 slashes at the top and bottom also worked every time so I started using that instead but not I know the CPP adds more formatting features so I’ll stick with that.