5

I would like to have the midrule of my table to the same length as the width of my table. With the code that I currently have, this is the output that I get:

enter image description here

Can you please help me get the midrule to be the same size as my table's width?

Here is my code:

\documentclass[article, english]{book}

\usepackage{booktabs}
\usepackage[table,x11names,dvipsnames,table]{xcolor}
\usepackage{multicol}
\usepackage{tabularx}
\usepackage{ctable}


\begin{document}

\begin{table}[ht!]
%\centering
\setlength\tabcolsep{12pt}
\rowcolors{2}{blue!20}{white}
%\begin{tabular}{@{}llll@{}}
\begin{tabularx}{\textwidth}{llll}
\rowcolor{blue!50}
\textbf{Freq} & \textbf{Board} & \textbf{\begin{tabular}[c]{@{}l@{}}PTT\\   ACC\end{tabular}} & \textbf{MAP} \\
136.0125 & 508  & -62.1 & -61.5 \\
173.9875 & 508  & -61.2 & -61.1 \\
380.0125 & 508  & -59.8 & -59.9 \\
519.9875 & 508  & -59   & -58.8 \\
764.0625 & 508  & -56.8 & -56.6 \\
869.8875 & 508  & -55.8 & -55.7 \\
\midrule[2pt]
136.0125 & 3511 & -61.9 & \cellcolor[HTML]{FE0000}-55.0 \\
173.9875 & 3511 & -61.7 & \cellcolor[HTML]{FE0000}-55.0 \\
380.0125 & 3511 & -59.7 & \cellcolor[HTML]{FE0000}-54.5 \\
519.9875 & 3511 & -58.7 & \cellcolor[HTML]{FE0000}-54.3 \\
764.0625 & 3511 & -56.5 & \cellcolor[HTML]{FE0000}-53.4 \\
869.8875 & 3511 & -55.5 & \cellcolor[HTML]{FE0000}-53.0
\end{tabularx}                                      
\end{table}

\end{document} 
  • 4
    in tabularx at least one column should be X tyoe. otherwise use tabular table environment. – Zarko Mar 12 '18 at 23:39
5

The default behaviour or tabularx is to set a table of width given by the first argument - \textwidth in your case. Additionally, it requires the use of at least one X-column in order to stretch to that width. If none are supplied, there's really no need for a tabularx.

To this end, don't use tabularx, but tabular instead:

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage[table,x11names,dvipsnames,table]{xcolor}

\begin{document}

\begin{table}
  \centering
  \rowcolors{2}{blue!20}{white}%
  \begin{tabular}{ *{4}{l} }
    \rowcolor{blue!50}
    \textbf{Freq} & \textbf{Board} & \textbf{\begin{tabular}[c]{ @{} l @{} }PTT \\ ACC\end{tabular}} & \textbf{MAP} \\
    136.0125 & 508  & -62.1 & -61.5 \\
    173.9875 & 508  & -61.2 & -61.1 \\
    380.0125 & 508  & -59.8 & -59.9 \\
    519.9875 & 508  & -59   & -58.8 \\
    764.0625 & 508  & -56.8 & -56.6 \\
    869.8875 & 508  & -55.8 & -55.7 \\
    \specialrule{2pt}{0pt}{0pt}
    136.0125 & 3511 & -61.9 & \cellcolor[HTML]{FE0000}-55.0 \\
    173.9875 & 3511 & -61.7 & \cellcolor[HTML]{FE0000}-55.0 \\
    380.0125 & 3511 & -59.7 & \cellcolor[HTML]{FE0000}-54.5 \\
    519.9875 & 3511 & -58.7 & \cellcolor[HTML]{FE0000}-54.3 \\
    764.0625 & 3511 & -56.5 & \cellcolor[HTML]{FE0000}-53.4 \\
    869.8875 & 3511 & -55.5 & \cellcolor[HTML]{FE0000}-53.0
  \end{tabular}                                      
\end{table}

\end{document}

Note the use of \specialrule{<width>}{<above sep>}{<below sep>} to avoid coloured gaps between rows that have colours.

6

as i noted in my comment, table environment should has at least on ˛X type of column. since your table is your table is relatively narrow, is better to use tabular environment and rather gives some attention to column desing. for them i suggest to use S column type provided by package makecell, for column headers use \thead from package makecell. instead midrule i would rather use Xhline{<thiknes>} also from makecel:

\documentclass[article, english]{book}
\usepackage[table]{xcolor}
\definecolor{myred}{HTML}{FE0000}
\usepackage{siunitx}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\bfseries}

\begin{document}
    \begin{table}[ht!]
\centering
\setlength\tabcolsep{12pt}
\rowcolors{2}{blue!20}{white}
\begin{tabular}{S[table-format=3.4]
                S[table-format=4.0]
                S[table-format=-2.1]
                S[table-format=-2.1]
                }
\rowcolor{blue!50}
{\thead{Frequency}}
            & {\thead{Board}}
                    & {\thead{PTT\\ ACC}}
                            & {\thead{MAP}}         \\
136.0125    & 508   & -62.1 & -61.5                 \\
173.9875    & 508   & -61.2 & -61.1                 \\
380.0125    & 508   & -59.8 & -59.9                 \\
519.9875    & 508   & -59   & -58.8                 \\
764.0625    & 508   & -56.8 & -56.6                 \\
869.8875    & 508   & -55.8 & -55.7                 \\
    \Xhline{2pt}
136.0125    & 3511  & -61.9 & \cellcolor{myred}-55.0 \\
173.9875    & 3511  & -61.7 & \cellcolor{myred}-55.0 \\
380.0125    & 3511  & -59.7 & \cellcolor{myred}-54.5 \\
519.9875    & 3511  & -58.7 & \cellcolor{myred}-54.3 \\
764.0625    & 3511  & -56.5 & \cellcolor{myred}-53.4 \\
869.8875    & 3511  & -55.5 & \cellcolor{myred}-53.0
\end{tabular}
    \end{table}
\end{document}

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.