3

I have this simple table and I need some help please:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{table}[]
\resizebox{\textwidth}{!}{%
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
   & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1 \\ \hline
Value  & 1       & 2       & 3       & 4       & 5       & 6       \\       \hline
\multicolumn{7}{|c|}{Total}                                        \\     \hline
\multicolumn{3}{|c|}{Val1} & \multicolumn{4}{c|}{Val2}             \\    \hline
\end{tabular}%
}
\end{table}
\end{document}

And looks like this : enter image description here

How can I make the columns in the row with val1 and val2 equal width?

Thank you.

1
  • Please don't post code fragments. I've made your fragment into a compilable document. If that doesn't match your document please edit the question accordingly. As an aside, it's really bad practice to use \resizebox to change the size of the table. To get a table the width of the line use tabularx. If the table is too large, figure out ways to split it, display it sideways, or reduce the width of columns.
    – Alan Munn
    Commented Aug 29, 2018 at 14:58

2 Answers 2

4

Without any additional package and (almost) no change to your existing table (I only reduced your code to the tabular and changed the relevant line):

\documentclass[border=2mm]{standalone}

\begin{document}
\begin{tabular}{|c|c|c|c|c|c|c|}
  \hline
         & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1  \\
  \hline
  Value  & 1       & 2       & 3       & 4       & 5       & 6        \\
  \hline
  \multicolumn{7}{|c|}{Total}                                         \\
  \hline
  \multicolumn{7}{|c|}{\hfill Val1\hfill\vline\hfill Val2\hfill\null} \\
  \hline
\end{tabular}
\end{document}

enter image description here

3
  • +1 But to the extent that this doesn't capture what the \resizebox code was doing in the OPs question, it may not be what they want.
    – Alan Munn
    Commented Aug 29, 2018 at 19:29
  • @AlanMunn but since you added an answer which deals with the macro-which-shall-not-be-used-on-tables's task I felt that I'd have to do it without tabularx just for the sake of it. And I didn't feel like doing the calculation of the available width for equal-spaced cells myself :)
    – Skillmon
    Commented Aug 29, 2018 at 19:39
  • Thank you for your answer. I fix my problem using your example. :)
    – Adrian
    Commented Aug 30, 2018 at 6:40
4

Here's one way to do this: use the tabularx to make the table have equally spaced columns for the whole width of the table, and then embed a second table inside for the evenly spaced columns at the bottom.

I've used the array package to define a centred X column.

\documentclass{article}
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}

\begin{tabularx}{\linewidth}{|C|C|C|C|C|C|C|}
\hline
   & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1 & Zone A1 \\ \hline
Value  & 1       & 2       & 3       & 4       & 5       & 6       \\       \hline
\multicolumn{7}{|c|}{Total}                                        \\     \hline
\multicolumn{7}{|@{}c@{}|}{\begin{tabularx}{\columnwidth}{C|C}Val1 & Val2 \end{tabularx}}       \\    \hline
\end{tabularx}%

\end{document}

output of code

As I mentioned in the comments, it's generally bad practice to use \resizebox to change a table size, since it changes the font size of the document. So using tabularx allows you to specify the width of the table directly. Vertical lines in tables are also usually not necessary, so you might also want to look at using the booktabs package instead and not using any vertical lines. Of course they are sometimes appropriate, so I have left your example as is in my answer.

output of code

5
  • No need to load array yourself, it's required by tabularx anyway.
    – Skillmon
    Commented Aug 29, 2018 at 19:14
  • 2
    @Skillmon I prefer to load package dependencies explicitly in answers like this since the documentation for array is separate from that of tabularx.
    – Alan Munn
    Commented Aug 29, 2018 at 19:27
  • Very good point.
    – Skillmon
    Commented Aug 29, 2018 at 19:35
  • Out of curiosity I tried to run your code, but it failed (no error, but misalignments). For me, the line which shall be split should be \multicolumn{7}{|@{}C@{}|}{\begin{tabularx}{\columnwidth}{C|C}Val1 & Val2 \end{tabularx}} \\\hline (the misalignment is noticeable in your uploaded picture).
    – Skillmon
    Commented Aug 30, 2018 at 7:22
  • @Skillmon You're absolutely right. Thanks. I've fixed the code.
    – Alan Munn
    Commented Aug 30, 2018 at 11:13

You must log in to answer this question.

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