2

I have the table below.

\begin{table}[]
\centering
\resizebox{\textwidth}{!}{
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|}
\hline
\textbf{Cache Configuration} &
  \textbf{Access Latency (ns)} &
  \textbf{Overhead} &
  \textbf{Dynamic Read Energy (nJ)} &
  \textbf{Overhead} &
  \textbf{Leakage Power (mW)} &
  \textbf{Overhead} &
  \textbf{Area (mm2)} &
  \textbf{Overhead} \\ \hline
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ \hline
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.11  &      & 18.7754 &      \\ \hline
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ \hline
Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \hline
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ \hline
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\ \hline
Precompression-enabled Cache (1MB) & 2.2873 & 74\% & 0.4178 & 26\% & 273.539  & 23\% & 3.9191  & 15\% \\ \hline
Precompression-enabled Cache (8MB) & 3.7251 & 35\% & 0.9489 & 10\% & 1692.853 & 3\%  & 19.2859 & 5\%  \\ \hline
\end{tabularx}
}
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}

Table Output

I have tried different techniques, but I can't seem to make it work. I want the table to fit in the page, along with automatic line breaks that don't break words with hyphens (maybe except for the Overhead columns).

6
  • 1
    Welcome to TeX.SE! Please extend you code fragment to complete small document which reproduce your table,. We havent any information about used document class nor page layout.
    – Zarko
    Commented Jan 29, 2020 at 18:56
  • Thanks for the reply Zarko. The document class is defined in a separate class file. What would be the best way to include that without having it take the entire page?
    – vic
    Commented Jan 29, 2020 at 19:11
  • @vic If you could just add the \documentclass[...]{...} line and all other lines from the preamble that affect the code you're having problems with (so for example here it would be \usepackage{tabularx}, and maybe some more if you loaded extra packages or changed commands that change how your table behaves), that would be optimal.
    – TivV
    Commented Jan 29, 2020 at 19:19
  • Although it's not an elegant solution, allowing "Overhead" to be hyphenated would remove the overfull lines in the table header. The first word in a paragraph is not hyphenated; this is a design feature of TeX. Hyphenation can be accomplished by preceding the first word by \hspace{0pt} as shown in this question: Table column text exceeds column width Commented Jan 29, 2020 at 19:20
  • @barbarabeeton Or using tabulary, or using Ragged... from ragged2e.
    – Sveinung
    Commented Jan 29, 2020 at 19:52

4 Answers 4

5

First off, do observe that the \resizebox{\textwidth}{...} "wrapper" serves no useful purpose since the width of the command's second argument is equal \textwidth. In fact, the way you set up the wrapper, it is actually slightly counterproductive. Why? Your code is a follows:

\resizebox{\textwidth}{!}{
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|}
...
\end{tabularx}
}

So what exactly is the problem, you may ask? It's the fact that you didn't terminate the line

