5

I'm trying to create a tabular with all the cells centered. I have found a way to make this possible but all the cells have to contain:

\multicolumn{1}{|c|}{"Text"}

Is there a way to center the text without the need to add the multicolumn command everywhere?

Tabular

\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{a4wide}      
\usepackage[utf8]{inputenc}     
\usepackage{mathtools}
\usepackage{float}
\usepackage{makecell}
\usepackage{slashbox}
\usepackage{multirow}
\usepackage{array}

\begin{document}
\begin{table}[H]
\centering
\begin{tabular}    {|c|p{.7\textwidth/5}|p{.7\textwidth/5}|p{.7\textwidth/5}|p{.7\textwidth/5}|}
\hline
\multirow{2}{*}{\backslashbox{Distance [m]}{Module}} & \multicolumn{2}{c|}{Range Test [dBm]} & \multicolumn{2}{c|}{Throughput Test [Kbps]}\\\cline{2-5}
   & \multicolumn{1}{c|}{A-B} & \multicolumn{1}{c|}{A-C} & \multicolumn{1}{c|}{A-B} & \multicolumn{1}{c|}{A-C} \\\cline{1-5}
\multicolumn{1}{|c|}{10} & \multicolumn{1}{c|}{•} & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{15} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{20} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{25} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{30} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{35} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{40} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{45} & • & • & • & • \\ 
\hline 
\multicolumn{1}{|c|}{50} & • & • & • & • \\ 
\hline 
\end{tabular} 
\end{table}

\end{document}

when I try and use only centered cells I get the issue that the cells are not equally sized. table centered

8
  • Welcome to TeX.SX!
    – moewe
    Mar 13 '19 at 9:26
  • Maybe tex.stackexchange.com/q/12703/35864 can help you. The accepted answer shows how to define fixed-width (like p{...}) centred (like c) columns.
    – moewe
    Mar 13 '19 at 9:28
  • Thank you for your comment. The point is that I would rather not hardcode the size of the cells and keep this variable.
    – BobLee
    Mar 13 '19 at 9:31
  • Currently you are hard-coding the size with p{.7\textwidth/5}, so I thought that is what you are after. If you want automatic sizing you may want to look into tabularx (tex.stackexchange.com/q/89166/35864) or tabulary.
    – moewe
    Mar 13 '19 at 9:33
  • 1
    The question I linked (tex.stackexchange.com/q/12703/35864) shows how to define fixed-width centred columns. You would have \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}} and would then use C{.7\textwidth/5} instead of p{.7\textwidth/5} or c.
    – moewe
    Mar 13 '19 at 9:46
2

Here a much shorter code:

\documentclass[10pt]{article}
\usepackage[english]{babel}
%\usepackage{a4wide}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{float}
\usepackage{makecell}
%\usepackage{slashbox}
\usepackage{diagbox}
\usepackage{multirow}
\usepackage{array}

\begin{document}
\begin{table}[H]
\centering\setlength{\extrarowheight}{2pt}
\begin{tabular} {|c|*{4}{>{\centering\arraybackslash}p{.7\textwidth/5}|}}
\hline
\multirow{2}{*}{\diagbox[height=\dimexpr2\line +6pt]{Distance \rlap{{[m]}}}{Module}} & \multicolumn{2}{c|}{Range Test [dBm]} & \multicolumn{2}{c|}{Throughput Test [Kbps]}\\[2pt]\cline{2-5}
   & A-B & A-C & A-B & A-C \\\cline{1-5}
10 & • & • & • & • \\
\hline
15
 & • & • & • & • \\
\hline
20 & • & • & • & • \\
\hline
25 & • & • & • & • \\
\hline
30 & • & • & • & • \\
\hline
35 & • & • & • & • \\
\hline
40 & • & • & • & • \\
\hline
 45 & • & • & • & • \\
\hline
50 & • & • & • & • \\
\hline
\end{tabular}
\end{table}

