How do I include Matlab source code in forum posts?

As discussed in this forum post, the CVX Forum uses the Markdown language to render text. If you want to insert a line of code in the middle of a paragraph, just surround the text by back-quotes: e.g., typing `cvx_begin sdp` in the editing window produces the result: cvx_begin sdp.

For a longer, multi-line codeblock, Markdown requires the following:

  • include a blank line before and after the code block
  • indent each line of the code block by four (4) spaces.

Clearly, the indentation step is a bit inconvenient to do by hand, especially for a long code block. Fortunately, the CVX Forum editing window makes the process dead simple. Simply type in (or cut and paste) your code block, select it, and click on the “code” button on the editing toolbar—the button that looks like this: </>

Here’s an example of the results:

m = 20; n = 10; p = 4;
A = randn(m,n); b = randn(m,1);
C = randn(p,n); d = randn(p,1); e = rand;
cvx_begin
    variable x(n)
    minimize( norm( A * x - b, 2 ) )
    subject to
        C * x == d
        norm( x, Inf ) <= e
cvx_end

Notice that the internal indentation of the code block has been preserved.

For a more complex example, I’ve included the entire Markdown source of the above text in a code block below, so you can see all of the commands and tricks I used to get the above results.

As discussed in [this forum post](/t/how-do-i-format-forum-posts/17/1), the CVX Forum uses the Markdown language to render text. If you want to insert a line of code in the middle of a paragraph, just surround the text by back-quotes: *e.g.*, typing \`cvx_begin sdp\` in the editing window produces the result: `cvx_begin sdp`.

For a longer, multi-line codeblock, Markdown requires the following:

* include a blank line before and after the code block
* indent each line of the code block by four (4) spaces.

Clearly, the indentation step is a bit inconvenient to do by hand, especially for a long code block. Fortunately, the CVX Forum editing window makes the process dead simple. Simply type in (or cut and paste) your code block, select it, and click on the "code" button on the editing toolbar&mdash;the button that looks like this: `</>`

Here's an example of the results:

    m = 20; n = 10; p = 4;
    A = randn(m,n); b = randn(m,1);
    C = randn(p,n); d = randn(p,1); e = rand;
    cvx_begin
        variable x(n)
        minimize( norm( A * x - b, 2 ) )
        subject to
            C * x == d
            norm( x, Inf ) <= e
    cvx_end

Notice that the *internal* indentation of the code block has been preserved.

As discussed in this forum post), the CVX Forum uses the Markdown language to render text. If you want to insert a line of code in the middle of a paragraph, just surround the text by back-quotes: e.g., typing `cvx_begin sdp` in the editing window produces the result: cvx_begin sdp.

For a longer, multi-line codeblock, Markdown requires the following:

  • include a blank line before and after the code block
  • indent each line of the code block by four (4) spaces.

Clearly, the indentation step is a bit inconvenient to do by hand, especially for a long code block. Fortunately, the CVX Forum editing window makes the process dead simple. Simply type in (or cut and paste) your code block, select it, and click on the “code” button on the editing toolbar—the button with 1s and 0s on it.

Here’s an example of the results:

m = 20; n = 10; p = 4;
A = randn(m,n); b = randn(m,1);
C = randn(p,n); d = randn(p,1); e = rand;
cvx_begin
    variable x(n)
    minimize( norm( A * x - b, 2 ) )
    subject to
        C * x == d
        norm( x, Inf ) <= e
cvx_end

Notice that the internal indentation of the code block has been preserved.

For a more complex example, I’ve included the entire Markdown source of the above text in a code block below, so you can see all of the commands and tricks I used to get the above results.

As discussed in [this forum post](/t/how-do-i-format-forum-posts/17/1)), the CVX Forum uses the Markdown language to render text. If you want to insert a line of code in the middle of a paragraph, just surround the text by back-quotes: *e.g.*, typing \`cvx_begin sdp\` in the editing window produces the result: `cvx_begin sdp`.

For a longer, multi-line codeblock, Markdown requires the following:

* include a blank line before and after the code block
* indent each line of the code block by four (4) spaces.

Clearly, the indentation step is a bit inconvenient to do by hand, especially for a long code block. Fortunately, the CVX Forum editing window makes the process dead simple. Simply type in (or cut and paste) your code block, select it, and click on the "code" button on the editing toolbar&mdash;the button with `1`s and `0`s on it.

Here's an example of the results:

    m = 20; n = 10; p = 4;
    A = randn(m,n); b = randn(m,1);
    C = randn(p,n); d = randn(p,1); e = rand;
    cvx_begin
        variable x(n)
        minimize( norm( A * x - b, 2 ) )
        subject to
            C * x == d
            norm( x, Inf ) <= e
    cvx_end

Notice that the *internal* indentation of the code block has been preserved.