[Tex/LaTex] pgfplotstable – Is there a way to use \textwidth and centering for text

horizontal alignmentpgfplotstable

I am using pgfplotsable and I can't seem to figure out an easy way to both a) set a specific with for my columns and b) center the text within the columns. No matter what I try, one seems to override the other.

Here is an example of what I have been trying

\documentclass[12pt]{book}

\usepackage{array, booktabs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\renewcommand{\arraystretch}{1.5}

\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usepackage{pgfplotstable}

\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\usepackage{makecell}
% Using this in conjunction with the pgfplotstable

\begin{document}

\begin{table}[htpb]
    \centering
    \pgfplotstabletypeset[
    col sep=&,
row sep=\\,
string type,
columns = {Divisor, Rule, Example},
    columns/Divisor/.style={column name={ \bf Divisor},column type={p{.1\textwidth} } } ,
    columns/Rule/.style={column name={ \bf Rule},column type={p{.4\textwidth} } } ,
    columns/Example/.style={column name={ \bf Example},column type={p{.3\textwidth} } } ,
    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}

The output looks like this (ignore the repeated entry for the rules for divisibility by 6 — I was just playing around with the \makecell and \Gape commands!).

enter image description here

All I want to be able to do is center the text within each column using a global command, rather than adding \centering to each cell.

I have also tried

 \begin{table}[htpb]
    \centering
    \resizebox{\textwidth}{!}{
    \pgfplotstabletypeset[

and removed the "column type={p{.3\textwidth} }" commands. While that accomplishes the task of centering everything, it shriks the entire table down and text becomes too small.

Any help would be appreciated.

Best Answer

like this?

enter image description here

\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:

enter image description here

\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}
Related Question