12
$\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$
0

3 Answers 3

14
$\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$
2
  • 1
    $\begingroup$ I think in cases of most (if not all) infix operators, linkbreak should only happen after the operator. (+1) $\endgroup$
    – Silvia
    Commented Nov 29, 2014 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
    Commented Oct 4, 2016 at 17:09
11
$\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$
2
$\begingroup$

You may type "esc" + "cont" + "esc" then you could type "Enter" key. https://reference.wolfram.com/language/ref/character/Continuation.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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