Multiple \arrayrulecolor per line with booktabs - latex

Taking this to tex.stackexhange.
The problem
Let's say I want to have more than rules in two different colors in the same row ... how can I make it work?
Here is a minimal example:
\documentclass{article}
\usepackage{colortbl}
\usepackage{booktabs}
\begin{document}
\begin{table}[ht]
\begin{tabular}{llll}
\toprule
A & B & alpha & beta\\
\arrayrulecolor{black} \cmidrule[1pt](l){1-1} \arrayrulecolor{red} \cmidrule[1pt](r){2-2} \arrayrulecolor{black} \cmidrule[1pt](l){3-3} \arrayrulecolor{black} \cmidrule[1pt](r){4-4}
1 & 5 & 10 & 100\\
2 & 6 & 11 & 101\\
2 & 7 & 12 & 102\\
3 & 8 & 13 & 103\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
What happens is that with each call of \arrayrulecolor{}, the corresponding cmidrule is moved down. Is there a solution to this?
The problem with the earlier solution
The solution mentioned in the comments has some trouble when you have different widths in one row. Consider this example:
\documentclass{article}
\usepackage{colortbl}
\usepackage{booktabs}
\newcommand{\corcmidrule}[1][2pt]{% \corcmidrule[<len>]
\\[\dimexpr-\normalbaselineskip-\belowrulesep-\aboverulesep-#1\relax]%
}
\begin{document}
\begin{table}[ht]
\begin{tabular}{llll}
\toprule
A & B & alpha & beta\\
\arrayrulecolor{black}\cmidrule[1pt](l){1-1}
\corcmidrule[2pt]\arrayrulecolor{black}\cmidrule[2pt](r){2-2}
\corcmidrule[5pt]\arrayrulecolor{black}\cmidrule[5pt](l){3-3}
\corcmidrule[1pt]\arrayrulecolor{black}\cmidrule[1pt](r){4-4}
1 & 5 & 10 & 100\\
2 & 6 & 11 & 101\\
2 & 7 & 12 & 102\\
3 & 8 & 13 & 103\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
What's wrong with this?
Consider what it looks like without the macro:
\documentclass{article}
\usepackage{colortbl}
\usepackage{booktabs}
\newcommand{\corcmidrule}[1][2pt]{% \corcmidrule[<len>]
\\[\dimexpr-\normalbaselineskip-\belowrulesep-\aboverulesep-#1\relax]%
}
\begin{document}
\begin{table}[ht]
\begin{tabular}{llll}
\toprule
A & B & alpha & beta\\
\arrayrulecolor{black}\cmidrule[1pt](l){1-1}
\cmidrule[2pt](r){2-2}
\cmidrule[5pt](l){3-3}
\cmidrule[1pt](r){4-4}
1 & 5 & 10 & 100\\
2 & 6 & 11 & 101\\
2 & 7 & 12 & 102\\
3 & 8 & 13 & 103\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
The macro might need a modification.

Related

How to get rid of vertical space?

How can I get rid of vertical extra space in latex? I am getting unnecessary space between the table and the figure in the latex appendix.
\documentclass{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tabularray}
\begin{document}
\chapter{Appendix}
\begin{table}[!ht]
\centering
\begin{tabular}{|l|l|l|l|}
\hline
Sender & Receiver & Total(Bits) & Frame Rate (ms) \\ \hline
VCU & ACU & 88 & 10 \\ \hline
VCU & BCU & 488 & 10 \\ \hline
VCU & DataRecorder & 536 & 10 \\ \hline
\end{tabular}
\end{table}
\begin{figure}
\centering
\includegraphics[width = \textwidth,trim={2.3cm 2.3cm 2.3cm 2.3cm},clip]{Device Graphics.pdf}
\caption{Device Block}
\label{fig:DeviceOverview}
\end{figure}
\end{document}
I tried using [H] in front of image but did not work instead [H] got printed in script
You have to specify where your images is allowed to be placed. To give latex the best possible chances to find a good location, you can use [htbp] to allow the image to be placed here, at the top, at the bottom or on a separate float page:
\documentclass{scrbook}
%\usepackage[utf8]{inputenc} % default since several years
%\usepackage{tabularray} % nothing to do with the question
\usepackage{graphicx}
\begin{document}
\chapter{Appendix}
\begin{table}[!ht]
\centering
\begin{tabular}{|l|l|l|l|}
\hline
Sender & Receiver & Total(Bits) & Frame Rate (ms) \\ \hline
VCU & ACU & 88 & 10 \\ \hline
VCU & BCU & 488 & 10 \\ \hline
VCU & DataRecorder & 536 & 10 \\ \hline
\end{tabular}
\end{table}
\begin{figure}[htbp]
\centering
\includegraphics[width=\textwidth,trim={2.3cm 2.3cm 2.3cm 2.3cm},clip]{example-image-duck}
\caption{Device Block}
\label{fig:DeviceOverview}
\end{figure}
\end{document}

No space between the text and the border line in a table and misalignment in the border

I have created the following table in LaTeX:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\begin{tabular}{c||cccc||cccc||}
& \multicolumn{4}{c}{Group1} & \multicolumn{4}{c}{Group2} \\ \cline{2-9}
& \rotatebox{90}{Column First} & \rotatebox{90}{Column Second} & \rotatebox{90}{Column Third} & \rotatebox{90}{Column Fourth} & \rotatebox{90}{Column Fifth} & \rotatebox{90}{Column Sixth} & \rotatebox{90}{Column Seventh} & \rotatebox{90}{Column Eighth} \\ \hline \hline
Row 1 & 1 & 2 & 3 & 4 & 5\% & 6\% & 7\% & 8\% \\
Row 2 & 9 & 10 & 11 & 12 & 13\% & 14\% & 15\% & 16\% \\
\end{tabular}
\end{table}
\end{document}
However, as the blue arrow shows, there is no space between the text and the borderline and the top border is nearly stuck to the word "Seventh". Is there any way that I can create some space there?
There is also another issue regarding the table border. I don't understand why the left and right borders (indicated by red arrows) are not in the same height and the right one is shorter. Is there a way to fix this and make their height equal?
there is no double line besides "Group 2" because you are using c as argument for the multicolumn, which says "draw the cell without lines". If you want the lines to be drawn, use c||
to get more space above "Column Seventh", you can manually add \space or similar
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\begin{tabular}{c||cccc||cccc||}
& \multicolumn{4}{c}{Group1} & \multicolumn{4}{c||}{Group2} \\ \cline{2-9}
& \rotatebox{90}{Column First} & \rotatebox{90}{Column Second} & \rotatebox{90}{Column Third} & \rotatebox{90}{Column Fourth} & \rotatebox{90}{Column Fifth} & \rotatebox{90}{Column Sixth} & \rotatebox{90}{Column Seventh\space} & \rotatebox{90}{Column Eighth} \\ \hline \hline
Row 1 & 1 & 2 & 3 & 4 & 5\% & 6\% & 7\% & 8\% \\
Row 2 & 9 & 10 & 11 & 12 & 13\% & 14\% & 15\% & 16\% \\
\end{tabular}
\end{table}
\end{document}

latex: color one column

I would like to create a table like the following:
in which only one column has a color and the color is applied to alternate rows.
The basic table would be:
\documentclass{article}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{float}
\usepackage[margin=2cm,left=2.5cm,includefoot]{geometry}
\begin{document}
\begin{table}[!h]
\centering
\caption{HS Sections}
\begin{tabular}{ccc}
& node & degree \\
\hline
& 1 & 1 \\
& 2 & 2 \\
& 3 & 2 \\
& 4 & 4 \\
& 5 & 2 \\
& 6 & 2 \\
& 7 & 1 \\
\hline
\end{tabular}
\label{tab:Table}
\end{table}
\end{document}
You could either manually use \cellcolor{...} to colour the cells you want or you could cheat a bit by colouring the whole rows in alternate colours and then overpaint the cells of the first column with white:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{float}
\usepackage[margin=2cm,left=2.5cm,includefoot]{geometry}
\begin{document}
\begin{table}[!h]
\centering
\caption{HS Sections}
\rowcolors{2}{cyan!50}{white}
\begin{tabular}{>{\cellcolor{white}}cc}
node & degree \\
\hline
1 & 1 \\
2 & 2 \\
3 & 2 \\
4 & 4 \\
5 & 2 \\
6 & 2 \\
7 & 1 \\
\hline
\end{tabular}
\label{tab:Table}
\end{table}
\end{document}

Vertical align in table on Latex

I want to make a table and print the text in the middle of all cases. I use the array package and I wrote:
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|m{2.5cm}|m{2.5cm}|m{2.5cm}|m{6cm}|}
But then Latex says that I missed one '$' or one '{' in my table... but there is only text, so I don't understand. When I switched to :
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|C{2.5cm}|C{2.5cm}|C{2.5cm}|C{6cm}|}
all is ok and works... but it's ugly. I read a lot of stuff about that on the site already but never fixed my obvious-like problem :/
Here is a minimal example of the layout I use for tables.
Different combinations of sizing and formatting are shown.
I am not sure if this is what you asked for. Please reformulate your question, if it is not.
\documentclass{scrartcl}
\usepackage{booktabs} % \cmidrule in tables
% \usepackage{caption} % Nice Captions
% \usepackage{longtable} % Tables larger than one page
% \usepackage{multirow} % Mergings Cells
% \usepackage{multicol} % Merging Cells
\usepackage{tabularx}
\begin{document}
\begin{table}
\caption{Some caption, for tables always above}
\label{some label}
\begin{tabularx}{0.99\textwidth}{>{\centering}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}p{0.5cm}>{\raggedleft}p{3cm}>{\raggedright}X>{\raggedleft\arraybackslash}X}
\toprule
\textsc{Foobar} & $a$ & $b$ & $c$ & $d$ & $e$ & $f$ \\
\cmidrule(r){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(l){7-7}
1 & 2 & 2 & 2 & 2 & 2 & 2 \\
2 & 4 & 5 & 4 & 4 & 4 & 4 \\
3 & 4 & 3 & 4 & 3 & 3 & 3 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

How to make all cells boldface in LaTeX table?

Given the following LaTeX table, how can I make all cells have a bold style?
MWE:
\documentclass{article}
\begin{document}
\begin{table}[ht]%
\centering
\caption[Data description]{Dataset description}
\begin{tabular}{|l|l|c|c|c|c|}
\hline
&Dataset &Samples&Numerical&Categorical&Class\\ \hline
1&Horse & 300 & 7 & 16 & 2 \\ \hline
2&Ionosphere & 351 & 32 & 1 & 2 \\ \hline
3&Band & 540 & 13 & 6 & 2 \\ \hline
4&Australian+MV& 621 & 3 & 11 & 2 \\ \hline
5&Hepatitis & 155 & 2 & 17 & 2 \\ \hline
6&Credit & 690 & 3 & 12 & 2 \\ \hline
\end{tabular}
\label{tab:data}
\end{table}
\end{document}
You can set the formatting of each column in the tabular argument with >{...}, see Tabular, make a column or a row emphasized:
\begin{tabular}{|>{\bfseries}l|>{\bfseries}l|>{\bfseries}c|>{\bfseries}c|>{\bfseries}c|>{\bfseries}c|}
Use the *{num}{str} syntax to make it shorter and more readable:
\begin{tabular}{*{2}{|>{\bfseries}l} *{4}{|>{\bfseries}c} |}\hline
I.e. 'two times flush left and bold', then 'four times centred and bold'. (Note that \bf is deprecated.)
Your MWE:
\documentclass{article}
\usepackage{array} % otherwise you get "Error: Illegal character in array arg."
\begin{document}
\begin{table}[ht]%
\centering
\caption[Data description]{Dataset description}
\begin{tabular}{*{2}{|>{\bfseries}l} *{4}{|>{\bfseries}c} |}\hline
&Dataset &Samples&Numerical&Categorical&Class\\ \hline
1&Horse & 300 & 7 & 16 & 2 \\ \hline
2&Ionosphere & 351 & 32 & 1 & 2 \\ \hline
3&Band & 540 & 13 & 6 & 2 \\ \hline
4&Australian+MV& 621 & 3 & 11 & 2 \\ \hline
5&Hepatitis & 155 & 2 & 17 & 2 \\ \hline
6&Credit & 690 & 3 & 12 & 2 \\ \hline
\end{tabular}
\label{tab:data}
\end{table}
\end{document}
You can also set the formatting of a whole row (less interesting in this case) - see for instance How to change a whole row of a table.
Note: you can make a single cell normal again by using \normalfont{}.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{table}[ht]%
% If you want all in bold face then enclose it between {\ bf }
\centering {\bf
\caption[Data description]{Dataset description}
\begin{tabular}{|l|l|c|c|c|c|}\hline
& Dataset & Samples & Numerical & Categorical & Class\\\hline
1 & Horse & 300 & 7 & 16 & 2 \\\hline
2 & Ionosphere & 351 & 32 & 1 & 2 \\\hline
3 & Band & 540 & 13 & 6 & 2 \\\hline
4 & Australian+MV& 621 & 3 & 11 & 2 \\\hline
5 & Hepatitis & 155 & 2 & 17 & 2 \\\hline
6 & Credit & 690 & 3 & 12 & 2 \\\hline
\end{tabular}
\label{tab:data}

Resources