4

As mentioned in the title: I would like to know if there is an elegant way to plot long mathematical expressions in a latex document in a very "coherent" way.

I have no specifically idea what is the best solution for that: so I am just asking for people who are familiar with long equations in a latex document: How do you manage to write it?

For instance : I have these equations :

enter image description here

But you can notice that the expressions are not structured in an homogeneous way: what could you advise?

\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}

\begin{document}

\begin{enumerate}
\newpage
\item \textbf{En $i$} 
\begin{align}
h_{i}^{n} = - \color{red}h_{i-1}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \right)^3 \right) &+ \color{red} h_{i}^{n+1} \color{black} \left (1+  \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \right)^3 + \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i+1}^{n} + h_{i}^{n}}{2} \right)^3 \right) \notag \\
- \color{red}h_{i}^{n+1} \color{black}\left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i+1}^{n}}{2} \right)^3  \right) \notag
\end{align}

\item \textbf{En $i = (N-1)$} 
\begin{align}
h_{N-1}^{n} = - \color{red} h_{N-2}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N-2}^{n}}{2} \right)^3 \right) + \color{red} h_{N-1}^{n+1} \color{black} \left (1+  \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \right)^3 + \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N}^{n} + h_{N-1}^{n}}{2} \right)^3 \right) \notag \\
- \color{red} h_{N}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \right)^3  \right) \notag
\end{align}

\end{enumerate}
\end{document}
2
  • Please, make your document example compilable. What mean "homogen way" to you?
    – Zarko
    May 22, 2022 at 18:45
  • You can make it oneline equation by resizing You equation like in example 1.1 from github.com/AnMnv/eBook May 22, 2022 at 19:13

4 Answers 4

8

With align* and equations in three lines:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{enumitem}

\begin{document}

\begin{enumerate}
\newpage
\item \textbf{En $i$}
\begin{align*}
h_{i}^{n} 
    = &{} - {\color{red}h_{i-1}^{n+1}} \Biggl(\frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \biggr)^3 \Biggr)   \\
      &{} + {\color{red} h_{i}^{n+1}} \Biggl(1+\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i-1}^{n}}{2}\right)^3 +
          \frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{i+1}^{n} + h_{i}^{n}}{2}\biggr)^3 \Biggr)   \\
      &{} + {\color{red}h_{i}^{n+1}} \Biggl(\frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{i}^{n} + h_{i+1}^{n}}{2} \biggr)^3  \Biggr)
\end{align*}

\item \textbf{En $i = (N-1)$}
\begin{align*}
h_{N-1}^{n} 
    = &{} - {\color{red} h_{N-2}^{n+1}} \biggl(\frac{\Delta t}{(\Delta x)^2}
        \Bigl(\frac{h_{N-1}^{n} + h_{N-2}^{n}}{2} \Bigr)^3\biggr)     \\
      &{} - {\color{red} h_{N-1}^{n+1}} \left (1+  \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \right)^3 + \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N}^{n} + h_{N-1}^{n}}{2} \right)^3 \right)       \\
      &{} - {\color{red} h_{N}^{n+1}} 
        \biggl(\frac{\Delta t}{(\Delta x)^2} \Bigl(\frac{h_{N-1}^{n}+ h_{N}^{n}}{2} \Bigr)^3  \Biggr)
\end{align*}

\end{enumerate}
\end{document}

enter image description here

1
  • @Mico, oh, this is mistake. Of course it is ˙align*. Corrected now.
    – Zarko
    May 24, 2022 at 11:03
5

Here's an answer that's similar to the one by @Zarko in that it uses 3 rather than 2 lines per equation. It differs in the use of \textcolor instead of \color, the replacement of the needlessly large outer round parentheses with smaller, but adequately sized square brackets, and a "snugging up" of the power-3 terms to the respective closing parentheses.

enter image description here

\documentclass{article}
\usepackage{amsmath,xcolor}
\begin{document}

\begin{enumerate}

\item \textbf{En} $i$
\begin{align*}
h_{i}^{n} 
= -\textcolor{red}{h_{i-1}^{n+1}} 
 &\biggl[\frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} 
  \biggr)^{\!\!3}\, \biggr] \\
 {}+\textcolor{red}{h_{i}^{n+1}} 
 &\biggl[1+  \frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \biggr)^{\!\!3} 
  + \frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{i+1}^{n} + h_{i}^{n}}{2} 
  \biggr)^{\!\!3}\, \biggr]  \\
 {}-\textcolor{red}{h_{i}^{n+1}} 
 &\biggl[\frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{i}^{n} + h_{i+1}^{n}}{2} 
  \biggr)^{\!\!3} \, \biggr] 
\end{align*}

\item \textbf{En} $i = (N-1)$
\begin{align*}
h_{N-1}^{n} 
= -\textcolor{red}{h_{N-2}^{n+1}} 
 &\biggl[\frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{N-1}^{n} + h_{N-2}^{n}}{2} 
  \biggr)^{\!\!3}\, \biggr] \\
 {}+\textcolor{red}{h_{N-1}^{n+1}} 
 &\biggl[1+  \frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \biggr)^{\!\!3} 
  + \frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{N}^{n} + h_{N-1}^{n}}{2} 
  \biggr)^{\!\!3}\, \biggr]  \\
 {}-\textcolor{red}{h_{N}^{n+1}} 
 &\biggl[\frac{\Delta t}{(\Delta x)^2} 
  \biggl(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} 
  \biggr)^{\!\!3} \, \biggr] 
\end{align*}

\end{enumerate}
\end{document}
5

