[Tex/LaTex] Specify column width using tabularx Table environment

column widthtabularx

I am working on a complex document in LaTeX and have various types of tables. I'd like the 2nd column wider and the 3rd column rather narrow. I tried different ideas with this code chunk {c *6{>{}X}}.

I am thinking something like this for width: 1st column 25pts, 2nd column 300pts, last column 80pts. The idea is, to define column widths right within the table environment, but not in the document preamble. I reviewed the tabularx manual, some posts here, I'd like to see a full example of the table syntax, below what I have.

\begin{table}[!htbp] 
\setlength{\tabcolsep}{6pt}
{\tabcolsep=0pt\def\arraystretch{1}
    \caption{caption} \label{tab:some-table}
    \begin{tabularx}{\textwidth}{c *6{>{}X}}\midrule
        & \centering\hspace{-1in} some text & \centering\hspace{-0.75in} some text \tabularnewline\midrule 
        1 \hspace{12pt}  & some very very long text and much much more text & \multirow{5}{\linewidth}{some text}\tabularnewline 
        2 \hspace{12pt}  & some text  & \tabularnewline
        3 \hspace{12pt}  & some text   &  \tabularnewline
        4 \hspace{12pt}  & some text  &   \tabularnewline
        5 \hspace{12pt}  & some text &  \tabularnewline
        \midrule
\end{tabularx}}
\end{table}

Best Answer

Here are three versions of your table. In the first one, I have used a regulat tabular environment with the column widths you specified in your question. However, the table is a lot larger than the textwidth (at least of a standard article class; indicated by the red lines). I therefore suggest to use tabularx with either one or two X type columns. Which of them is better depends on the contents of the actual table. I have also replaced two of the \midrule commands with \toprule and \bottomrule respectively. To horizontally center the column headers with respect to the corresponding columns, I have used the \thead command from the makecellpackage:

enter image description here

\documentclass{article}
\usepackage{tabularx}

\usepackage{multirow}
\usepackage{booktabs}

\usepackage{makecell}
\renewcommand{\theadfont}{\normalfont}

\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}

\begin{table}[!htbp] 
    \caption{caption} \label{tab:some-table}
    \begin{tabular}{p{25pt}p{300pt}p{80pt}}
    \toprule
        & \thead{some text} & \thead{some text} \\
    \midrule 
        1  & some very very long text and much much more text & \multirow{5}{*}{some text} \\ 
        2  & some text & \\
        3  & some text & \\
        4  & some text & \\
        5  & some text & \\
        \bottomrule
\end{tabular}
\end{table}

\begin{table}[!htbp] 
    \caption{caption} \label{tab:some-table}
    \begin{tabularx}{\textwidth}{cXX}
    \toprule
        & \thead{some text} & \thead{some text} \\
    \midrule 
        1  & some very very long text and much much more text & \multirow{5}{*}{some text} \\ 
        2  & some text & \\
        3  & some text & \\
        4  & some text & \\
        5  & some text & \\
        \bottomrule
\end{tabularx}
\end{table}

\begin{table}[!htbp] 
    \caption{caption} \label{tab:some-table}
    \begin{tabularx}{\textwidth}{cXl}
    \toprule
        & \thead{some text} & \thead{some text} \\
    \midrule 
        1  & some very very long text and much much more text & \multirow{5}{*}{some text} \\ 
        2  & some text & \\
        3  & some text & \\
        4  & some text & \\
        5  & some text & \\
        \bottomrule
\end{tabularx}
\end{table}
\end{document}

Update with the apa6 document class: I have used \multirow{6}{=}{...}. 6 is needed in order to vertically center the text since the contents of the column take up a total of 6 lines. = ensures that the text is only as wide as the corensponding column.

\documentclass[doc,natbib,floatsintext,12pt,noextraspace]{apa6}
\shorttitle{short title}
\usepackage{tabularx}

\usepackage{multirow}
\usepackage{booktabs}

\usepackage{makecell}
\renewcommand{\theadfont}{\normalfont}

\begin{document}


\begin{table}[!htbp] 
    \caption{caption} \label{tab:some-table}
    \begin{tabularx}{\textwidth}{cXX}
    \toprule
        & \thead{some text} & \thead{some text} \\
    \midrule 
        1  & some very very long text and much much more text & \multirow{6}{=}{some text some text some text some text some text some text some text some text some text} \\ 
        2  & some text & \\
        3  & some text & \\
        4  & some text & \\
        5  & some text & \\
        \bottomrule
\end{tabularx}
\end{table}

\end{document}

enter image description here

Related Question