Latex tables

I wrote my Master’s Thesis in Latex since it allows me to focus on the content of the paper rather than on how it looks. Having the content it is possible to change the output style by changing the Latex code. Recently, I faced a problem of creating tables with background color. After some time Googleing I found some information. On its basis a created table I wanted (look below).

table Latex tables

The Latex code for the document containing page with such a output looks as follows:


\documentclass{article}

\usepackage[english]{babel}
\usepackage{colortbl}

\begin{document}

\section{Table test}

\begin{tabular}{|l|p{8cm}|}

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Id} & 
UC01 \\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Title} & 
Delivering a list of LOs\\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Description} & 
Providing a list of existing Learning Objects created from 
the informal knowledge stored in the repository.\\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Actors} & 
A02, A03 \\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Initial occurence} 
& Claim for list of LOs.\\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{
Exceptional occurence}
 &  Unsuccesful connection to the repository\\

\hline

\multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Related use cases} & 
UC07, UC17\\

\hline

\end{tabular}

\end{document}


So whenever You use tables with coloured cells:

  • inlcude colortbl package
  • use \multicolumn{1}{| >{\columncolor[rgb]{0.8,0.8,0.8}}r|}{Id}, where in first bracket you define the number of columns this applies to, in the second the color and in the last the content of the cell(s)

9 Responses to “Latex tables”


  • Thnak you very much! :D I was also working on use cases hahaha

  • Hello,
    nice tips, thanks. Would you know how we can change the color of the borders of a table in LaTeX? Have you tried this before?

    thanks

  • Unfortunately I haven’t tried that. I hope Google will me more of the help :)

  • hi,

    thax for posting. exactly what i was looking for.

    can you add an explanation how the rgb code works? must be a value between 0 and 1. i only found either hex or decimal numbers as reverences to the color. how do i get the right number for a specific color, if i got the hex number or decimal number?

    cheers

  • Thanks a lot!!!!

  • you can also use \columncolor{red} for example. For some reason the color model of RGB does not work (enabling values of 0-255) and it also breaks the printing of cells in a multirow cell. In other words yet more latex hack and pain. I could never have believed something so painfully necessary as colored columnds and rows could be implemented so painfully. In addition, the comments about latex separating content from presentation are ludicrous : its one of the most presentation orientated content management forums I have ever seen.

  • Zeid:
    Here is how to have colored \hline .
    Hope this helps,
    Jrouquie.

    \documentclass{article}
    \usepackage{array,colortbl,booktabs}
    \begin{document}

    \begin{tabular}{lc}
    \hline
    \arrayrulecolor{green} %always at the beginning of a table row
    a&a\\
    \midrule[2pt] %see booktabs.pdf
    b&b\\
    \hline
    \arrayrulecolor{black}
    c&c\\
    \hline
    \end{tabular}
    \end{document}

  • When I use \cellcolor[gray]{0.9} to color a cell (in a table) and convert to .pdf, the left and top borders of the cell, which were black get erased. How could I fix this problem. Thanks!

  • Thanks to you all!

    I was looking for the colored \hline and I got just what I wanted here.

    Cheers,

    D

Leave a Reply