1

I have a table that, due to long text, is not fitting on the (landscape) page. consider this code as an example (packages used in the actual document, so included them to avoid compatibility issues):

\documentclass[a4paper,12pt]{article}

\usepackage{natbib} 
\usepackage[utf8]{inputenc}
\usepackage[english]{babel} 
\usepackage[T1]{fontenc}
\usepackage{graphicx} 
\usepackage{lmodern} 
\usepackage{csquotes} 
\usepackage[ddmmyyyy]{datetime} 
\usepackage{color} 
\usepackage{enumerate} 
\usepackage{enumitem} 
\usepackage{float} 
\usepackage{lscape} 
\usepackage{booktabs} 
\usepackage{tabularx} 
\usepackage{mathtools}

\begin{document}

\begin{landscape}
    \begin{table}[]
        \begin{tabular}{@{}|c|c|c|c|c|c|c|c|c|@{}}
            \toprule
            \textbf{header 1} & \multicolumn{2}{c|}{header 2}   & \multicolumn{4}{c|}{header 3} & \multicolumn{2}{c|}{header 4}\\ \midrule
            \textbf{tex1}     &  foo       & text here          & some more, longer text &really long text here that makes table wide&text&longer text&more text& text that is also really long \\ \bottomrule
        \end{tabular}
        \caption{individuals reactions by actor-specific characteristics}
        \label{tab:individuals reactions by actor-specific characteristics}
    \end{table}
\end{landscape}

\end{document}

I cannot get other solutions like this one to work that are using p{width} or resizebox which are mentioned elsewhere due to the \begin{tabular}{@{}|c|c|c|c|c|c|c|c|c|@{}}-line in my code.

As I'm rather new to LaTeX, could someone point me to the best solution? Thanks a lot and all the best.

Ivo
  • 361

2 Answers2

3

With use of tabularx you can obtain:

enter image description here

\documentclass[a4paper,12pt]{article}

\usepackage{lscape}
\usepackage{booktabs, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{landscape}
    \begin{table}
    \setlength\tabcolsep{3pt}
        \begin{tabularx}{\linewidth}{cccCCcccC}
            \toprule
            \textbf{header 1} & \multicolumn{2}{c}{header 2}   & \multicolumn{4}{c}{header 3} & \multicolumn{2}{c}{header 4}\\ 
            \cmidrule(l){1-1}\cmidrule(l){2-3}\cmidrule(l){4-7}\cmidrule(l){8-9}
            \textbf{tex1}     &  foo       & text here          & some more, longer text &really long text here that makes table wide&text&longer text&more text& text that is also really long \\ \bottomrule
        \end{tabularx}
        \caption{individuals reactions by actor-specific characteristics}
        \label{tab:individuals reactions by actor-specific characteristics}
    \end{table}
\end{landscape}

\end{document}                                     
Zarko
  • 296,517
0

The solution coming closest to what I wanted is using \newcolumntype{P} and then defining the column widths manually (see below).

\documentclass[a4paper,12pt]{article}

\usepackage{float} 
\usepackage{lscape} 
\usepackage{booktabs} 
\usepackage{tabularx} 
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{table}[H]
    \begin{tabular}{|P{2cm}||P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}||}
        \hline
        \textbf{position} & \multicolumn{2}{P{2cm}|}{text no. one} & \multicolumn{4}{c|}{text}                         & \multicolumn{2}{c|}{text no. two} \\ \hline
        \textbf{other} & \multicolumn{2}{c|}{-} & \multicolumn{2}{c|}{towards no. one} & \multicolumn{2}{c|}{away from no. one} & \multicolumn{2}{c|}{-} \\ \hline
        \textbf{two line text} & low          & high          & low          & high           & low          & high          & low          & high           \\ \hline
    \end{tabular}
\end{table}

\end{document}
Ivo
  • 361