3

I have multiline table.

  \documentclass[11pt,table,a5paper]{article}
  \usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
  \usepackage{collcell}
  \usepackage{longtable}
  \usepackage{color}
  \usepackage{colortbl}
  \begin{document}
  \definecolor{textcol}{rgb}{.118, .565, 1.00}
  \definecolor{rowcol}{rgb}{.218, .565, 1.00}
  \textbf{Antioxidant/Longevity }
    ~\\
    {\renewcommand{\arraystretch}{1.8}\begin{longtable} {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
   \hline
   \rowcolor{textcol}
   \textbf{\textcolor{white}{Title1}} &\textbf{\textcolor{white}{Title2}}  &\textbf{\textcolor{white}{Title3}} &\textbf{\textcolor{white}{Title4}}\\
  \hline
  \rowcolor{rowcol}
   54880/RET2& -& 1&-\\
  \hline
  \rowcolor{rowcol}
   rs5746136/SOD2& First Line~\\
  ~\\
  Second Line & 1& some text\\
  \hline
  \end{longtable}}
  \end{document}

enter image description here

But output is not multiline. How can i fix this pblm?

David Carlisle
  • 757,742
manish
  • 9,111

2 Answers2

2

Does this code give what you want ? I use the makecell package; it allows me to replace changing \baselinestretch with the use of starred versions of \makecell or thead, the command that formats (multiline) cells. These starred versions add a small vertical spacing symmetrically at the top and the bottom of a cell, hence the content of the cells in the row is vertically centred, which is not the case with changing \baselinestretch.

I also use tabulary, a variation on tabularx such that, given the total width of the table, the width of a column is based on the widest cell in that column. As it does not work perfectly with \thead (no correct display of the \thead in the first column), I had to circumvent the problem by adding a first empty column.

 \documentclass[11pt,table,a5paper]{article}
  \usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
  \usepackage{collcell}
  \usepackage{ltablex}
  \usepackage{tabulary}
  \usepackage{color}
  \usepackage{colortbl}
  \usepackage{makecell}
  \renewcommand{\theadfont}{\color{white}\bfseries}%
  \renewcommand{\theadalign}{lc}
  \renewcommand{\cellalign}{lc}
  \definecolor{textcol}{rgb}{.118, .565, 1.00}
  \definecolor{rowcol}{rgb}{.218, .565, 1.00}

  \begin{document}

    \begin{tabulary}{\linewidth}{| l@{}L|L|C|L|}%{|*{4}{p{0.2\linewidth}|}}% {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
    \multicolumn{5}{l}{\bfseries Antioxidant/Longevity}\\
   \hline
   \rowcolor{textcol}%
   & \thead*{Title1} &\thead{{Title2}}  &\thead{Title3} &\thead{Title4}\\
  \hline
  \rowcolor{rowcol}
    & \makecell*{54880/RET2} & --& 1&--\\
  \hline
  \rowcolor{rowcol}
     &  rs5746136/SOD2& \makecell*{First Line\\Second Line}& 1& some text\\
  \hline
  \end{tabulary}

  \end{document} 

enter image description here

Bernard
  • 271,350
2

Don't use ~\\ for paragraph breaks (in the table or outside)

enter image description here

\documentclass[11pt,table,a5paper]{article}
  \usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
  \usepackage{collcell}
  \usepackage{longtable}
  \usepackage{color}
  \usepackage{colortbl}
  \begin{document}
  \definecolor{textcol}{rgb}{.118, .565, 1.00}
  \definecolor{rowcol}{rgb}{.218, .565, 1.00}
  \textbf{Antioxidant/Longevity }

    {\renewcommand{\arraystretch}{1.8}\begin{longtable} {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
   \hline
   \rowcolor{textcol}
   \textbf{\textcolor{white}{Title1}} &\textbf{\textcolor{white}{Title2}}  &\textbf{\textcolor{white}{Title3}} &\textbf{\textcolor{white}{Title4}}\\
  \hline
  \rowcolor{rowcol}
   54880/RET2& -& 1&-\\
  \hline
  \rowcolor{rowcol}
   rs5746136/SOD2& First Line

  Second Line & 1& some text\\
  \hline
  \end{longtable}}
  \end{document}
David Carlisle
  • 757,742