\resizebox{\textwidth}{!}{

with a % (comment) symbol. As a result, the total width of the material governed by \resizebox is not just \textwidth but \textwidth plus 1 interword space. The correct way to write the instruction is like this:

\resizebox{\textwidth}{!}{%

However, as argued above, the \resizebox wrapper is superfluous to begin with and should be omitted.

Second, I suggest you continue to use a tabularx environment, but use a narrower width for the "Overhead" column than for the other columns. In the solution below, the four "overhead" columns are 40% narrower than the other four data columns. When setting up varying-width columns of type X, the cardinal rule is that the sum of the relative widths -- 4*1.25+4*0.75 -- must equal the number of columns of type X -- here: 8.

Third, I would try hard to give a the a much more open "look", mainly by omitting all vertical rules and using fewer, but well-spaced, horizontal rules.

Lastly, I'd left-align the first column and center-set all 8 data columns.


Here's the proposed modification and the associated screenshot:

enter image description here

\documentclass{article}
\usepackage[letterpaper,hmargin=1in]{geometry} % set page parameters suitably
\usepackage{tabularx,ragged2e,booktabs}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
% set up a modified, i.e., centered version of the "X" column type:
\newcolumntype{C}[1]{>{\Centering\arraybackslash%
     \hsize=#1\hsize\linewidth=\hsize\hspace{0pt}}X}

% determine width and column type of first column:
\newlength\mylen
\settowidth\mylen{enabled Cache (8MB)} 
\newcolumntype{M}{>{\RaggedRight}m{\mylen}}

\begin{document}
\begin{table}
\setlength\tabcolsep{3pt} % default: 6pt
\begin{tabularx}{\textwidth}{@{} M *{4}{C{1.25}C{0.75}} @{}}
\toprule
  \textbf{Cache Configuration} &
  \textbf{Access Latency} (ns) &
  \textbf{Overhead} &
  \textbf{Dynamic Read Energy} (nJ) &
  \textbf{Overhead} &
  \textbf{Leakage Power} (mW) &
  \textbf{Overhead} &
  \textbf{Area} (mm\textsuperscript{2}) &
  \textbf{Overhead} \\ 
\midrule
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ \addlinespace
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.11  &      & 18.7754 &      \\ \addlinespace
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ \addlinespace
Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \addlinespace
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ \addlinespace
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\ \addlinespace
Precompression-enabled Cache (1MB) & 2.2873 & 74\% & 0.4178 & 26\% & 273.539  & 23\% & 3.9191  & 15\% \\ \addlinespace
Precompression-enabled Cache (8MB) & 3.7251 & 35\% & 0.9489 & 10\% & 1692.853 & 3\%  & 19.2859 & 5\%  \\ 
\bottomrule
\end{tabularx}
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}
\end{document}
4

Based on guessing about your document layout. One way to avoid overlapping is use rotated column headers:

\documentclass[a4paper,fleqn,10pt,oneside]{book}
\usepackage{geometry}

\usepackage{siunitx}
\usepackage{rotating}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\small\footnotesize}
\renewcommand\theadgape{}

\begin{document}
\begin{table}[htb]
\centering
\small
\setlength\tabcolsep{4pt}
\setcellgapes{3pt}
\makegapedcells
\settowidth\rotheadsize{\theadfont Dynamic Read} 
\begin{tabularx}{\textwidth}{|>{\raggedright}X|S[table-format=1.4]|c|
                                S[table-format=1.4]|c|
                                S[table-format=4.4]|c|
                                S[table-format=2.4]|c|}
\hline
\thead{Cache\\ Configuration} &
  \rothead{Access Latency (ns)} &
  \rothead{Overhead} &
  \rothead{Dynamic Read Energy (nJ)} &
  \rothead{Overhead} &
  \rothead{Leakage Power (mW)} &
  \rothead{Overhead} &
  \rothead{Area (mm2)} &
  \rothead{Overhead} \\ \hline
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ \hline
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.11  &      & 18.7754 &      \\ \hline
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ \hline
Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \hline
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ \hline
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\ \hline
Precompression-enabled Cache (1MB) & 2.2873 & 74\% & 0.4178 & 26\% & 273.539  & 23\% & 3.9191  & 15\% \\ \hline
Precompression-enabled Cache (8MB) & 3.7251 & 35\% & 0.9489 & 10\% & 1692.853 & 3\%  & 19.2859 & 5\%  \\ \hline
\end{tabularx}

\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}
\end{document}

enter image description here

or when the \textwidth is smaller:

\documentclass[a4paper,fleqn,10pt,oneside]{book}
\usepackage{siunitx}
\usepackage{rotating}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\small\footnotesize}
\renewcommand\theadgape{}

\begin{document}
\begin{table}[htb]
\centering
\small
\setlength\tabcolsep{3pt}
\setcellgapes{3pt}
\makegapedcells
\settowidth\rotheadsize{\theadfont Dynamic Read} 
\begin{tabularx}{\textwidth}{|>{\raggedright}X|
                                S[table-format=1.4]|c|
                                S[table-format=1.4]|c|
                                S[table-format=4.4]|c|
                                S[table-format=2.4]|c|}
