Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 &\quad\ \fi}
+
{\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.

share|improve this question
    
Welcome to TeX.SX! –  Papiro Jul 1 '13 at 11:08

2 Answers 2

up vote 5 down vote accepted

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}{&\quad\ }{}
+
\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}

\begin{equation}
  \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{equation}

\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:

Sample narrow output

share|improve this answer
    
+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}
\sformat{&\quad\ }
+
\sformat{\left.}
\|(I - D^i)(I - S^i) u \|_{V(\Omega)} \right).
\end{align}

\end{document}
share|improve this answer
    
Thank you for your answer, I like it because it works without additional packages, and it solves my problem just fine. I accepted Andrew Swann's answer though because I learned even more from it. –  Stan4355 Jul 1 '13 at 11:49
    
@Stan4355 OK you can't win 'em all:-) –  David Carlisle Jul 1 '13 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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