Here is a possible solution, loading geometry to have more decent margins, and using the [wide=0pt] key from enumitem. For the second item, I also propose a solution with multline*.

    \documentclass{article}
    \usepackage[showframe]{geometry}
    \usepackage{xcolor}
    \usepackage{amsmath}
    \usepackage{enumitem}

    \begin{document}

    \begin{enumerate}[wide=0pt]
    \newpage
    \item \textbf{En $i$}
    \begin{align*}
    h_{i}^{n} = - \color{red}h_{i-1}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \right)^{\!\!3} \right) & + \color{red} h_{i}^{n+1} \color{black} \biggl (1+ \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i-1}^{n}}{2} \right)^{\!\!3} + \frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i+1}^{n} + h_{i}^{n}}{2} \right)^{\!\!3} \biggr) \\
    %{} & \phantom{ = }
     & - \color{red}h_{i}^{n+1} \color{black}\left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{i}^{n} + h_{i+1}^{n}}{2} \right)^{\!\!3} \right) \notag
    \end{align*}

    \item \textbf{En $i = (N-1)$}
     \begin{align*}
    h_{N-1}^{n} = & - \color{red} h_{N-2}^{n+1} \color{black} \Biggl(\frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N-1}^{n} + h_{N-2}^{n}}{2} \biggr)^{\!\!3} \Biggr) \\
    & + \color{red} h_{N-1}^{n+1} \color{black} \Biggl(1+ \frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \biggr)^{\!\!3} %\\
     + \frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N}^{n} + h_{N-1}^{n}}{2} \biggr)^{\!\!3}\Biggr) \\
     & - \color{red} h_{N}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \right)^{\!\!3} \right)
    \end{align*}
     \begin{multline*}
    h_{N-1}^{n} = - \color{red} h_{N-2}^{n+1} \color{black} \Biggl(\frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N-1}^{n} + h_{N-2}^{n}}{2} \biggr)^{\!\!3} \Biggr)% \\
     + \color{red} h_{N-1}^{n+1} \color{black} \Biggl(1+ \frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \biggr)^{\!\!3} \\
     + \frac{\Delta t}{(\Delta x)^2} \biggl(\frac{h_{N}^{n} + h_{N-1}^{n}}{2} \biggr)^{\!\!3}\Biggr) %\\
     - \color{red} h_{N}^{n+1} \color{black} \left(\frac{\Delta t}{(\Delta x)^2} \left(\frac{h_{N-1}^{n} + h_{N}^{n}}{2} \right)^{\!\!3} \right)
    \end{multline*}
    \end{enumerate}

    \end{document} 

enter image description here

2
  • Thank you again for all your answers. @Bernard : could you tell me what is the difference between multline* and aligned in this case ? What do you think is more suitable ?
    – Waxler
    May 22, 2022 at 20:35
  • @Wiss multline is not aligned: when the equation is split between two rows, as here, the upper row is flush left and the lower row is flushright. Note alos the mathtools defines the multlined environment.
    – Bernard
    May 22, 2022 at 20:39
0

Structure with \newcommmand

Based on Zarko's answer, I introduced some \newcommand definitions to reduce the amount repeating markup to highlight the differences between the different terms:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{enumitem}

\newcommand{\dtdxtwo}{\frac{\Delta t}{(\Delta x)^2}}
\newcommand{\redh}[1]{{\color{red}h_{#1}^{n+1}}}
\newcommand{\hcube}[2]{\biggl(\frac{h_{#1}^{n} + h_{#2}^{n}}{2} \biggr)^3}
\newcommand{\aterm}[2]{\dtdxtwo \hcube{#1}{#2}}

\begin{document}
\begin{enumerate}
  \item \textbf{En} $i$
    \begin{align*}
      h_{i}^{n} 
      = &{} - \redh{i-1} \Biggl(\aterm{i}{i-1}\Biggr) \\
        &{} + \redh{i} \Biggl(1+\aterm{i}{i-1}+\aterm{i+1}{i}\Biggr) \\
        &{} + \redh{i} \Biggl(\aterm{i}{i+1}\Biggr)
    \end{align*}
  \item \textbf{En} $i = N - 1$
    \begin{align*}
      h_{N-1}^{n}
      = &{} - \redh{N-2} \Biggl(\aterm{N-1}{N-2}\Biggr) \\
        &{} - \redh{N-1} \Biggl(1+\aterm{N-1}{N}+\aterm{N}{N-1}\Biggr) \\
        &{} - \redh{N} \Biggl(\aterm{N-1}{N}\Biggr)
   \end{align*}
\end{enumerate}
\end{document}

My feeling is that it is much easier to spot inconsistencies, sign or index errors in this markup. Also, differences in repeating terms can be expressed more clearly.

Finally, this opportunity for naming makes it possible to express equations with meaningful terms.

3
  • Since \textbf does not influence how math material is typeset, you may want to replace \textbf{En $i$} and \textbf{En $i = (N-1)$} with \textbf{En} $i$ and \textbf{En} $i = (N-1)$, respectively. Actually, since there seems to be little point to boldfacing just the word "En", you could (should?) probably skip boldfacing the word entirely. And, for the sake of good housekeeping, you may want to move the four \newcommand directives to the document's preamble.
    – Mico
    May 23, 2022 at 16:34
  • @Mico: followed suggestions, except for keeping boldface (as it is in the Q as well). Is there a technical benefit for moving the command definitions to the preamble? If they were used only withing that enumerate, I feel the "locality" would be communicated more clearly by keeping them within the environment they are used in.
    – ojdo
    May 24, 2022 at 9:31
  • I think it's really good practice to generate all macros in the preamble. (If a macro has to be redefined locally, that's a different matter.) If nothing else, this practice helps with simplifying the tracking down of bugs contained in the macro definitions...
    – Mico
    May 24, 2022 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .