Search code examples
latex

Wrap text in Latex


I have an issue with wrapping text in some columns of my table in latex. I do not get why the text is nicely wrapped insice each cell in the first three columns, but not in the last 2. Some of the text crosses over to the next column and appears on top of the other text, but the column format is supposed to be the same for all of them. Please could someone help me figure out what I am doing wrong?

Thank you!

My code is as follows.

\documentclass[12pt]{article}
\usepackage{setspace}
\usepackage[labelfont=bf, justification=justified, singlelinecheck=false]{caption}
\usepackage{afterpage}
\usepackage{longtable}
\usepackage[flushleft]{threeparttable}
\usepackage{array,booktabs,makecell}
\usepackage{caption}
\usepackage{changepage}
\setcounter{tocdepth}{1}
\usepackage{fullpage}
\usepackage[T1]{fontenc}
\usepackage{natbib}
\doublespacing
\usepackage{titlesec}
\usepackage{url}
\setlength{\bibsep}{0.0pt plus 0.3ex}
\renewcommand\refname{References}
\usepackage[capposition=top]{floatrow}
\usepackage[flushleft]{threeparttable}
\usepackage{array}
\usepackage{lipsum}
\usepackage{pdflscape}


\begin{document}

\begin{landscape}
\begin{table}[H]
  \caption{Data - Additional Information}
  \label{oa_tab:cis_additional}
  \centering 
  \begin{threeparttable}
    \begin{tabular}{p{6cm} >{\arraybackslash}p{6cm} >{\centering\arraybackslash}p{2.5cm} >{\arraybackslash}p{4cm} >{\arraybackslash}p{2.5cm} >{\arraybackslash}}
    %{m{15mm} m{70mm} m{18mm}}
    \hline\hline
\textbf{Question in language} &
  \textbf{Question in language} &
  \textbf{Days} &
  \textbf{Responses Options} &
  \textbf{Numbers} \\ \cline{1-5} 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
  Monday - Tuesday &
  \begin{tabular}[t]{@{}l@{}}1.- A\\      2.- B\\      3.- C\\      4.- D\\     \end{tabular} &
  \begin{tabular}[t]{@{}l@{}}- 1=A\\      - 2=B\\      - 3=C\\      - 4=D\\      - 5=E\end{tabular} \\ \hline
ELorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
  Monday - Tuesday &
  \begin{tabular}[t]{@{}l@{}}1.-   The meaning of life\\      2.- The meaning of life and the meaning of words\\      3.- Both equally\\      4.- Neither\\      \end{tabular} &
  \begin{tabular}[t]{@{}l@{}}- 0=A\\      - 1=B\\      (for meaning of life, vice versa for meaning of words)\end{tabular} \\
  \end{tabular}
    \hline \hline
\end{threeparttable}
\end{table}
\end{landscape}

\end{document}

Solution

  • Two problems:

    • The last two \hline are outside of the tabular environment. You can only use them inside

    • your inner tables use the l column type. This column type does not allow line breaks. If you want your text to wrap lines, use a column type which allows line breaks. Or get altogether rid of these nested tables. In your main table, you use columns which allows line breaks, so the inner tables aren't useful anyway.

    \documentclass[12pt]{article}
    \usepackage{setspace}
    \usepackage[labelfont=bf, justification=justified, singlelinecheck=false]{caption}
    \usepackage{afterpage}
    \usepackage{longtable}
    \usepackage[flushleft]{threeparttable}
    \usepackage{array,booktabs,makecell}
    \usepackage{caption}
    \usepackage{changepage}
    \setcounter{tocdepth}{1}
    \usepackage{fullpage}
    \usepackage[T1]{fontenc}
    \usepackage{natbib}
    \doublespacing
    \usepackage{titlesec}
    \usepackage{url}
    \setlength{\bibsep}{0.0pt plus 0.3ex}
    \renewcommand\refname{References}
    \usepackage[capposition=top]{floatrow}
    \usepackage[flushleft]{threeparttable}
    \usepackage{array}
    \usepackage{lipsum}
    \usepackage{pdflscape}
    
    
    \begin{document}
    
    \begin{landscape}
    \begin{table}[H]
      \caption{Data - Additional Information}
      \label{oa_tab:cis_additional}
      \centering 
      \begin{threeparttable}
        \begin{tabular}{p{6cm} p{6cm} >{\centering\arraybackslash}p{2.5cm} p{4cm} p{2.5cm}}
        %{m{15mm} m{70mm} m{18mm}}
        \hline\hline
    \textbf{Question in language} &
      \textbf{Question in language} &
      \textbf{Days} &
      \textbf{Responses Options} &
      \textbf{Numbers} \\ \cline{1-5} 
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Monday - Tuesday &
      1.- A\newline      2.- B\newline     3.- C\newline    4.- D\newline &
      - 1=A\newline      - 2=B\newline     - 3=C\newline      - 4=D\newline      - 5=E \\ \hline
    ELorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Monday - Tuesday &
      1.-   The meaning of life\newline      2.- The meaning of life and the meaning of words\newline      3.- Both equally\newline      4.- Neither &
      - 0=A\newline      - 1=B\newline      (for meaning of life, vice versa for meaning of words) \\
          \hline \hline
      \end{tabular}
    \end{threeparttable}
    \end{table}
    \end{landscape}
    
    \end{document}
    

    I think your table might look better using left aligned text instead of justified text. Here an example with the more flexible tabularray package:

    \documentclass[12pt]{article}
    \usepackage{setspace}
    \usepackage[labelfont=bf, justification=justified, singlelinecheck=false]{caption}
    \usepackage{afterpage}
    \usepackage{longtable}
    \usepackage[flushleft]{threeparttable}
    \usepackage{array,booktabs,makecell}
    \usepackage{caption}
    \usepackage{changepage}
    \setcounter{tocdepth}{1}
    \usepackage{fullpage}
    \usepackage[T1]{fontenc}
    \usepackage{natbib}
    \doublespacing
    \usepackage{titlesec}
    \usepackage{url}
    \setlength{\bibsep}{0.0pt plus 0.3ex}
    \renewcommand\refname{References}
    \usepackage[capposition=top]{floatrow}
    \usepackage[flushleft]{threeparttable}
    \usepackage{array}
    \usepackage{lipsum}
    \usepackage{pdflscape}
    
    \usepackage{tabularray}
    
    
    \begin{document}
    
    \begin{landscape}
    \begin{table}[H]
      \caption{Data - Additional Information}
      \label{oa_tab:cis_additional}
      \centering 
    %  \begin{threeparttable}
        \begin{tblr}{
          colspec={X[l]X[l]X[c,0.5]X[l]X[l]}
        }
        %{m{15mm} m{70mm} m{18mm}}
        \hline\hline
    \textbf{Question in language} &
      \textbf{Question in language} &
      \textbf{Days} &
      \textbf{Responses Options} &
      \textbf{Numbers} \\ \cline{1-5} 
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      {Monday\\ --\\ Tuesday} & {1.- A\\ 2.- B\\ 3.- C\\ 4.- D} &
      {- 1=A\\ - 2=B\\ - 3=C\\ - 4=D\\ - 5=E} \\ \hline
    ELorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua &
      {Monday\\ --\\ Tuesday} &
      {1.- The meaning of life\\  2.- The meaning of life and the meaning of words\\ 3.- Both equally\\ 4.- Neither} &
      {- 0=A\\ - 1=B\\ (for meaning of life, vice versa for meaning of words)} \\
          \hline \hline
      \end{tblr}
    %\end{threeparttable}
    \end{table}
    \end{landscape}
    
    \end{document}
    

    enter image description here