1

I have this MWE:

\documentclass[a4paper,12pt]{article}
\usepackage{color}
\usepackage{colortbl}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{array}
\begin{document}
\begin{longtable}{|p{.3\textwidth}|>{\raggedright\arraybackslash}p{.7\textwidth}|}
\hline
\cellcolor{gray!30}Donnée & \cellcolor{gray!30}Définition \\
\hline
\multicolumn{2}{|p{\textwidth}|}{\cellcolor{green!30} \centering \bfseries Semis} \\
\hline
text & text
\end{longtable}
\end{document}

Here's what I get : enter image description here

Why the second line is not entirely colored in green? How to fix it?

Second question : how to center vertically my text inside the columns?

  • you will have had warnings about over full boxes: |p{.3\textwidth}|>{\raggedright\arraybackslash}p{.7\textwidth}|} is too wide by the width of 3 rules and 6 tabcolsep. \multicolumn{2}{|p{\textwidth}|} is a different width, it is just wider that textwidth by the width of 2 rules and 4 tabcolsep, this difference accounting for the colour gap. – David Carlisle Jun 30 '14 at 10:19
  • Thanks for explanation, how to fix that? – Hunsu Jun 30 '14 at 10:23
  • see for example tex.stackexchange.com/a/150987/1090 – David Carlisle Jun 30 '14 at 10:26
  • Comment aside: don't forget loading inputenc to obtain accented letters. – Bernard Jun 30 '14 at 10:50
3

It was enough to replace the p specifier in \multicolumn to have both a simpler code and a solution to your problem.

By the way, since you load longtable and tabularx, you might consider loading ltablex instead: it loads the former and extends the possibilities of longtable to the tabularx environment (essentially without changing its syntax). Also, you don't need to load color if you load xcolor.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{colortbl}
\usepackage{ltablex} 
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\begin{longtable}{|p{.3\textwidth}|>{\raggedright\arraybackslash}p{.7\textwidth}|}
\hline
\rowcolor{gray!30}Donnée & Définition \\
\hline
\multicolumn{2}{|c|}{\cellcolor{green!30}\bfseries Semis} \\
\hline
text & text\\
\hline
\end{longtable}

\end{document} 

enter image description here

For vertically centred cell contents, the simplest thing to do is to use tabularx, redefining the X column type as m{…} rather than p{…}. The cellspace or makecell package allow to have less tight vertical spacing in cells, unfortunately, none works with colour commands in tables — cellspace works with l, c or c types, though, as one can see in the middle row of the following example:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{colortbl}
\usepackage{ltablex}
\renewcommand{\tabularxcolumn}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{Y}{ >{\hsize=0.3\hsize}X}
\newcolumntype{Z}{ >{\hsize=0.7\hsize}X}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{booktabs}

\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{6pt}
\addparagraphcolumntypes{X, Y, Z}

\begin{document}

\begin{tabularx}{\linewidth}{|Y|Z|}
\hline
\rowcolor{gray!30}Donnée & Définition : Text text text text text text text text text text text text text text text text text \\
\hline
\multicolumn{2}{|Sc|}{\cellcolor{green!30}\bfseries Semis} \\
\hline
text & Text with descenders: gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy gjpqy\\
\hline
\end{tabularx}

\end{document} 

enter image description here

  • The font axes is vertically centred, I think. Look at the lowercase letters. If you centre capital letters, lowercase won't be: it's an optical effect due to the very tight vertical spacing in default latex tables. Anyway, I'll modify my code in a moment, I hope you'll it better looking. – Bernard Jun 30 '14 at 11:49
  • In this example yes. But if I have big paragraph in the second column? – Hunsu Jun 30 '14 at 11:54
  • @user230137: I posted a code with an example of what can be done with multilined paragraphs. Vertical spacing is not wholly satisfactory, due to problems with colour in tables. – Bernard Jun 30 '14 at 13:08
0
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{ltablex,ragged2e}
\renewcommand{\tabularxcolumn}[1]{m{#1}}% or {>{\RaggedRight}m{#1}} 
\usepackage{booktabs}
\usepackage{blindtext}  
\begin{document}

\begin{tabularx}{\linewidth}{| p{0.3\textwidth} | X |}\hline
\rowcolor{gray!30}Donnée & Définition : Text text text text text text text text text text text text text text text text text\\\hline
\multicolumn{2}{|c|}{\cellcolor{green!30}\bfseries Semis} \\\hline
text & \blindtext\\
\hline
\end{tabularx}

\end{document} 

The missing lines in the output are only a problem with the pdf viewer and the magnification.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.