\hline
\thead{Cache\\ Configuration} &
  \rothead{Access Latency (ns)} &
  \rothead{Overhead} &
  \rothead{Dynamic Read Energy (nJ)} &
  \rothead{Overhead} &
  \rothead{Leakage Power (mW)} &
  \rothead{Overhead} &
  \rothead{Area (mm2)} &
  \rothead{Overhead} \\ \hline
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ \hline
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.11  &      & 18.7754 &      \\ \hline
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ \hline
Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \hline
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ \hline
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\ \hline
Precompression-enabled Cache (1MB) & 2.2873 & 74\% & 0.4178 & 26\% & 273.539  & 23\% & 3.9191  & 15\% \\ \hline
Precompression-enabled Cache (8MB) & 3.7251 & 35\% & 0.9489 & 10\% & 1692.853 & 3\%  & 19.2859 & 5\%  \\ \hline
\end{tabularx}

\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}
\end{document}

enter image description here

3
  • Thanks for your solution Zarko. What would be the way to have all the values in the Leakage Power and Area columns be right aligned?
    – vic
    Commented Jan 29, 2020 at 19:47
  • Column 8 is too narrow. You should also move the per cent sign to the headings in column 3, column 5, column 7 and column 9.
    – Sveinung
    Commented Jan 29, 2020 at 20:42
  • @Sveinung, thank you very much for pointing me to error in last S column definitions. Regarding %, I left them as OP writes them in his code fragment, however I agrree with you.
    – Zarko
    Commented Jan 29, 2020 at 21:16
4

Here is a possible solution using the package tabulary instead of tabularx. I have also loaded babel to have automatic hyphenation of ‘Overhead’. Also, I loaded geometry, because I assumed that you have more sensible margins in your real document. The font size is reduced to \footnotesize, and then you have a readable tabular that fits inside sensible margins.

Of cause, I suggest setting the tabular using booktabs and no vertical rules, see examples 1 and 3 below. The per cent sign (%) should be moved from the column to the heading.

The (updatred) example 1 uses fixed width x-columns in combination with tabular*. Headings are set in a nested tabular and line breakes are set manually using \\. You may use \parbox instead of the tabulars. Font size for headings are reduces two grades, to \footnotesize, rest of the tabular's text and figures are in normal font size (10 pt). I have aligned the heading at the bottom, because that is according to the recommendation in my part of the word.

If you have the right to restructure the tabular, I suggest that you move the four overhead columns to two new rows with a description what the overheads refer to (comparing conventional caches and pre-compression enabled caches?)

Example 1 - tabular*

enter image description here

\documentclass[UKenglish]{article}
\usepackage{array, tabulary, booktabs, lmodern}
\usepackage{babel, caption}
\usepackage{geometry}



% Multi-line left-aligned text with manual line breaks.
% The base line of the whole is at the top row.
\newcommand*{\tstack}[1]{%
  \begingroup
    \renewcommand*{\arraystretch}{0.85}%
    \footnotesize\bfseries\begin{tabular}[b]{c}#1\end{tabular}%
  \endgroup
}

\setlength{\tabcolsep}{0pt}
\setlength{\extrarowheight}{2pt}
\begin{document}

\begin{table}[]
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}

\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
                                                l
                                                wr{1.2cm}
                                                wr{0.9cm}
                                                wr{1.45cm}
                                                wr{0.9cm}
                                                wr{1.55cm}
                                                wr{0.95cm}
                                                wr{1.4cm}
                                                wr{0.9cm}
                                                }
