4

I am trying to make use of the array environment (anything that operates similarly would suffice) that will automatically stretch the columns to be even-width and spread across the entire page. One example:

\documentclass[12pt]{article}
\usepackage{amsmath}
\renewcommand{\u}[1]{\underline{#1}}

\begin{document}
\[\begin{array}{rrr}
    5 & 5 & 5\\
    \u{+4} &\u{+4} &\u{+4}
\end{array}\]
\end{document}

All that I know how to do at the moment is to insert a 'fake' column between each pair of columns and use \hspace*{##cm} to force the columns to spread out, but this gets a bit tedious if I have, say 7 columns on 13 rows that, for one reason or another, cannot all be a part of a single array environment.

Is there a semi-simple approach to this problem? I've been searching, but I haven't found anything that has seemed very helpful (it may be the case that I'm not familiar enough with the jargon to enter the best search phrases, too).

4

You can use a tabular* and fill the space. To have columns in math mode use array package and >{$} as I did.

\documentclass[12pt]{article}
\usepackage{amsmath,array}
\renewcommand{\u}[1]{\underline{#1}}

\begin{document}
\begin{tabular*}{\linewidth}{>{$}r<{$}@{\extracolsep{\fill}}>{$}r<{$}>{$}r<{$}}
    5 & 5 & 5\\
    \u{+4} &\u{+4} &\u{+4}
\end{tabular*}
\end{document}

You can simplify, as suggested by Mico, by defining a new column type:

\documentclass[12pt]{article}
\usepackage{amsmath,array}
\renewcommand{\u}[1]{\underline{#1}}

\newcolumntype{R}{>{$}r<{$}}

\begin{document}
\begin{tabular*}{\linewidth}{R@{\extracolsep{\fill}}RR}
    5 & 5 & 5\\
    \u{+4} &\u{+4} &\u{+4}
\end{tabular*}
\end{document}
  • To simplify the setup, one could issue the instruction \newcolumntype{R}{>{$}r<{$}} in the preamble and then type begin{tabular*}{\linewidth}{R@{\extracolsep{\fill}}RR} in the body of the document. – Mico Aug 1 '15 at 2:15
  • Thank you! This is exactly what I have been looking for. Is there a way to redefine the tabular* environment so that it takes the form just written by default (default up to the number of columns, anyway)? That is, it seems like making several environments like this might get tiresome if the number of columns varies and the tried-and-true copy/paste technique fails. – Clayton Aug 1 '15 at 2:15
  • @Mico: Thanks! (at)HarishKumar: The suggestion by Mico answered my question, I believe. One last thing regarding the structure of the document (although not important): is it possible to get the environment centered? This part seems more complicated to explain. Essentially, I just want the whitespace to be equal everywhere horizontally. – Clayton Aug 1 '15 at 2:23
  • @Clayton - The tabular* method, when used with the @{\extracolsep{\fill}} device, will equalize the amounts of intercolumn whitespace, while the widths of the columns themselves will depend on what's in the columns. If, instead, you want the columns themselves to occupy equal widths, you should probably use a tabularx environment instead. – Mico Aug 1 '15 at 2:32
  • @Mico: I'm not sure if you understood me; the intercolumn space is equal and therefore perfect! I'm also wanting the space from the edge of the page to be equal with the intercolumn space (if that makes sense). I don't care so much about the column width (unless that is a thing that becomes necessary to care about) as much as I do overall presentation, and equally spaced across the page looks slightly better in my opinion as opposed to offset to the right-hand side due to the right alignment. – Clayton Aug 1 '15 at 2:35
2

(Answer updated to incorporate the OP's follow-up comments.)

If I understand your requirements correctly, you want

  • the numbers within each column to be decimal-aligned

  • the columns themselves to be centered

  • automatic adjustment to the column widths and/or the amounts of intercolumn whitespace so that the entire table takes up the width of the text block.

One way to achieve these objectives is to use a tabularx environment, load the dcolumn package to decimal-align the numbers, and use a modified (i.e., centered) form of the X column type to typeset the headers of each row.

enter image description here

\documentclass[12pt]{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcommand\mc[1]{\multicolumn{1}{Y}{#1}}

\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}} % "decimal-aligned" column type

\usepackage[normalem]{ulem} % for "safe underlining"
\renewcommand\u[1]{\uline{#1}}

\begin{document}
\hrule % just to illustrate width of text block
\smallskip\noindent
\begin{tabularx}{\linewidth}{ *{4}{d{2.0}} }
\mc{Header 1} & \mc{Header 2} & \mc{Header 3} & \mc{Header 4}\\
    5 & 5 & 5 & 5\\
    \u{+4} &\u{+4} &\u{+4} &\u{+4}\\
\end{tabularx}
\end{document}
  • The use of tabularx might produce the more-desirable effect I'm looking for in this endeavor. I'm going to try to create a command that will produce the addition symbols but still allow me to use centered columns. In theory, this will equalize the space between objects and the edge of the page (right?) – Clayton Aug 1 '15 at 2:44
  • @Clayton - Do check if changing the definition of the Z column type from \newcolumntype{Z}{>{\raggedleft\arraybackslash}X} to \newcolumntype{Z}{>{\centering\arraybackslash}X} does what you're looking to achieve. – Mico Aug 1 '15 at 2:48
  • +1: You've earned the points (and then some!). Thanks for the help, Mico! Unrelated question: suggestions of places to learn this stuff as well as you? As a mathematician, I use this LaTeX all of the time, but I'm not very well versed in its capabilities. Maybe a favorite reference? – Clayton Aug 1 '15 at 3:00
  • @Clayton - Gratzer's "More Math into LaTeX" (4th ed 2007) is a very good general intro to LaTeX. Herbert Voss's "Typesetting tables with LaTeX" and "Typesetting mathematics with LaTeX" (pdf versions available online, I think) are also great. "The LaTeX Companion" (2nd ed 2004) is a fabulous and very complete reference for all-things LaTeX2e. Last but not least, do peruse this site for ideas and solutions. :-) – Mico Aug 1 '15 at 3:13
  • @Clayton - Do also peruse What are good learning resources for a LaTeX beginner? – Mico Aug 1 '15 at 3:17

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.