7
$\begingroup$

This is probably a very simple question and I am probably not using the right keywords in my search; but I have a long expression that I would like to break across multiple lines to improve readability.

I believe this can be done with a \ at the end of the line to be continued but want to be sure.

$\endgroup$
11
$\begingroup$

When Mathematica encounters an incomplete expression, it will just continue reading on the next line. For example,

1 +
2

is the same as 1+2.

However,

1
+ 2

is interpreted as two distinct inputs, 1 (which is complete) and +2.


As you mentioned, \ can also be used at the end of the line to indicate continuation. So

1 \
+ 2

is also equivalent to 1+2. However, this doesn't seem to work when used in a notebook cell in the front end. Apparently it only works when running Mathematica in command line mode.

$\endgroup$
  • 1
    $\begingroup$ I think in cases of most (if not all) infix operators, linkbreak should only happen after the operator. (+1) $\endgroup$ – Silvia Nov 29 '14 at 18:23
  • 1
    $\begingroup$ Not only does the backslash not produce line continuation in a notebook, neither does the line continuation character [Esc]cont[Esc], and they are not even handled in the same way. $\endgroup$ – Alan Oct 4 '16 at 17:09
7
$\begingroup$

For completeness, the issue of the interpreter prematurely thinking a line end completes an expression is avoided by the use of enclosing parentheses:

(
1
+ 2
)

3

This may be safer than taking care about where you break lines. (And maybe you like the way it looks with the operators out front)

This applies inside any other construct as well, eg this works as expected:

 Table[
     i
     + 2 
         , {i,2}]

{3,4}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.