\end{document} 

enter image description here

1

Here is how you can construct that table with {NiceTabular} of nicematrix.

 \documentclass{article}
 \usepackage{nicematrix}
 \usepackage{float}

 \begin{document}
 \begin{table}[H]
 \centering\setlength{\extrarowheight}{2pt}
 \begin{NiceTabular}{w{c}{3cm}*{4}{w{c}{0.14\textwidth}}}[hvlines]
 \Block{2-1}{\diagbox{Distance [m]}{Module}} & \Block{1-2}{Range Test [dBm]} && \Block{1-2}{Throughput Test [Kbps]}\\
    & A-B & A-C & A-B & A-C \\
 10 & • & • & • & • \\
 15 & • & • & • & • \\
 20 & • & • & • & • \\
 25 & • & • & • & • \\
 30 & • & • & • & • \\
 35 & • & • & • & • \\
 40 & • & • & • & • \\
 45 & • & • & • & • \\
 50 & • & • & • & • \\
 \end{NiceTabular}
 \end{table}

 \end{document} 

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

0

text in your columns is not centered since you prescribe p{...} column type. as mentioned in comments to your question you should modify column type to:

>{\centerng\arraybackslash}p{<column width>}

However, I would not use slashbox for first cell in table and for table I would use tabularx table environment and equal width in all columns:

enter image description here

Also I would remove all unnecessary multicolumns:

\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{a4wide}
%\usepackage[utf8]{inputenc}
\usepackage{mathtools}
%\usepackage{float}
\usepackage{array, makecell, multirow, tabularx}
%\usepackage{slashbox}

\begin{document}
    \begin{table}[htb]
    \centering
    \renewcommand\arraystretch{1.2}
\begin{tabularx}{0.7\linewidth}{| c | *{4}{>{\centering\arraybackslash}X|}}
    \hline
    & \multicolumn{4}{c|}{Module}            \\
    \cline{2-5}
\multirow{3}{=}{\centering  Distance [m]}
    & \multicolumn{2}{c|}{Range Test [dBm]}
                                & \multicolumn{2}{c|}{Throughput Test [Kbps]}   \\
    \cline{2-5}
   &  A-B   &   A-C &   A-B & A-C        \\
   \cline{1-5}
10  &   •   &   •   &   •   & •         \\
    \hline
15  &   •   &   •   &   •   & •         \\
    \hline
20  &   •   &   •   &   •   & •         \\
    \hline
25  &   •   &   •   &   •   & •         \\
    \hline
30  &   •   &   •   &   •   & •         \\
    \hline
35  &   •   &   •   &   •   & •         \\
    \hline
40  &   •   &   •   &   •   & •         \\
    \hline
45  &   •   &   •   &   •   & •         \\
    \hline
50  &   •   &   •   &   •   & •         \\
    \hline
\end{tabularx}
    \end{table}
\end{document}
4
  • Thank you for your comment. When I copy your code and paste it in my working document I don't get the same results. I get the error " X Columns too narrow (table too wide)" it looks like the distance column is the size of the page and the others are slapped against it on the right.
    – BobLee
    Mar 13 '19 at 10:23
  • @DylanBeijersbergen: You can try with \multirow{3}{*}{\centering Distance [m]} & \multicolumn{4}{c|}{Module} \\ \cline{2-5} & \multicolumn{2}{c|}{Range Test [dBm]}...
    – leandriis
    Mar 13 '19 at 10:31
  • I get the error that the last cell on the right is slightly larger then the rest. i.stack.imgur.com/fpJvo.png
    – BobLee
    Mar 13 '19 at 10:38
  • @DylanBeijersbergen, to your preamble I only add the package tabularx. if your real document has different page layout, than it can happen, that table has different size. try to increase the table environment width from 0.7\linewidth to for example 0.75\linewidth to even to \linewidth.
    – Zarko
    Mar 13 '19 at 10:51

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.