8

I'm working on creating a specific two-column table in \LaTeX. The second column, $f(n)$, contains expressions that are linear combinations of $\alpha$, $\beta$, and $\gamma$, but not all terms are present in every row.

My goal is to achieve a consistent column alignment for the coefficients of $\alpha$, $\beta$, and $\gamma$ across all rows, with consistent spacing around the $+$ signs. The terms $\alpha$, $\beta$, $\gamma$ themselves should also align.

To achieve this alignment, I currently rely heavily on \phantom{} and empty {} groups, which feels overly complicated. I suspect there is a much cleaner, more idiomatic approach that I am missing.

Since I'm not very proficient, could you please critique my current approach and suggest a more robust method to achieve this specific alignment?

Here is my current code snippet:

\documentclass{article}

\begin{document}

\[
  \begin{array}{@{}c|c@{}}
    n & f(n) \\
    \hline
    1 & \phantom{0}\alpha\phantom{{}+0\beta+0\gamma} \\
    \hline
    2 & 2\alpha+\phantom{0}\beta\phantom{{}+0\gamma} \\
    3 & 2\alpha\phantom{{}+0\beta}+\phantom{0}\gamma \\
    \hline
    4 & 4\alpha+3\beta\phantom{{}+0\gamma}           \\
    5 & 4\alpha+2\beta+\phantom{0}\gamma             \\
    6 & 4\alpha+\phantom{0}\beta+2\gamma             \\
    7 & 4\alpha\phantom{{}+0\beta}+3\gamma           \\
    \hline
    8 & 8\alpha+7\beta\phantom{{}+0\gamma}           \\
    9 & 8\alpha+6\beta+\phantom{0}\gamma
  \end{array}
\]

\end{document}
New contributor
tmc is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • 1
    All right! Thank you for your comment; you are correct, of course. I will include that. Commented yesterday
  • 2
    Welcome to TeX.SX! Commented yesterday
  • 1
    @jlab: Thank you! I will take a look at the information you've sent! Commented yesterday

2 Answers 2

9

You want more columns:

\documentclass{article}
\usepackage{amsmath,array}

\begin{document}

\begin{equation*}
  \newcolumntype{T}{r@{}l}
  \newcolumntype{B}{@{}>{{}}c<{{}}@{}}
  \begin{array}{c|TBTBT}
    n & \multicolumn{8}{c}{f(n)} \\
    \hline
    1 &  &\alpha \\
    \hline
    2 & 2&\alpha &+&  &\beta \\
    3 & 2&\alpha & &  &      &+&  &\gamma \\
    \hline
    4 & 4&\alpha &+& 3&\beta \\
    5 & 4&\alpha &+& 2&\beta \\
    6 & 4&\alpha & &  &      &+& 2&\gamma \\
    7 & 4&\alpha & &  &      &+& 3&\gamma \\
    \hline
    8 & 8&\alpha &+& 7&\beta \\
    9 & 8&\alpha &+& 6&\beta &+& &\gamma
  \end{array}
\end{equation*}

\end{document}

I locally define two new column types to avoid a complicated setup for the specification: T for “terms”, one right and one left aligned column; B for “binary”, with appropriate spacing. So you see that the input can be well aligned to ease maintenance.

If you want the vertical rule, don't remove the spacing at either side.

output vr

Without the vertical rule:

\documentclass{article}
\usepackage{amsmath,array,booktabs}

\begin{document}

\begin{equation*}
  \newcolumntype{T}{r@{}l}
  \newcolumntype{R}{@{}>{{}}c<{{}}@{}}
  \begin{array}{@{}cTRTRT@{}}
    \toprule
    n & \multicolumn{8}{l}{f(n)} \\
    \midrule
    1 &  &\alpha \\
    \midrule
    2 & 2&\alpha &+&  &\beta \\
    3 & 2&\alpha & &  &      &+&  &\gamma \\
    \midrule
    4 & 4&\alpha &+& 3&\beta \\
    5 & 4&\alpha &+& 2&\beta \\
    6 & 4&\alpha & &  &      &+& 2&\gamma \\
    7 & 4&\alpha & &  &      &+& 3&\gamma \\
    \midrule
    8 & 8&\alpha &+& 7&\beta \\
    9 & 8&\alpha &+& 6&\beta &+& &\gamma \\
    \bottomrule
  \end{array}
\end{equation*}

\end{document}

output no vr

1
  • 1
    Thank you very much for the detailed answer! It addresses all my points perfectly and your suggested method is much cleaner than my original approach. I'm not sure what is the policy for accepting answers. I would gladly accept yours right now. Should I leave it open for more time? Commented yesterday
5

The following solution doesn't require inserting & particles between the coefficients \alpha, \beta, and \gamma and their associated numbers. Furthermore, it gets by with defining just 1 new column type -- for the columns with + (or -) symbols.

enter image description here

\documentclass{article}
\usepackage{booktabs} % for '\midrule' macro
\usepackage{array}    % for '\newcolumtype' macro
\newcolumntype{C}{>{{}}c<{{}}} % for columns with '+' symbols

\begin{document}

\[
\setlength\arraycolsep{0pt} % obviate need for '@{}' particles
\begin{array}{ c @{\hspace{1.5em}} rCrCr }
    n & \multicolumn{5}{l}{f(n)} \\
    \midrule
    1 &  \alpha \\
    \midrule
    2 & 2\alpha &+& \beta             \\
    3 & 2\alpha & &        &+& \gamma \\
    \midrule
    4 & 4\alpha &+&3\beta             \\
    5 & 4\alpha &+&2\beta  &+& \gamma \\
    6 & 4\alpha &+& \beta  &+&2\gamma \\
    7 & 4\alpha & &        &+&3\gamma \\
    \midrule
    8 & 8\alpha &+&7\beta             \\
    9 & 8\alpha &+&6\beta  &+& \gamma
\end{array}
\]

\end{document}
3
  • Very nice! Really elegant. I like the fact that it reduces the number of columns needed, since there's no need for inserting extra & separators. You've managed to align both coefficients and the unknowns in one go, using a right alignment. I really appreciate your help! I've learned a lot from both yours and egreg's solution. Commented 6 hours ago
  • I've already accepted egreg's answer, but I've also upvoted yours. It certainly does what I asked. Thanks again. Commented 6 hours ago
  • @tmc - Thanks for the upvote. :-) Incidentally, you asked elsewhere about the timing of when to accept any given answer you may have received. While it's perfectly ok to upvote good answers as you receive them, site guidelines strongly recommend waiting at least several hours, and better a day or even longer, before "accepting" any given answer. That way, you don't inadvertently discourage other would-be answer providers from coming up with additional answers. Commented 3 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.