7
$\begingroup$

I just want to add a computer program to my answer. However all my attempts to copy it result in total garbage displayed because all "less" and "greater" signs are perceived as HTML markers. I tried to type in the "pre" and "code" HTML tags myself but it just doesn't work if you try to paste between the opening and the closing tags. Here is the typical result:

for(int m=0; m

I feel like I'm just stupid because many people have posted computer programs all over the place but when I click the "edit" button on their posts, I do not see any clues as to how it actually hangs there :-(. I also tried to search old questions but apparently used wrong key words. Can anybody help? Thanks in advance!

$\endgroup$
1
  • 2
    $\begingroup$ An experiment to try is to indent all the lines in the snippet by four spaces. That might be recognized as a code block. Gerhard "Happy Fourth By The Way" Paseman, 2019.07.04. $\endgroup$ Commented Jul 4, 2019 at 16:56

2 Answers 2

8
$\begingroup$

As explained in the editing help, in MarkDown you can achieve preformatted text by using four spaces indentation.

To achieve this after typing (or copy pasting) the code, simply select the relevant part and the click on the code button in the editor (between blockquote and insert image) or press Ctrl+K.

To test this I have taken an example of for cycle in C from this website. (Basically a random site that I got after searching for "for cycle c example".)

#include <stdio.h>
int main()
{
   int i;
   for (i=1; i<=3; i++)
   {
       printf("%d\n", i);
   }
   return 0;
}

You can find much more detailed information in the FAQ post: How do I format my code blocks? Here is a screenshot which I copied from that post:

Code blocking icon properly highlighted


Since you explicitly mentioned <code>, the FAQ post says about this possibility: "Embed within <code> tags, and manually encode HTML entities: `` <html> `"

So if you prefer this for some reason this solution, it is also possible, although with some additional work. (In the above excerpt I had to use &num; for #, &lt; for < and &gt; for >.)

#include <stdio.h> int main() { int i; for (i=1; i<=3; i++) { printf("%d\n", i); } return 0; }

$\endgroup$
1
  • 2
    $\begingroup$ Thanks a lot, Martin! You see, my problem is that I'm an old-fashioned guy, who can understand only trivial stuff like HTML and javascript. The modern dragging and clicking techniques are way above my head. Needless to say, at school I only learned how to count sticks, cubes, and clouds. Counting empty spaces on a blank line still perplexes me a lot :-). Thanks again for helping me to survive in this brave new world! $\endgroup$
    – fedja
    Commented Jul 4, 2019 at 19:51
2
$\begingroup$

Here I typed some code, then selected it, then clicked on the {} icon (preformatted text), and the result looks like this:

for(int m=0; m < N; m++):
<<&etc.>>
$\endgroup$

You must log in to answer this question.

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