# Conditional linebreak in align environment

I have a large document to typeset in two different page formats. Some of the equation lines are too long in the small format so I wanted to break some equation lines conditioned on whether the macro \usesmallformat is defined:

\documentclass{article}

\usepackage{amsmath}

% Uncomment for small format - doesn't work, however
% \newcommand{\usesmallformat}{1}
% \setlength{\textwidth}{5cm}

\begin{document}

\begin{align}
A
&\leq
\sum_{i = 0}^L \|(I-K^i)(I-F^i)u\|_{V(\Omega)} \nonumber \\
%
&\leq
\sum_{i = 0}^L \left(\|(B - C^i)(I - D^i)u\|_{V(\Omega)}
{\ifdefined\usesmallformat \right. \nonumber\\ \fi}
%
\label{eq:One}
+
{\ifdefined\usesmallformat \left. \fi}
\|(I - D^i)(I - S^i) u \|_{V(\Omega)} \right).
\end{align}

\end{document}


I would like the second line of the align environment to be broken at the +-sign in the small format. Only the last line should be numbered. Also I would like to avoid duplicating formula code. Setting the align environment in two versions and surrounding them with the \ifdefined would work, but leads to code bloat and is harder to maintain in the future.

If I uncomment the two lines at the beginning of the document, however, I get Missing } inserted \end{align} and some follow-up errors.

Do you have any idea how to achieve the desired effect?

PS: I've read about the breqn package, but I can't use it because it conflicts with some other package that I have to include.

-
Welcome to TeX.SX! –  Papiro Jul 1 '13 at 11:08

It is better and simpler to use the mechanisms provided by etoolbox, where you can set up a toggle (or alternatively a bool). The following makes your example work, but I don't like the coding and will give another approach below:

\documentclass{article}

\usepackage{amsmath,etoolbox}

\newtoggle{smallformat}
\settoggle{smallformat}{false} %set to true or false as required
\iftoggle{smallformat}{\setlength{\textwidth}{5cm}}{}

\begin{document}

\begin{align}
A
&\leq
\sum_{i = 0}^L \|(I-K^i)(I-F^i)u\|_{V(\Omega)} \nonumber \\
%
&\leq
\sum_{i = 0}^L \left(\|(B - C^i)(I - D^i)u\|_{V(\Omega)}
\iftoggle{smallformat}{\right. \nonumber\\}{}
%
\label{eq:One}
+
\iftoggle{smallformat}{\left. }{}
\|(I - D^i)(I - S^i) u \|_{V(\Omega)} \right).
\end{align}

\end{document}


The addition to your code declares a new toggle smallformat, and in this case initially sets it to false. You then have the \iftoggle{smallformat}{truecase}{falsecase} for use in your code.

Now to how I would prefer to code this:

\documentclass{article}

\usepackage{mathtools,amssymb,etoolbox}

\newtoggle{smallformat}
\settoggle{smallformat}{true}
\iftoggle{smallformat}{\setlength{\textwidth}{5cm}}{}

\newcommand{\eqbreak}[1][2]{\\&\hskip#1em}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}

\begin{document}

$$\label{eq:One} \begin{split} A &\leqslant \sum_{i = 0}^L \norm{(I-K^i)(I-F^i)u}_{V(\Omega)} \\ &\leqslant \sum_{i = 0}^L \bigl(\norm{B - C^i)(I - D^i)u}_{V(\Omega)} \iftoggle{smallformat}{\eqbreak}{} + \|(I - D^i)(I - S^i) u \|_{V(\Omega)} \bigr). \end{split}$$

\end{document}


Your display is a single equation spread over several lines, therefore equation with an internal split is more appropriate as you only want one equation number.

Use of \left...\right should be restricted as much as possible. Using \bigl...\bigr and their cousins makes coding in your example easier. It also avoids the situation where all the tall material is on just one line, which in the narrow version would result in parentheses of different sizes.

The optional line break is via a new command \eqbreak that I find very useful in displays. It takes an optional argument saying how much the next line should be indented. By default this is set to \qquad = 2em, but e.g. \eqbreak[4] will give twice as much.

I have used mathtools to set up a \norm command form your \|...\|, this should give better spacing around these terms, without something like this LaTeX does not which \| is a left delimiter.

Finally I prefer \leqslant to \leq.

Narrow output:

-
+1 as the OP says it's a better answer:-) –  David Carlisle Jul 1 '13 at 12:08

You need to not insert {} around the alignment markup in the small case, and you need to not put & in the middle of the \if in the other case, so..

\documentclass{article}

\usepackage{amsmath}

\newcommand{\usesmallformat}{1}
\setlength{\textwidth}{5cm}
\makeatletter
\newcommand\sformat{%
\relax\ifdefined\usesmallformat\expandafter\@firstofone\else\expandafter\@gobble\fi}
\makeatother
\begin{document}

\begin{align}
A
&\leq
\sum_{i = 0}^L \|(I-K^i)(I-F^i)u\|_{V(\Omega)} \nonumber \\
%
&\leq
\sum_{i = 0}^L \left(\|(B - C^i)(I - D^i)u\|_{V(\Omega)}
\sformat{\right. \nonumber\\}
%
\label{eq:One}