\toprule
\tstack{Cache Configuration} &
\tstack{Access\\ Latency\\ \scriptsize (ns)} &
\tstack{Over-\\head \\\scriptsize(\%)} &
\tstack{Dynamic\\Read\\ Energy\\ \scriptsize(nJ)} &
\tstack{Over-\\head\\\scriptsize(\%)} &
\tstack{Leakage \\Power\\ \scriptsize(mW)} &
\tstack{Over-\\head\\\scriptsize(\%)} &
\tstack{Area\\\scriptsize(mm\textsuperscript{2})} &
\tstack{Over-\\head\\\scriptsize(\%)} 
\\ \midrule
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ 
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.110  &      & 18.7754 &      \\
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ 
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ 
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\
Precompression-enabled Cache (1MB) & 2.2873 & 74  & 0.4178 & 26  & 273.539  & 23  & 3.9191  & 15  \\ 
Precompression-enabled Cache (8MB) & 3.7251 & 35  & 0.9489 & 10  & 1692.853 & 3   & 19.2859 & 5  \\ 
\bottomrule
\end{tabular*}
\end{table}

\end{document}

Example 2

enter image description here

\documentclass[UKenglish]{article}
\usepackage{array, tabulary, booktabs}
\usepackage{babel}
\usepackage{geometry}


\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\begin{document}

\begin{tabulary}{\textwidth}{|L|R|R|R|R|R|R|R|R|}
\hline
\textbf{Cache Configuration} &
  \textbf{Access Latency (ns)} &
  \textbf{Overhead (\%)} &
  \textbf{Dynamic Read Energy (nJ)} &
  \textbf{Overhead (\%)} &
  \textbf{Leakage Power (mW)} &
  \textbf{Overhead (\%)} &
  \textbf{Area (mm2)} &
  \textbf{Overhead} \\ \hline
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ \hline
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.11  &      & 18.7754 &      \\ \hline
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ \hline
Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \hline
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ \hline
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\ \hline
Precompression-enabled Cache (1MB) & 2.2873 & 74  & 0.4178 & 26  & 273.539  & 23  & 3.9191  & 15  \\ \hline
Precompression-enabled Cache (8MB) & 3.7251 & 35  & 0.9489 & 10  & 1692.853 & 3   & 19.2859 & 5  \\ \hline
\end{tabulary}
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}

\end{document}

Example 3 - booktabs and tabulary

enter image description here

\documentclass[UKenglish]{article}
\usepackage{array, tabulary, booktabs}
\usepackage{babel}
\usepackage{geometry}


\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\begin{document}

\begin{table}[]
\centering\footnotesize

\begin{tabulary}{\textwidth}{@{}L*{8}{R}@{}}
\toprule
\textbf{Cache Configuration} &
  \centering\textbf{Access Latency (ns)} &
  \textbf{Overhead (\%)} &
  \centering\textbf{Dynamic Read Energy (nJ)} &
  \textbf{Overhead(\%)} &
  \centering\textbf{Leakage Power (mW)} &
  \textbf{Overhead (\%)} &
  \centering\textbf{Area (mm2)} &
  \textbf{Overhead (\%)} 
\\ \midrule
Conventional Cache (1MB)           & 1.3125 &      & 0.3295 &      & 221.796  &      & 3.4085  &      \\ 
Conventional Cache (8MB)           & 2.7504 &      & 0.8605 &      & 1641.110  &      & 18.7754 &      \\
Frequency Table (FT)               & 0.4129 &      & 0.0008 &      & 2.4785   &      & 0.0296  &      \\ Precompression Table (PT)          & 0.6353 &      & 0.0014 &      & 4.9366   &      & 0.0788  &      \\ \midrule
Mapping Table (MT)                 & 0.0927 &      & 0.0011 &      & 0.3396   &      & 0.0032  &      \\ 
Overflow Mapping Table             & 0.3394 &      & 0.0020 &      & 4.5186   &      & 0.038   &      \\
Precompression-enabled Cache (1MB) & 2.2873 & 74  & 0.4178 & 26  & 273.539  & 23  & 3.9191  & 15  \\ 
Precompression-enabled Cache (8MB) & 3.7251 & 35  & 0.9489 & 10  & 1692.853 & 3   & 19.2859 & 5  \\ 
\bottomrule
\end{tabulary}
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}

