![table with highlighted rows and cells](https://i.stack.imgur.com/7yJpn.png)
This solution uses the colortbl
package. The spacing in the first example is not perfect. In the second example this is corrected by using \mystrut
. The second example shows also other ideas like \textbf
and line breaking in a cell are shown.
The LaTeX code is shown below. For LyX, just copy the marked part to the LyX Preamble (you can set a default color and rule width!) and edit your table according to these examples...
\documentclass{article}
\usepackage{booktabs}
%%% add following to LyX Preamble...
\usepackage{xcolor}
\usepackage{array}
\usepackage{colortbl}
\newcommand{\highlightcolor}{red} %set default color of the box
\setlength\arrayrulewidth{3pt} %set default rule width of the box
\newcommand{\mystrut}[2]{\vrule height #1 depth #2 width 0pt}
\newcommand{\coloredhline}{\arrayrulecolor{\highlightcolor}\hline}
%%% ...until here
\begin{document}
\centering
%%% as ERT in LyX
\begin{tabular}{@{\hskip\tabcolsep\hskip\arrayrulewidth}l rlrl }
Table 1&\multicolumn{2}{c}{Model 1} &\multicolumn{2}{c@{}}{Model 2}\\
\midrule \midrule
\coloredhline
\multicolumn{1}{|l}{Variable Variable Variable 1 (Variable) Variable} &17.122 &* &3.481 &\multicolumn{1}{l|}{*}\\
\coloredhline
Variable Variable Variable 2 (Variable) Variable & 76.48 &* &0.441 &* \\
\cline{2-5}
\mystrut{12pt}{0pt}Variable Variable Variable 3 (Variable) Variable &\multicolumn{1}{|l}{117.132} &* &9.216 &\multicolumn{1}{l|}{*} \\
\cline{2-5}
\arrayrulecolor{black}\bottomrule
\end{tabular}
\bigskip
%%% as ERT in LyX: configure color and rulewidth of box
\renewcommand{\highlightcolor}{blue}
\setlength\arrayrulewidth{5pt}
\begin{tabular}{@{\hskip\tabcolsep\hskip\arrayrulewidth}p{5cm} rlrl }
Table 2&\multicolumn{2}{c}{Model 1} &\multicolumn{2}{c@{}}{Model 2}\\
\midrule \midrule \addlinespace[0pt]
\coloredhline
\multicolumn{1}{|p{5cm}}{\mystrut{10pt}{0pt}Variable Variable Variable 1 (Variable) Variable} &17.122 &* &3.481 &\multicolumn{1}{l|}{*}\\
\rowcolor{lightgray}
\multicolumn{1}{|p{5cm}}{Variable Variable Variable 2 (Variable) Variable} & 76.48 &* &0.441 &\multicolumn{1}{l|}{*}\\
\coloredhline
\arrayrulecolor{black}\addlinespace[0pt]\midrule
\textbf{Variable Variable Variable 3 (Variable) Variable} & 117.132 & * & 9.216 &* \\
\bottomrule
\end{tabular}
\end{document}
Always supply a complete, compilable document.
If you really want to use \texit
, you have to load \usepackage[T1]{fontenc}
in your preamble so that the <
will render correctly. Or you can just put the whole thing in math environment and use \mathit
instead.
Here is one method.
%\documentclass[10pt]{article}
\documentclass[preview,convert,border=5]{standalone}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{c c c c c c c}
\toprule
& \multicolumn{2}{c}{Model 1}&\multicolumn{2}{c}{Model 2}&\multicolumn{2}{c}{Model 3}\\
\midrule
Variable 1 & 12.758 & *** & 13.822 & *** & 2.123 & ***\\
\bottomrule
\multicolumn{7}{r}{\footnotesize$\mathit{*p < 0.05, ** p < 0.01, *** p < 0.001}$}\\
\end{tabular}
\end{document}
![enter image description here](https://i.stack.imgur.com/rBaip.png)
However, I don't like how the asterisks are separated from the p-values so I suggest that you eliminate some columns. This is what I did in the next method with the use of threepartable
package. (There is also a threeparttablex
package.)
%\documentclass[10pt]{article}
\documentclass[preview,convert,border=5]{standalone}
\usepackage{booktabs}
\usepackage[para]{threeparttable}
\begin{document}
\begin{table}
\centering
\caption{\label{tab:1} Your table}
\begin{threeparttable}
\begin{tabular}{c c c c c c c}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
Variable 1 & 12.758\tnote{***} & 13.822\tnote{***} & 2.123\tnote{***}\\
\bottomrule
\end{tabular}
\begin{tablenotes}\footnotesize
\item [*] $p<0.05$
\item [**] $p<0.01$
\item [***] $p<0.001$
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
![enter image description here](https://i.stack.imgur.com/xr71D.png)
Update
I have included \renewcommand{\arraystretch}{0.5}
inside a scope to make the effect local. I have replaced \footnotesize
with \tiny
but it's up to you if you want to resort to a smaller font size. (Just a caution: be kind to your eyes. :)
%\documentclass[10pt]{article}
\documentclass[preview,convert,border=5]{standalone}
\usepackage[T1]{fontenc}
\usepackage{threeparttable}
\usepackage{booktabs}
\begin{document}
{\renewcommand{\arraystretch}{0.5}
\begin{threeparttable}
\begin{tabular}{c c c c}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
Variable 1 & 12.758\tnote{***} & 13.822\tnote{***} & 2.123\tnote{***}\\
\bottomrule
\multicolumn{4}{r}{\tiny *$\mathit{p < 0.05}$, ** $\mathit{p < 0.01}$, *** $\mathit{p < 0.001}$}\\
\end{tabular}
\end{threeparttable}
}
\end{document}
![enter image description here](https://i.stack.imgur.com/SZnTG.png)
Best Answer
You may want to change the value of
\tabcolsep
, a length variable that equals (half) the width of the whitespace that separates columns. You use the command\setlength
to change the value of this length variable. The default value of this variable is6pt
(if the main text's font size is 10pt); a value of3pt
will give you a rather tightly-set group of columns.Separately, you may also want to consider eliminating the vertical rules between columns entirely. When one has a lot of columns, the vertical rules tend to take up a lot of space while not adding appreciably to the table's readability.
The following code and the associated image illustrate the effects that reducing the value of
\tabcolsep
from 6pt to 4, 3, and 2pt has. In all cases, I've eliminated all vertical rules.