4

I am trying make the width of the columns below Very very long name the same (G1 and G2). The one on the top is created by using tabular and the other one is created with tabularx. enter image description here I have seen similar questions, but could not apply their solutions to my case.

\documentclass{article}

\usepackage{graphicx}
\usepackage{multirow}
\usepackage{tabularx}


\begin{document}

\begin{table}[ht]
    \centering
    \resizebox{\textwidth}{!}{
    \renewcommand{\arraystretch}{1.5} % Default value: 1
    \begin{tabular}{lcccccc}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Algorithm}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Graph}} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} \\ \hline
    \end{tabular}}
\end{table}


\begin{tabularx}{\textwidth}{XXXXXXX}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Algorithm}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Graph}} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} \\ \hline
\end{tabularx}

\end{document}
1

3 Answers 3

5

If you load geometry, you'll have more sensible margins. The default is designed for marginal notes, so if you don't use them, you'll have more space on the line.

Note you shouldn't use \resizebox with tables, as it leads to inconsistent font sizes. Finally I don't see what all those \multicolumn{1}{c|} are in the code for, since you can centre the content of an X cell?

Here is a code with X cells:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
\usepackage{ragged2e}
\usepackage{tabularx}

\begin{document}

\begin{table}[!ht]
    \centering
    \renewcommand{\arraystretch}{1.5} % 
    \setlength{\tabcolsep}{4pt}
\begin{tabularx}{\textwidth}{|>{\hsize=1.72\hsize\bfseries\RaggedRight}X|*{6}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
            \textbf{Algorithm}& \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\
            \hline%
            \textbf{Graph} & $G₁$ & $G₂$ & $G₁$ & $G₂$ & $G₁$ & $G₂$ \\ 
            \hline%
            \textbf{Weighted Ortho\-gonal Ordering} & $G₁$ & $G₂$ & $G₁$ & $G₂$ & $G₁$ & $G₂$ \\ \hline
\end{tabularx}
\end{table}

\end{document}

enter image description here

4
  • thanks for your answer. When one of the entries in a cell is too long this one still does not work (replace graph with "Weighted Orthogonal Ordering"). Resizing - inconsistent font size is not a problem for me, I just want the cells (G1 and G2) have the same size. I created the initial version of the table with an online table generator, those redundant \multicolumn comes from that tool. Jun 26, 2018 at 15:00
  • @user3140972: I've posted another code, entirely based on the X columtype, using its capability to have different widths with \hsize=xxx\hsize.
    – Bernard
    Jun 26, 2018 at 15:26
  • thanks @Bernard I was able to get the desired result by setting the widths manually and decreasing the font size slightly. Jun 26, 2018 at 15:53
  • Playing with the font size and the value of \tabcolsep are indeed two possibilities.
    – Bernard
    Jun 26, 2018 at 16:02
4

which table is more easy to read? the first? the second?

enter image description here

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcxit[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}C|}{\textit{#1}}}

\begin{document}

\begin{table}[ht]
    \centering
    \tiny
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{table}

\begin{table}[ht]
    \centering
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{table}

\end{document}

in both cases, as you can see from mwe, are used your page layout (default for article). however, if you increase \textwidth with use of the package geometry, as suggested Bernard in his answer:

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tabularx}
% follows the same code as in mwe above

the table looks far better:

enter image description here

from above table images follows, that the second one is reasonable compromise.

edit: one possible solution is to allow to table that it can spill-out from right text border. for example with use of the package changepage:

\documentclass{article}
\usepackage{changepage}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcxit[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}C|}{\textit{#1}}}

%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.25pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}

\begin{table}[ht]
\begin{adjustwidth*}{0pt}{-\dimexpr\marginparsep+\marginparwidth}
    \small
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{adjustwidth*}
\end{table}
\end{document}

which still result with column headers text in two lines:

enter image description here

(red lines indicate page layout).

4

A possible solution based on tabularx could be the following:

\documentclass{article}

\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{Y}{>{\Centering\arraybackslash}X}
\newcolumntype{Z}[1]{>{\Centering\arraybackslash\hsize=#1\hsize}X}

\begin{document}

\begin{tabularx}{\textwidth}{|l|Y|Y|Y|Y|Y|Y|}
  \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ 
  \hline
  \textbf{Algorithm} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} \\ 
  \hline
  \textbf{Graph} & $G_1$  & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}

\end{document}

enter image description here

In the above example, I have removed redundant \multicolumn commands. In order to horizontally center the contents of X columns, I have defined the new column type Y. For the multiolumns, I have replaced the c type columns by additionally defined Z type columns, that are based on X columns and therefor allow for automated linebreaks to make the table fit into the textwidth.

6
  • thanks. The linebreaks look ugly, especially when some lines are broken and other are not... Jun 26, 2018 at 15:03
  • 1
    @user3140972, just increase text width of your page. than text in column headers will not be in two lines anymore.
    – Zarko
    Jun 26, 2018 at 15:07
  • @Zarko I can't really play with the geometry, this is a thesis template. Jun 26, 2018 at 15:21
  • @user3140972, see my answer below. however, you should give us more accurate information about your page layout.
    – Zarko
    Jun 26, 2018 at 15:29
  • Hint: Load the ragged2e package and replace all \centering instructions with \Centering; the line lengths will be much more evenly distributed. Also, consider 2.3 instead of 2 as the argument of the Z columns. (Why? The usable width of a Z column is 2\hsize+2\tabcolsep+\arrayrulewidth -- or ca 2.3\hsize...)
    – Mico
    Jun 26, 2018 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .