23

How can I break long formulas in LyX into two (or more) lines? I know how to write several lines of equations in one "math box" but I'm looking for a solution to break lines even in the middle of a parenthesis.

I'm using LyX 1.6.4 and currently, when the formula is too big, it doesn't do anything (it just doesn't print the last segment of it, which is "beyond" the page).

Is this possible in LyX?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248

7 Answers7

5

Yes, but you have to do it by hand.

Check out:

  1. How can I have linebreaks in my long LaTeX equations?
  2. Latex Multiline Equations

Lyx has an "Insert -> Math -> AMS multline environment" control, or you can use the solutions recommended above by entering Latex directly using the Insert Latex control.

Note on breqn

Michael Downes has done some extraordinary Tex coding with this package, but it is not robust enough for general use - it is mostly incompatible with Amsmath, for instance, breaks in hard-to-understand ways, and makes some demonstrably unsound linebreaks in formulae.

Automatic line-breaking in display formulae was listed as one of the top open research problems in Tex.

Community
  • 1
  • 1
Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
  • 1
    The AMS Multiline environment works, but not in the middle of a parenthesis, which is surrounding most of my equation. Is there a solution that does that? – Amir Rachum May 25 '10 at 13:21
  • @Amir: Insert a Latex code block directly using "Insert -> TeX Code" (Alt-L). – Charles Stewart May 25 '10 at 13:28
  • 2
    @Amir: if you mean '\left('-type self-sizing parentheses, then you cannot really use them with 'multline'. Either close with '\right.' before line-break (and reopen with '\left.') which can lead to parentheses of different size, or size them yourself using '\big(', '\Big(', '\Bigg(', etc. – Aniko May 25 '10 at 13:52
4

If you include \usepackage{breqn} in the LyX "LaTeX Preamble" (plus the following code) then the standard "equation" environment (Displayed Formula) will automatically break like you want.

% Add support for automatic equation breaking
\gdef\wrap@breqn@environ#1#2{
    \expandafter\let\csname breqn@oldbegin@#1\expandafter\endcsname\csname #1\endcsname
    \expandafter\let\csname breqn@oldend@#1\expandafter\endcsname\csname end#1\endcsname
    \expandafter\gdef\csname breqn@begin@#1\endcsname{%
        \expandafter\let\csname #1\expandafter\endcsname\csname breqn@oldbegin@#1\endcsname%
        \begin{#2}%
    }
    \expandafter\gdef\csname breqn@end@#1\endcsname{%
        \expandafter\let\csname end#1\expandafter\endcsname\csname breqn@oldend@#1\endcsname%
        \end{#2}%
        \expandafter\let\csname #1\expandafter\endcsname\csname breqn@begin@#1\endcsname%
        \expandafter\let\csname end#1\expandafter\endcsname\csname breqn@end@#1\endcsname%
    }
    \expandafter\let\csname #1\expandafter\endcsname\csname breqn@begin@#1\endcsname
    \expandafter\let\csname end#1\expandafter\endcsname\csname breqn@end@#1\endcsname
}
\wrap@breqn@environ{equation}{dmath}
\wrap@breqn@environ{equation*}{dmath*}

Added 2016-12-08:

If you wish to override the default (unnumbered) LyX equation environment then also add the following:

\DeclareRobustCommand\[{\begin{equation*}}
\DeclareRobustCommand\]{\end{equation*}}
Compholio
  • 733
  • 1
  • 9
  • 17
  • I just tried this in Lyx and it doesn't do anything... line still goes beyond right margin. – Dmitri Nesteruk Apr 22 '12 at 14:44
  • @DmitriNesteruk: LyX uses \[ and \] to begin and end math environments, you would have to add the following at the end: `\global\let\[=\equation` `\global\let\]=\endequation` – Compholio Jun 13 '12 at 16:06
  • @Compholio, where do I put the line you wrote? – adiro Jul 06 '16 at 08:34
  • @Compholio I got this error on lax 2.2: `\begin{equation} Your command was ignored. Type I to replace it with another command, or to continue without it.` Do you know what to do with it? – bobsacameno Nov 25 '16 at 10:15
  • @adiro Document -> Settings -> LaTeX Preamble – Compholio Nov 28 '16 at 14:43
  • 1
    @roi.holtzman Did you include the package? (`\usepackage{breqn}`) – Compholio Nov 28 '16 at 14:46
  • @Compholio thanks! I didn't include the package. Now I get that all of the equations are numbered. Can I get rid of that? Also, I see that this breaks a line after every equal sign (=). Is there a way that It'll break a line only when needed? – bobsacameno Nov 28 '16 at 19:21
  • @roi.holtzman The numbering/lack of numbering should be dictated by the environment you are using (equation vs. equation*). LyX generally uses "`\[`" and "`\]`" to denote unnumbered equations, so to override that "environment" you will need: `\DeclareRobustCommand\[{\begin{equation*}} \DeclareRobustCommand\]{\end{equation*}}` To break the equation less "vigorously" you can turn on compact mode (after including the package): `\setkeys{breqn}{compact={1}}` – Compholio Nov 28 '16 at 20:10
  • @Compholio I still see that it only breaks a line after `=` sign. Is there a way for even just a long expression it'll break a line if doesn't fit on the page? – bobsacameno Nov 29 '16 at 09:25
  • @roi.holtzman That is the whole point of the breqn package. While there are a lot of settings that control its behavior, the default is to break any line that doesn't fit on the page. Here is a simple example that I just tested with (with "compact" on): `a+b+c+\left(d+e+f\right)=g=h=i+\left(j+k+l+m\right)+n+o+p+q+r=s+t+u+v+w+x+y+z+aa+bb+cc+dd+ee+ff+gg+hh+ii+jj=0` In this case I see a break after "r" (at the equal sign) and a break after "bb" (no equal sign). – Compholio Dec 01 '16 at 15:34
  • @Compholio I copied your equation, but I get a new line after each `=`. I have copied exactly what's written in your answer above. Do you know what am I doing wrong? – bobsacameno Dec 01 '16 at 17:43
  • @roi.holtzman I have no idea, try starting with this [simple example](https://dl.dropboxusercontent.com/u/195059/breqn-test.lyx) – Compholio Dec 04 '16 at 15:47
  • @Compholio your example works great! I now copied your latex preamble and put it into another document I have. But when I export it I get some error. E.g. I get this: ` ...ght]=i\hbar\delta\left(x-y\right)\sp\left [\phop\left(x\right),\phop... I've deleted a group-closing symbol because it seems to be spurious, as in `$x}$'. But perhaps the } is legitimate and you forgot something else, as in `\hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing `I$}'.` Do you know the problem? – bobsacameno Dec 05 '16 at 16:31
  • @roi.holtzman The example I gave is ignoring some of the complexity that usually surrounds `\[` and `\]` about whether a bracket exists within a math-mode environment. If you could send me a minimal example that reproduces your problem then I can probably fix the declaration pretty easily. – Compholio Dec 05 '16 at 17:35
  • @Compholio thanks! here you go file – bobsacameno Dec 05 '16 at 19:55
  • 1
    @roi.holtzman Sorry it's taken me a while to get back to you, my goodness did you have fun with macros. You have two problems: 1) you have overriden the LaTeX command `\sp`, since breqn uses that command you need to choose another name for your command, 2) you have a naked carriage return just before "Working in this", you will need to remove it for the document to compile. wrt to #1, you can revise your document to replace `\sp` with `\spacer` (on Linux/Mac) like this: `sed 's|\\sp\([\ -}\\]\)|\\spacer\1|g' tmp.lyx > new.lyx` (replace tmp.lyx with filename) – Compholio Dec 08 '16 at 14:37
  • @Compholio now that it works, I see that when I write formulas with line breaks, it doesn't do the line breaking automatically. Do you how to force it, always to do the automatic thing? Now what I do it write my formulas and that I copy it to a one-liner, and so it works. I want to not copy if possible.. – bobsacameno Dec 29 '16 at 11:08
  • @roi.holtzman That is probably a separate question that gets into the specifics about how breqn works, but if you send me an example of what you're talking about then I will try to help. – Compholio Dec 30 '16 at 17:09
  • @Compholio thanks! here's an example file. https://drive.google.com/file/d/0B2SqsTZ9GqcESjFhTS01UzE4bjg/view?usp=sharing note that I have many long equations. Some of the have multiple lines, because it's easier to type them this way. But they don't get automatic line breaks when needed. At the beginning you have 2 equations which are the same. One written as one liner and a copy written in multiple lines. – bobsacameno Jan 04 '17 at 21:29
  • @roi.holtzman I don't understand what you are trying to do. Are you saying that you want to use equation breaking with an "align*" equation environment? – Compholio Jan 06 '17 at 20:16
  • @Compholio I want the equations to break, even when I have more then one line in the equation. Meaning that I myself wrote a 3 line equation, which still has a line that doesn't fit the page. I want this line to break also. Is this possible? – bobsacameno Jan 07 '17 at 11:07
  • 1
    @roi.holtzman It is possible, but not easy. LyX multi-line equations use a different environment (align*) that does not easily translate into the scheme that breqn uses. I will look into it, but it may take me a while to figure out how to translate the commands from the align environment. – Compholio Jan 09 '17 at 15:09
3

You might want to try the breqn package. It provides automatic line breaking for big formulas. This way there is no need for manually fixing your delimiters or choosing the break points. Of course, this is no actual LyX functionality but it does exactly what you want.

Pieter
  • 2,822
  • 18
  • 17
  • For some reason MikTeX can't install this automatically. What do I do with the dtx file I downloaded from your link? – Amir Rachum May 26 '10 at 20:53
  • @Amir There is an installation guide provided in both the README and the actual .dtx file, these might be a good start :). My MikTeX installed the `mh` bundle correcty by the way, so it should be possible without having to resort to manual installation. – Pieter May 26 '10 at 21:10
  • Installed it, added `\usepackage{breqn}` to latex preamble and it doesn't do anything. I didn't understand from the document if I need to use any special command in the document. – Amir Rachum May 26 '10 at 21:23
  • @Amir You have to replace the math environments LyX used for you with `dmath` environments. And make sure you use your delimiters and operators properly. – Pieter May 27 '10 at 05:46
1

Insert -> Math-> AMS Multiline Environment

write your equation and click 'Ctrl+Enter' where you want to split Then Right click the equation and select number whole formula

Guest
  • 11
  • 1
0

Very recently I had to deal with the very same problem. My simple solution (don't know if best) is to use ctrl+enter for multiple lines and, for the parentheses problem, you can use the button right after the parentheses, brackets and braces, which opens a window with these and many other options and, importantly, the option to have different choices at the same time, (like this}. In particular, you can chose "none" and only have either "(" or ")" for each line, (like +/this) Hope it helps.

Pablo
  • 1
0

My way of "breaking long formula lines in lyx" is to use breqn on demand. The method is demonstrated in this post.

In the LyX "LaTeX Preamble" put the following

\usepackage{amsmath}
\usepackage{etoolbox} 
\usepackage{breqn}

\newcommand{\breqnoverloadothers}
{% 
    \renewenvironment{equation}{\ignorespaces\begin{dmath}}{\end{dmath}\ignorespacesafterend}%
    \renewenvironment{equation*}{\ignorespaces\begin{dmath*}}{\end{dmath*}\ignorespacesafterend}%
    \renewenvironment{multline}{\ignorespaces\begin{dmath}}{\end{dmath}\ignorespacesafterend}%
    \renewenvironment{multline*}{\ignorespaces\begin{dmath*}}{\end{dmath*}\ignorespacesafterend}%

}

\newcommand\breqnundefineothers
{%  
    \renewenvironment{equation}{}{}%
    \renewenvironment{equation*}{}{}%
    \renewenvironment{multline}{}{}%
    \renewenvironment{multline*}{}{}%

}

\AtBeginEnvironment{dmath}{\breqnundefineothers}
\AtBeginEnvironment{dmath*}{\breqnundefineothers}

\AtBeginEnvironment{dgroup}{\def\breqnundefineothers{}\breqnoverloadothers}
\AtBeginEnvironment{dgroup*}{\def\breqnundefineothers{}\breqnoverloadothers}

In Lyx, for long equations for which you want to apply breqn, just 1) typeset the equations using any of math environments that have been overloaded above, i.e., equation/equation*/multline/multline*, and 2) surround the equation(s) using the dmath environment or the dgroup environment, like:

\begin{dmath}
    hit ctrl+shift+M to insert a long equation here, and number the equation
\end{dmath} 

or

\begin{dgroup}
    hit ctrl+shift+M to insert a long equation here, and number the equation
    hit ctrl+shift+M again to insert another long equation here, and number the equation
\end{dgroup} 

Now you can export to PDF and see that the long equations are handled "automatically" by breqn. Equations not enclosed in dmath or dgroup won't be influenced.

Screenshots are generated but I do not have the 10-point reputation to upload them.

Peng Liu
  • 1
  • 2
0

I know this is a bit old, but someone might still need this info like I did not too long ago. I recently had similar issues to this, breaking a numbered line in the middle. (lyx 2.04 and higher). I used the lyx eqn array. Put the equation, all of it into the right box of the eqn array (there are two of them) At the point that you want to break the equation, press CTRL+Enter. This splits your equation into two lines. If you have a numbered equation array, toggle the upper line with: Alt+M Shift+N to remove the equation number, leaving you with only one equation on two lines.

Ejike
  • 1
  • 1