1

I'm trying to write the following equation using amsmath package in equation envoronment:

enter image description here

and break it into multiple lines using the following code

\begin{equation}
    \label{eq:eqn1}
        \begin{aligned}
            \vec{f}{}' &=\vec{\nabla}{f}                \\
                       &=\vec{\nabla}{f\left ( x, y, z \right )}                \\
                       &\approx \left ( \frac{1}{2} \left ( f\left ( x-1, y, z \right ) + f\left ( x+1, y, z \right ) \right ),  \\
                       &\qquad \frac{1}{2} \left ( f\left ( x, y-1, z \right ) + f\left ( x, y+1, z \right ) \right ),      \\
                       &\qquad \frac{1}{2} \left ( f\left ( x, y, z-1 \right ) + f\left ( x, y, z-1 \right ) \right ) \right )
        \end{aligned}
\end{equation}

This is giving me the following output:

enter image description here

I want the alignment and the line breaking the same but the final closing brace is missing here and for which I am getting error message also.

How can I get rid of this error and have the closing brace back in the equation?

  • 3
    \left( ...\right) doesn't work over ampersants. replace it with & \bigg( and \bigg) or left( <first line > \right. and & \left. <last line>\right). – Zarko Sep 20 '17 at 14:05
  • @Zarko Thanks. I choose the second option you have mentioned and it's working now. – Majis Sep 20 '17 at 14:10
  • 3
    potential duplicate: \left/\right across multiline equation – barbara beeton Sep 20 '17 at 14:18
2

You can use & with \left ... right if you load mathtools (needless to load amsmath in this case) and use its \DeclarePairedDelimiter and \MTkillspecial commands. I adapted an example in the documentation to define a \parens command, which can break across lines. Its starred version adds a pair of implicit \left \right around the parentheses. Alternatively to fine-tune the size of the parentheses, you can use an optional argument: [\big], [\Big], &c.

I took the opportunity to improve your layout and simplify your code, removing all unnecessary pairs of left \right, using a nested aligned environment, the medium-sized fractions from nccmath from the 1/2 coefficients, and the nicer-looking arrows from esvect:

\documentclass{article}
\usepackage{mathtools, nccmath}
\usepackage[b]{esvect} 

    \newcommand\MTkillspecial[1]{% helper macro
    \bgroup
    \catcode`\&=9
    \let\\\relax%
    \scantokens{#1}%
    \egroup
    }
    \DeclarePairedDelimiter\parens()
    \reDeclarePairedDelimiterInnerWrapper\parens{star}{
    \mathopen{#1\vphantom{\MTkillspecial{#2}}\kern-\nulldelimiterspace\right.}
    #2
    \mathclose{\left.\kern-\nulldelimiterspace\vphantom{\MTkillspecial{#2}}#3}}

\begin{document}

\begin{equation}
    \label{eq:eqn1}
        \begin{aligned}
            \vv{f}{}' &=\vv{\nabla}{f} \\
                       &= \vv{\nabla}{f( x, y, z)} \\
                       &\approx \begin{aligned}[t] \parens*{ & \mfrac{1}{2} \bigl( f( x-1, y, z) + f( x+1, y, z) \bigr), \\
                        & \mfrac{1}{2} \bigl( f( x, y-1, z) + f( x, y+1, z) \bigr), \\
                       & \mfrac{1}{2} \bigl( f( x, y, z-1) + f( x, y, z-1) \bigr)
                       }\end{aligned}
        \end{aligned}
\end{equation}

\end{document} 

enter image description here

  • Shouldn't the OP be placing the final three rows in a pmatrix environment? – Mico Sep 20 '17 at 14:57
  • 1
    I don't think so: clearly, it's a set of three coordinates, which should make up a row matrix if the page were wide enough – not a column matrix. – Bernard Sep 20 '17 at 15:02
  • @Bernard Great. Will it provide me the flexibility to choose between manual line break and automated line break depending on the page width. For example, in the given equation, the first two terms on the RHS can be written on a single line while the third one will go to the next line. Can it also be done using your framework without bothering for line breaks? Basically, in some cases, I need the kind of formatting I asked here, but there are cases where I prefer that my code will take care of the line break along with the alignment depending on the equation length and page width. – Majis Sep 20 '17 at 16:15
  • 1
    @Majis: The only package which caters for automatic line breaking I know is breqn, but I never used it. In general, I prefer to have full control on line breaking in equations. – Bernard Sep 20 '17 at 16:23
1

The following may be close to what you're looking for (^T denotes the transpose operator):

enter image description here

\documentclass{article} 
\usepackage{amsmath}
\begin{document}
\begin{equation}\label{eq:eqn1}
\renewcommand\arraystretch{1.25}
  \begin{aligned}[b]
    \vec{f}' =\vec{\nabla} f   
      &=\vec{\nabla} f(x, y, z)    \\
      &\approx 
        \begin{pmatrix} 
          \frac{1}{2} [ f(x-1, y, z) + f(x+1, y, z) ]  \\
          \frac{1}{2} [ f(x, y-1, z) + f(x, y+1, z) ]  \\
          \frac{1}{2} [ f(x, y, z-1) + f(x, y, z-1) ] 
        \end{pmatrix} ^{\!\!T}
  \end{aligned}
\end{equation}
\end{document} 
  • This is really a nice way to represent the equation. – Majis Sep 20 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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