13

I'm using the array environment and have something like this:

\begin{array}{|c|c|}
   \hline 
   \textbf{x} & \textbf{y} \\ 
   \hline
   \hline
   t_1 & lon_1 \\ 
   \hline 
\end{array}

I would like to have the two columns to have the same width. How can I do that?

Werner
  • 603,163
BAD_SEED
  • 1,075
  • One way would be to use p{1.0cm} as in \begin{tabular}{|p{1.0cm}|p{1.0cm}|}, which necessitates the content being in math mode: $t_1$. – Peter Grill May 08 '12 at 18:12
  • @MarcoDaniel: Good point. I dislike this solution as you have to fix the width, and have to do additional tweaking if you want to switch from l, to c to r alignment, but only mentioned it as an option. – Peter Grill May 08 '12 at 18:21

1 Answers1

17

If you don't want to use the the p{<width>} column type where you have to fix the width to a specific amount you could typeset one row using the SetToWidest macro as defined below. This requires that you determine what the widest entry is and define that as the value of WidestEntry:

enter image description here

\documentclass{article}
\usepackage{calc}

\newcommand{\WidestEntry}{$lon_1$}%
\newcommand{\SetToWidest}[1]{\makebox[\widthof{\WidestEntry}]{#1}}%

\begin{document}
\begin{tabular}{|c|c|}
   \hline 
   \textbf{x} & \textbf{y} \\ 
   \hline
   \hline
   \SetToWidest{$t_1$} & $lon_1$ \\ 
   \hline 
\end{tabular}

or similarly using array:

\documentclass{article}
\usepackage{calc}

\newcommand{\WidestEntry}{$lon_1$}%
\newcommand{\SetToWidest}[1]{\makebox[\widthof{\WidestEntry}]{$#1$}}%

\begin{document}$
\begin{array}{|c|c|}
   \hline 
   \textbf{x} & \textbf{y} \\ 
   \hline
   \hline
   \SetToWidest{t_1} & lon_1 \\ 
   \hline 
\end{array}$
\end{document}

If you are ok with specifying the width of each column you can use the p{<width>} column type:

\documentclass{article}
\usepackage{array}

\begin{document}$
\begin{array}{|>{\centering\arraybackslash$} p{1.0cm} <{$} | >{\centering\arraybackslash$} p{1.0cm} <{$} |}
   \hline 
   \textbf{x} & \textbf{y} \\ 
   \hline
   \hline
   t_1 & lon_1 \\ 
   \hline 
\end{array}$
\end{document}
Peter Grill
  • 223,288