like this?
\documentclass[12pt]{book}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usepackage{pgfplotstable}
\usepackage{booktabs, makecell}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} % <--- for centering
%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{table}%[htpb]
\centering
\setlength\extrarowheight{2pt}
\pgfplotstabletypeset[
col sep=&,
row sep=\\,
string type,
columns = {Divisor, Rule, Example},
columns/Divisor/.style={column name={\textbf{Divisor}},
column type={@{}C{.2\dimexpr\textwidth-4\tabcolsep}}} , % <--- correct calculation of column width
columns/Rule/.style={column name={\textbf{Rule}},
column type={C{.5\dimexpr\textwidth-4\tabcolsep} }} , % <---
columns/Example/.style={column name={\textbf{Example}},
column type={C{.3\dimexpr\textwidth-4\tabcolsep}@{} }} , % <---
every head row/.style=
{before row=\toprule\toprule, after row=\bottomrule},
every last row/.style=
{after row=\bottomrule\bottomrule},
]
{
Divisor & Rule & Example \\
2 & If the last digit is 0, 2, 4, 6 or 8 & $C'mon\ Man!$ \\ \hline
3 & If sum of digits is divisible by 3 & $ 921 \rightarrow \frac{9 + 2 + 1}{3} = 4 $ \\ \hline
4 & If number formed by last two digits is divisible by 4 & $ 524 \rightarrow\ \frac{24}{4} = 6 $ \\ \hline
5 & If last digit is 0 or 5 & $C'mon\ Man!$ \\ \hline
6 & If it is divisible by 2 AND divisible by 3 & $\frac{468}{2} = 234,\ \frac{468}{3} = 156$ \\ \hline
6 & If it is divisible by 2 AND divisible by 3 & \Gape[6pt]{\makecell{$\frac{468}{2} = 234$ \\ {} \\ $\frac{468}{3} = 156$}} \\ \hline
7 & No rule exists & {} \\ \hline
8 & If number formed by last three digits is divisible by 8 & $ 24,296 \rightarrow \frac{296}{8} = 37 $ \\ \hline
9 & If sum of digits is divisible by 9 & $549 \rightarrow \frac{5+4+9}{9} = 2$ \\
}
\caption{Divisibility rules for the integers less than 10.}
\end{table}
\end{document}
edit:
- for horizontal centering of column contents is defined new column type
C
(you can choose another letter for its name). it is derived from p{...}
with added option \centering
. since it interfere with \\
on the end of rows, is added \arraybackslash
which restore original meaning of \\
.
- in calculation of column width is used
\dimexpr
defined in latex
kernel. to have table width equal to \textwidth
you should:
- ensure that the sum of the shares of the table width of are arranged between the columns so that their sum is equal to 1 (0.2 + 0.5 + 0.3 = 1)
- in calculation of column width had to be consider
\tabcolsep
. table span six this lengths (each column has two, one on begin column, one on its end), but two of them (the first and the last one) are eaten by @{}
on beginning and end of column specification. therefore the width, which is shared between columns is \textwidth - 4\tabcolsep
addendum:
so far i didn't bother with table content. now i inspect it ... so below are some suggestion how to improve it:
- instead o
p{...}
column type use m{..}
type (provided by package array
).
- since
m{...}
type is not compatible with makecell
package, it should be removed
- instead of
\hline
use \midrule
- use
amsmath
package for aligned
environment
with this changes your table become:
\documentclass[12pt]{book}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{amsmath}
%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{table}%[htpb]
\centering
\pgfplotstabletypeset[
col sep=&,
row sep=\\,
string type,
columns = {Divisor, Rule, Example},
columns/Divisor/.style={column name={\textbf{Divisor}},
column type={@{}C{.15\dimexpr\textwidth-4\tabcolsep}}} ,
columns/Rule/.style={column name={\textbf{Rule}},
column type={C{.5\dimexpr\textwidth-4\tabcolsep} }} ,
columns/Example/.style={column name={\textbf{Example}},
column type={>{$\displaystyle}%
C{.35\dimexpr\textwidth-4\tabcolsep}<{$}
@{} }} ,
every head row/.style=
{before row=\toprule, after row=\midrule},
every last row/.style=
{after row=\bottomrule},
]
{
Divisor & Rule & Example \\
2 & If the last digit is 0, 2, 4, 6 or 8
& \textit{C'mon Man!} \\
\midrule
3 & If sum of digits is divisible by 3
& 921 \rightarrow \frac{9 + 2 + 1}{3} = 4 \\
\midrule
4 & If number formed by last two digits is divisible by 4
& 524 \rightarrow\ \frac{24}{4} = 6 \\
\midrule
5 & If last digit is 0 or 5
& \textit{C'mon Man!} \\
\midrule
6 & If it is divisible by 2 AND divisible by 3
& \frac{468}{2} = 234,\ \frac{468}{3} = 156 \\
\midrule
6 & If it is divisible by 3 AND divisible by 2
& {\begin{aligned}\frac{468}{3}&=156\\
\frac{468}{2}&=234
\end{aligned}} \\
\midrule
7 & No rule exists
& \\
\midrule
8 & If number formed by last three digits is divisible by 8
& 24,296 \rightarrow \frac{296}{8} = 37 \\
\midrule
9 & If sum of digits is divisible by 9
& 549 \rightarrow \frac{5+4+9}{9} = 2 \\
}
\caption{Divisibility rules for the integers less than 10.}
\end{table}
\end{document}
Best Answer
This should do the job:
The most convenient way how to center an equation is to use the
equation
more, which is done by\[..\]
. However, you can't bring that to the list of floats, because that causes errors, well, you don't want the equation on display in the list of floats, do you? So the short caption is the solution.