-2

I have a coding question and I want to paste the code that I have written into my question.

I have seen several examples of such formatting, but I can't find how do I manage to do the same and there doesn't seem to be any answered questions like mine.

Problem:

Can't paste code into stack exchange and have it look like code in a code editor

  • 3
    you've done it before – Memor-X Apr 5 '17 at 5:29
  • 1
    @Memor-X Sometimes it is accidental, when your code already is indented enough to take the shape of Code block on SE just by pasting it – NSNoob Apr 5 '17 at 5:42
  • @NSNoob you have a point there. though i still think if you have a working example you'd fiddle with it first comparing to what you want to look the same since there is a post preview, but maybe that's just me – Memor-X Apr 5 '17 at 5:46
  • Hey thanks for finding one of my other questions, I sincerely appreciate it because I really couldn't find, I went to look at my previous question and SE wasn't showing a question that I recognize as on to do with code so I asked my question here, thanks this is not a sarcastic thanks I really appreciate the help. – GBikeZ8 Apr 5 '17 at 7:38
-2

What I've seemed to found out is that based on the format or the way the code is arranged before you paste into SE or SO is what detemines how the code looks in the question.

eg. here is the code from a question I asked about coding in XNA

now here is the the exact same code copied pasted after I delete a few indents Original copy and paste

original: protected override void Update(GameTime gameTime) {

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    this.Exit();
MouseState current_mouse = Mouse.GetState();
int mousep1 = current_mouse.X;
int mousep2 = current_mouse.Y;
if (current_mouse.X >= 0)
{
    backcolor = Color.Green;
}
base.Update(gameTime);

}

Original with indents deleted after paste

protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); MouseState current_mouse = Mouse.GetState(); int mousep1 = current_mouse.X; int mousep2 = current_mouse.Y; if (current_mouse.X >= 0) { backcolor = Color.Green; } base.Update(gameTime); }

My conclusion is the website is trying to determine whether you pasted code or not based on the formatting of the input, and my formatting was not correct. My answer may be similar to what has already been said in the comments, but I just wanted to clarify for my self and for anyone else.

  • 3
    The site doesn't use some sort of vague heuristic guesswork for formatting. It follows fairly standard Markdown patterns, and, in particular, the first level of indentation is used to make a code block, as long as there's a blank line before it. So yes, if you remove the formatting instructions, the format stops working. That's all that's going on in your pair of examples. – Nathan Tuggy Apr 5 '17 at 8:11
  • Sorry for the wording of my answer I'll fix it – GBikeZ8 Apr 5 '17 at 8:14
  • Also something that seemed to work is to paste the code and then press the code btn. on the tool bar. – GBikeZ8 Apr 6 '17 at 0:52

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