TUTORIAL: How to format your question

6 views (last 30 days)
Andrew Newell
Andrew Newell on 20 May 2011
Edited: Andrew Newell on 25 Mar 2015
A lot of questions include code that looks like this:
A = []; for ii = 1:10 A = [A ii]; end
This makes it difficult for people who are trying to answer the question because they have to reformat the code before running it.
Many questions also have text that looks like this.
When you are composing a question, you can click on Markup help to find out how to format your code properly. However, many people may not understand how to use this formatting because there are no examples. In this tutorial I will provide some examples, but first I'll state the most important point:
Look at the preview window!
The Preview window is below the window you are typing your question into. Make sure your browser is scrolled down far enough to see it.
Now let's go back to the badly formatted code. Add two spaces at the start of the first line and it should look like this in the Preview window:
A = [];
for ii = 1:10
A = [A ii];
end
You don't need to indent after every blank line, but you do need to indent every time there is a blank line between lines of code. For example, if you type something that looks like this:
A = [];
for ii = 1:10 A = [A ii]; end
it is because you have a blank line between the first command and the for loop. You should indent the for two spaces to get this:
A = [];
for ii = 1:10
A = [A ii];
end
Code markup has (at least) one undocumented feature. Suppose you like to format loops so the code inside is indented. If you put two spaces in front of A = [A ii];, you'll see no difference. You need to indent the code two spaces plus the number of spaces you want to see. Indent four spaces and you'll get this:
A = [];
for ii = 1:10
A = [A ii];
end
Finally, if you see text that looks like this, get rid of the spaces at the beginning of the line.
To repeat, the bottom line is: Look at the Preview window!
EDIT: If you have multiple lines of code with some blank lines in the middle, it will be easier to just highlight the code with your mouse and press the {} Code button.
  3 Comments
Andrew Newell
Andrew Newell on 20 May 2011
Naturally, I think that only those people who have voted for this page should be allowed to ask a question!
Seriously, I don't think we can force people to read it, but we can add a comment linking to it. A lot of people (including me) include links to Oleg's tutorial already.
Jan
Jan on 22 Jul 2012
I bump this thread hoping, that more newcomers will read it.

Sign in to comment.

Accepted Answer

Andy
Andy on 20 May 2011
Is this tutorial supposed to be just about how to format code, or is it how to use formatting in general? If the latter, then there should probably be something about using links to helpful documents, or
  • Creating
  • bulleted
  1. or numbered
  2. lists,
not to mention bold or italic text.
  1 Comment
Andrew Newell
Andrew Newell on 20 May 2011
I decided to focus on formatting code, because that is the most important issue and it gives people the most trouble. However, answers like yours could explore the other things that can be done.

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!