\end{document}
0

I propose this variant, using a basic tabular and the possibilities of makecell:

\documentclass[a4paper, fleqn, 10pt, oneside]{book}
\usepackage{geometry}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{etoolbox}
\usepackage{makecell}
\renewcommand\theadfont{\small\footnotesize}
\renewcommand\theadgape{}
\newrobustcmd{\myrothead}[1]{\rotatebox[origin=l]{30}{\rlap{#1}}\hfill\null}

\begin{document}

\begin{table}[!htb]
\centering
\small
\renewcommand{\theadfont}{\small}
\renewcommand{\cellset}{\renewcommand{\arraystretch}{0.6}}
\setcellgapes{3pt}
\makegapedcells
\settowidth\rotheadsize{\theadfont Dynamic Read}
\sisetup{table-text-alignment=left}
\begin{tabular}{!{\vline width 1pt} l|S[table-format=1.4, table-text-alignment=left]|c|
                                S[table-format=1.4]|c|
                                S[table-format=4.4]|c|
                                S[table-format=2.4]|c!{\vline width 1pt}}
\noalign{\vskip4ex}
\multicolumn{1}{l}{\raisebox{-1.5ex}{\thead[bl]{Cache\\ Configuration}}} &
  \multicolumn{1}{l}{\myrothead{\makecell[tl]{Access Latency\\[-1ex] (ns)}}}&
  \multicolumn{1}{l}{\myrothead{\raisebox{-1.5ex}{Overhead}}} &
  \multicolumn{1}{l}{\myrothead{\makecell[tl]{Dynamic Read\\[-1ex] Energy (nJ)}}} &
  \multicolumn{1}{l}{\myrothead{\raisebox{-1.5ex}{Overhead}}} &
  \multicolumn{1}{l}{\myrothead{\makecell[tl]{Leakage Power\\[-1ex] (mW)}}} &
 \multicolumn{1}{l}{\myrothead{\raisebox{-1.5ex}{Overhead}}} &
  \multicolumn{1}{l}{\myrothead{\raisebox{-1.5ex}{Area (mm2)}}} &
  \multicolumn{1}{l}{\myrothead{\raisebox{-1.5ex}{Overhead}}} \\ \Xhline{1pt}
\makecell[l]{Conventional \\Cache (1MB)} & 1.3125 & & 0.3295 & & 221.796 & & 3.4085 & \\ \hline
\makecell[l]{Conventional\\ Cache (8MB)} & 2.7504 & & 0.8605 & & 1641.11 & & 18.7754 & \\ \hline
\makecell[l]{Frequency Table\\ (FT)} & 0.4129 & & 0.0008 & & 2.4785 & & 0.0296 & \\ \hline
\makecell[l]{Precompression Table\\ (PT)} & 0.6353 & & 0.0014 & & 4.9366 & & 0.0788 & \\ \hline
\makecell[l]{Mapping Table\\ (MT)} & 0.0927 & & 0.0011 & & 0.3396 & & 0.0032 & \\ \hline
\makecell[l]{Overflow\\ Mapping Table} & 0.3394 & & 0.0020 & & 4.5186 & & 0.038 & \\ \hline
\makecell[l]{Precompression-enabled\\ Cache (1MB)} & 2.2873 & 74\% & 0.4178 & 26\% & 273.539 & 23\% & 3.9191 & 15\% \\ \hline
\makecell[l]{Precompression-enabled\\ Cache (8MB)} & 3.7251 & 35\% & 0.9489 & 10\% & 1692.853 & 3\% & 19.2859 & 5\% \\ \Xhline{1pt}
\end{tabular}
%
\caption{Access Latency, Energy, Power and Area Overheads of Precompression logic and Precompression-enabled caches.}
\label{table:precompression-overhead}
\end{table}

\end{document} 

enter image description here

You must log in to answer this question.

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