3

I have the following equation inside an align and split environment but I would like to align the values of the constants below (of c_1, c_2 and c_3) even with the negative sign.

\begin{align}
\begin{split} 
    \cos(x) &= c_1 + c_2x^2 + c_3x^4 \\
            &= c_1 + x^2(c_2 + c_3x^2) \\
    \text{where} \\
    c_1 &= 0.99940307 \\
    c_2 &= -0.49558072 \\
    c_3 &= 0.03679168 
\end{split}
\end{align}

This code generates this:

enter image description here

I wanted to align all values after the minus sign, like this:

enter image description here

How can I do this?

  • 3
    Perhaps c_1 &= \hphantom{-}0.99940307? – GuM Sep 26 '18 at 22:15
  • 1
    Note, however, that you are misusing both the align and the split environments, and that \text{where} should probably be \intertext{where}. I advise you to have a look at the manual of the amsmath package: on a TeXLive installation, it can be brought up by typing texdoc amsmath at a shell prompt. – GuM Sep 26 '18 at 22:28
  • You could create break your column in two so that you can align on the decimal point. – A.Ellett Sep 26 '18 at 23:01
  • 1
    @GuM: \shortintertext{where} might be better with the mathtools package. – Peter Grill Sep 26 '18 at 23:08
  • 1
    @PeterGrill: I actually knew that, but I didn’t want to intimidate a (presumed) beginner with such niceties… ;-) – GuM Sep 26 '18 at 23:15
2

The simplest approach is to use a phantom where a sign is missing; \mathbin{\phantom{-}} will behave exactly like - in all situations, as far as spacing is concerned, but printing nothing.

\documentclass{article}
\usepackage{amsmath}

% first strategy
\newcommand{\nm}{\mathbin{\phantom{-}}}

% second strategy
\begingroup\lccode`~=``\lowercase{\endgroup\def~}{\mathbin{\phantom{-}}}
\AtBeginDocument{\mathcode``="8000 }

\begin{document}

\begin{equation}
\begin{split} 
    \cos(x) &= c_1 + c_2x^2 + c_3x^4 \\
            &= c_1 + x^2(c_2 + c_3x^2) \\
    \text{where} \\
    c_1 &= \nm 0.99940307 \\
    c_2 &=   - 0.49558072 \\
    c_3 &= \nm 0.03679168 
\end{split}
\end{equation}

\begin{equation}
\begin{split} 
    \cos(x) &= c_1 + c_2x^2 + c_3x^4 \\
            &= c_1 + x^2(c_2 + c_3x^2) \\
    \text{where} \\
    c_1 &= `0.99940307 \\
    c_2 &= -0.49558072 \\
    c_3 &= `0.03679168 
\end{split}
\end{equation}

\end{document}

The first strategy is simpler from a programming point of view; for the second I used a character that's not commonly used in math and made it “math active” expanding to the same phantom as in \nm.

enter image description here

0

Following the approach provided in this answer

\documentclass{article}
\usepackage{mathtools}
\newlength{\myindent}
\setlength{\myindent}{10pt}
\newcommand{\LHS}[2][\myindent]{\hspace{#1}\mathrlap{#2}} 
\newcommand{\RHS}[2][\myindent]{\mathrlap{#2}\hspace{#1}}

\begin{document}
    \begin{align}
        \begin{split} 
            \cos(x) &= c_1 + c_2x^2 + c_3x^4 \\
            &= c_1 + x^2(c_2 + c_3x^2) \\
            \shortintertext{where}
            c_1 &= \LHS{0.99940307}\\
            c_2 &=\RHS{-}0.49558072\\
            c_3 &= \LHS{0.03679168}
        \end{split}
    \end{align}
\end{document}

enter image description here

  • The spacing is, however, off. The - on the r.h.s. is a unary operator. Add a line c_2 &= -0.49... to see what I mean. – campa Sep 27 '18 at 8:41
  • @campa I am sorry I couldn't understand what you mean. If you are talking about the spacing between - and 0.49..., it can be controlled by setting the length \myindent. – Diaa Sep 27 '18 at 8:45

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.