Using CODE Tags

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
A common mistake many members make is pasting their code into their post without properly formatting it. Especially when the program is long, this makes the entire post difficult to read and it is easy to lose track of the poster’s comments.

Fortunately, there is a simple way to eliminate this problem. Someone may have mentioned code tags.

This excerpt from the Help pages under “BB codes” introduces the method.
[CODE] - Programming Code Display
Displays text in one of several programming languages, highlighting the syntax where possible.
Example:General code:
[CODE]General
code[/CODE]

The easiest way to include a code tag is to use the menu. Click on the three dots on the menu bar (on my system, they are in the upper left). A drop-down menu appears and you should click on “</> Code”.
37B5E41E-61B4-4299-8CEB-286F5C6D87DD.jpeg
There are several fields on the resulting page which are optional except the text block at the bottom. This is where you paste your program code. The other field I may use occasionally is “Language:”, which appropriately formats your code based on its language.

Hope you find this useful next time you are posting your program code.
 
Last edited by a moderator:

KeithWalker

Joined Jul 10, 2017
3,091
I wondered how to present programs correctly in this forum. Thank you for the information. The following is my first use of code tags, just to see how it works:


Code:
/* Handshaking between two arduinos.
   Connect 9 on one to 10 on the other and vica-versa
   Which ever one completes its loop first will wait for the other to finish
   before they both start again.
 */
void setup() {
 
  pinMode(9, INPUT);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  pinMode(13, OUTPUT);
}
  int i ;

void loop() {
    digitalWrite(10, HIGH);
    while (digitalRead(9) == (LOW));// Wait until 9 goes HIGH
    delay (10);
    digitalWrite(10, LOW);

  // Blink 6 times
  for(i = 0; i < 6; i++)   
  {digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500); }
}
 

MrChips

Joined Oct 2, 2009
30,802
If you ever need to demonstrate to someone how to use code tags such as in this example:

[code] ... your code goes here ... [/code]

enclose your demo with plain tags:

[plain][code] ... your code goes here ... [/code][/plain]
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
If you ever need to demonstrate to someone how to use code tags such as in this example:

[code] ... your code goes here ... [/code]

enclose your demo with plain tags:

[plain][code] ... your code goes here ... [/code][/plain]
I’ve been trying to figure that out! Thanks. I’m editing my original post.

UPDATE: Didn’t work for me. I tried before and inside the QUOTE tag.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,802
I’ve been trying to figure that out! Thanks. I’m editing my original post.

UPDATE: Didn’t work for me. I tried before and inside the QUOTE tag.
I cannot see what you tried.

Just so that I can see what you are doing, use ( ) braces instead:

(plain)(code) ... your code goes here ... (/code)(/plain)
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,163
I cannot see what you tried.

Just so that I can see what you are doing, use ( ) braces instead:

(plain)(code) ... your code goes here ... (/code)(/plain)


I tried two different ways. First:

This excerpt from the Help pages under “BB codes” introduces the method.
{QUOTE}{CODE} - Programming Code Display
Displays text in one of several programming languages, highlighting the syntax where possible.
Example:General code:
{plain}{CODE}General
code{/CODE}{/plain}
{/QUOTE}
And then (and didn’t expect this to work. I was right):

This excerpt from the Help pages under “BB codes” introduces the method.
{plain}{QUOTE}QUOTE}{CODE} - Programming Code Display
Displays text in one of several programming languages, highlighting the syntax where possible.
Example:General code:
{plain}{CODE}General
code{/CODE}
{/QUOTE}{/plain}
Note the first code tag was cut & pasted from the Help page.
 

KeithWalker

Joined Jul 10, 2017
3,091
To get it to work, I just opened an old arduino program, copied the contents and pasted it into the code area right after the "1" line number.
 

MaxHeadRoom

Joined Jul 18, 2013
28,684
Using Assembly.
[CODE = nasm

Netwide Assembler:
Set_PPS2
        banksel RA1PPS
        movlw   0x02
;        movwf   RA1PPS
        return      
      
dly_2   
; Delay = 2 seconds

        movlw    0x03
        movwf    d1
        movlw    0x18
        movwf    d2
        movlw    0x02
        return
 

WBahn

Joined Mar 31, 2012
30,052
There are several languages for which syntax highlighting is supported. C, javascript, assembly, and a few others. I've never been able to find a list. The Help topic here is out of date in this (and many) regards.

You can also put code snippets, such as x = sqrt(y);, inline by using [ICODE]x = sqrt(y);[/ICODE]
 

Ya’akov

Joined Jan 27, 2019
9,150
This excerpt from the Help pages under “BB codes” introduces the method.
Hey, @djsfantasi—I edited your post to use [PLAIN] tags like this:

This excerpt from the Help pages under “BB codes” introduces the method.

[QUOTE]
[PLAIN]​
[CODE] - Programming Code Display​
Displays text in one of several programming languages, highlighting the syntax where possible.​
Example: General code:​
[CODE]General code[/CODE]​
[/PLAIN]​
[/QUOTE]

The indentation is to make it more readable. You probably don't want to know the tag gymnastics I went through to get that to display correctly. Let's just say it wasn't easy.
 

MrChips

Joined Oct 2, 2009
30,802
In essence, this has drifted way off topic from the original intent of the thread.

The primary intent was to show how to embed programming code between opening and closing code tags.

[code] ... your program code ... [/code]

The secondary post was how to show the above example without actually invoking the code tags by embedding in between plain tags.

[plain][code] ... your program code ... [/code][/plain]

Let's leave it at that.
 

MrChips

Joined Oct 2, 2009
30,802
On another note, we often use the variable i as the index of an array.
This becomes interpreted as the italics tag by the BBcode analyzer. You will have to use a different variable when indexing arrays and don't use any other variable such as b, u, s that would be considered another format tag.
 
Top