0

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}
8
  • What about p type columns? They might be better suited if you want to manually define the column widths.
    – leandriis
    Nov 5, 2019 at 18:39
  • Please also explain why you define a total of 7 columns, while you table only seem to need 3. Aldo, what's the reason for a 0pt tabcolsep and the repeated hspace commands in the frdt column?
    – leandriis
    Nov 5, 2019 at 18:40
  • That's fine, whatever works. How would I need to do this? Thanks! 7 columns? I am not sure what this code chunk does "{c *6{>{}X}}"
    – togedo
    Nov 5, 2019 at 18:41
  • p{25pt} p{300pt} p{80pt} in a regular tabular should work.
    – leandriis
    Nov 5, 2019 at 18:43
  • *6 would mean "repeat the following code/column specifier six times" . So you have six columns plus the one c type column which sums up to a total of 7
    – leandriis
    Nov 5, 2019 at 18:44

1 Answer 1

2

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

9
  • ALMOST! I like the second one, however, in the 3rd column (multirow) the text does not wrap to match the columns width. I tried \\ between words but that gives an error. Hence, longer than 80pts text just runs into the right margin.Also, I would like the text in the right column vertically centered.
    – togedo
    Nov 5, 2019 at 19:46
  • @togedo: I have added an update based on the second example. Hope this suits your needs.
    – leandriis
    Nov 5, 2019 at 19:56
  • YES 1000x thanks! Looks good, Latex reports a bad box Overfull \hbox but the table looks very good.
    – togedo
    Nov 5, 2019 at 20:16
  • @togedo: You're welcome. Do you get the overfull box warning with the table from my answer or if you fill in yome actual contents?
    – leandriis
    Nov 5, 2019 at 20:34
  • Not from your answer, but when I fill in my contents. I have a feeling the combination of packages and/or my customization is causing this; I am also getting some other errors with titlesec and ragged2e, but could not solve those issues. PDF looks fine.
    – togedo
    Nov 5, 2019 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .