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.
share improve this question  follow
asked
Nov 29 '14 at 17:20

Doug Kimzey
1,975\u25cf1010 silver badges\u25cf1616 bronze badges edited
Dec 14 '15 at 14:52
 
J. M.'s technical difficulties\u2666
111k\u25cf1111 gold badges\u25cf340340 silver badges\u25cf508508 bronze badges
2 Answers
order by  
Up vote
12
Down vote
Accepted
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.
share improve this answer  follow
answered
Nov 29 '14 at 17:33

Szabolcs
206k\u25cf1919 gold badges\u25cf512512 silver badges\u25cf10811081 bronze badges edited
Nov 29 '14 at 17:38

1
I think in cases of most (if not all) infix operators, linkbreak should only happen after the operator. (+1) \u2013 Silvia Nov 29 '14 at 18:23 
1
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. \u2013 Alan Oct 4 '16 at 17:09
add a comment
Up vote
7
Down vote
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}
share improve this answer  follow
answered
Dec 14 '15 at 14:41

george2079
37.4k\u25cf11 gold badge\u25cf3737 silver badges\u25cf9999 bronze badges
Your Answer

Body
 Add picture


Log in

OR
Name

Email


By clicking \u201cPost Your Answer\u201d, you agree to our terms of service, privacy policy and cookie policy

Post Your Answer
meta chat tour help blog privacy policy legal contact us full site
2020 Stack Exchange, Inc. user contributions under cc by-sa