4

The following code is given:

\begin{alignat*}{3}
    h(0) & = g_s(0) - g_s(0 - 1) = 0.25 & -  0.00 & =  \nicefrac{1}{4}\\
    h(1) & = g_s(1) - g_s(1 - 1) = 0.50 & -  0.25 & =  \nicefrac{1}{4}\\
    h(2) & = g_s(2) - g_s(2 - 1) = 0.75 & -  0.50 & =  \nicefrac{1}{4}\\
    h(3) & = g_s(3) - g_s(3 - 1) = 1.00 & -  0.75 & =  \nicefrac{1}{4}\\
    h(4) & = g_s(4) - g_s(4 - 1) = 1.00 & -  1.00 & = 0
\end{alignat*}

which outputs align-environment The output however should have the same spacing between each number like the first equation h(x) = g_s(x) - g_s(x - 1) has. (In other words, the spacing between the minus sign is too small.) How will I obtain this result?

  • 1
    as always it's easier to test if you provide a test document not a fragment, but use {}= – David Carlisle Jun 22 at 20:43
  • I will take this into consideration for my next posts, thanks for pointing that out. – Doesbaddel Jun 22 at 21:15
5

Two ways depending how you think of it {}- forces a binary - but you could view the form as only having right hand sides of the second and third groups so if you mark the empty lhs with && then the - at the start of the right hand sides get the usual space.

enter image description here

\documentclass{article}

\usepackage{amsmath,nicefrac}

\begin{document}

aaa
\begin{alignat*}{3}
    h(0) & = g_s(0) - g_s(0 - 1) = 0.25 & -  0.00 & =  \nicefrac{1}{4}\\
    h(1) & = g_s(1) - g_s(1 - 1) = 0.50 & -  0.25 & =  \nicefrac{1}{4}\\
    h(2) & = g_s(2) - g_s(2 - 1) = 0.75 & -  0.50 & =  \nicefrac{1}{4}\\
    h(3) & = g_s(3) - g_s(3 - 1) = 1.00 & -  0.75 & =  \nicefrac{1}{4}\\
    h(4) & = g_s(4) - g_s(4 - 1) = 1.00 & -  1.00 & = 0
\end{alignat*}

bbb
\begin{alignat*}{3}
    h(0) & = g_s(0) - g_s(0 - 1) = 0.25 & {}-  0.00 & =  \nicefrac{1}{4}\\
    h(1) & = g_s(1) - g_s(1 - 1) = 0.50 & {}-  0.25 & =  \nicefrac{1}{4}\\
    h(2) & = g_s(2) - g_s(2 - 1) = 0.75 & {}-  0.50 & =  \nicefrac{1}{4}\\
    h(3) & = g_s(3) - g_s(3 - 1) = 1.00 & {}-  0.75 & =  \nicefrac{1}{4}\\
    h(4) & = g_s(4) - g_s(4 - 1) = 1.00 & {}-  1.00 & = 0
\end{alignat*}

ccc
\begin{alignat*}{3}
    h(0) & = g_s(0) - g_s(0 - 1) = 0.25 && -  0.00 && =  \nicefrac{1}{4}\\
    h(1) & = g_s(1) - g_s(1 - 1) = 0.50 && -  0.25 && =  \nicefrac{1}{4}\\
    h(2) & = g_s(2) - g_s(2 - 1) = 0.75 && -  0.50 && =  \nicefrac{1}{4}\\
    h(3) & = g_s(3) - g_s(3 - 1) = 1.00 && -  0.75 && =  \nicefrac{1}{4}\\
    h(4) & = g_s(4) - g_s(4 - 1) = 1.00 && -  1.00 && = 0
\end{alignat*}


\end{document}
  • Is there a difference between b) and c) or do both variants produce exactly the same result? – Doesbaddel Jun 22 at 21:17
  • 1
    @Doesbaddel same result here as your numbers are same width but change the 0.75 to 0.7 and you will see the && version keeps them left aligned wehereas your original and the {}- version right aligns them. I think && is better actually but... – David Carlisle Jun 22 at 21:39
  • Ok, thank you for clarification! – Doesbaddel Jun 22 at 22:07
5

Using {}= solves the spacing issue:

\documentclass{article}

\usepackage{amsmath}
\usepackage{nicefrac}

\begin{document}

\begin{alignat*}{3}
h(0) & = g_s(0) - g_s(0 - 1) = 0.25 & {}- 0.00 & = \nicefrac{1}{4} \\
h(1) & = g_s(1) - g_s(1 - 1) = 0.50 & {}- 0.25 & = \nicefrac{1}{4} \\
h(2) & = g_s(2) - g_s(2 - 1) = 0.75 & {}- 0.50 & = \nicefrac{1}{4} \\
h(3) & = g_s(3) - g_s(3 - 1) = 1.00 & {}- 0.75 & = \nicefrac{1}{4} \\
h(4) & = g_s(4) - g_s(4 - 1) = 1.00 & {}- 1.00 & = 0
\end{alignat*}

\end{document}

Output of the MWE using <code>\tfrac</code> instead of <code>nicefrac</code>.

(I do not have the package nicefrac installed. I would recommend to use \tfrac instead of \nicefrac. Most mathematicians prefer a horizontal fraction bar.)

Edit

Here is another version that might make more sense with respect to the structure of the content:

\documentclass{article}

\usepackage{amsmath}
\usepackage{nicefrac}

\begin{document}

\begin{alignat*}{3}
h(0) & = g_s(0) - g_s(0 - 1) && = 0.25 - 0.00 && = \nicefrac{1}{4} \\
h(1) & = g_s(1) - g_s(1 - 1) && = 0.50 - 0.25 && = \nicefrac{1}{4} \\
h(2) & = g_s(2) - g_s(2 - 1) && = 0.75 - 0.50 && = \nicefrac{1}{4} \\
h(3) & = g_s(3) - g_s(3 - 1) && = 1.00 - 0.75 && = \nicefrac{1}{4} \\
h(4) & = g_s(4) - g_s(4 - 1) && = 1.00 - 1.00 && = 0
\end{alignat*}

\end{document}

David Carlisle shows yet another version for placing the delimiters in his answer to this question. All versions have pros and cons, and it depends, e.g., on the format of the numbers and the other content, which one is best.

4

A tabstackengine approach. Here, the \TABbinary forces the leading negatives to be considered binary, the \setstackalingap{} allows the horizontal gap between align groups to be eliminated and the \setsatckgap{L}{} allows for the baselineskip to be set.

\documentclass{article}
\usepackage{amsmath,nicefrac,tabstackengine}
\TABstackMath
\begin{document}
\[
\TABbinary
\setstackaligngap{0pt}
\setstackgap{L}{1.2\baselineskip}
\alignCenterstack{
h(0) & = g_s(0) - g_s(0 - 1) = 0.25 & - 0.00 & = \nicefrac{1}{4} \\
h(1) & = g_s(1) - g_s(1 - 1) = 0.50 & - 0.25 & = \nicefrac{1}{4} \\
h(2) & = g_s(2) - g_s(2 - 1) = 0.75 & - 0.50 & = \nicefrac{1}{4} \\
h(3) & = g_s(3) - g_s(3 - 1) = 1.00 & - 0.75 & = \nicefrac{1}{4} \\
h(4) & = g_s(4) - g_s(4 - 1) = 1.00 & - 1.00 & = 0
}
\]
\end{document}

enter image description here

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.