8

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.

  • 2
    Please make your code compilable by putting it into complete document. – Alan Munn Nov 6 '17 at 18:51
  • 2
    Still not compilable. You need to add the definition of \Gape and \makecell and whatever package they require. – Alan Munn Nov 6 '17 at 19:01
  • Done -- added. In case there are still issues with the Gape command, it can be commented out. – G. Khanna Nov 6 '17 at 19:10
  • 1
    Still not compilable. – Alan Munn Nov 6 '17 at 19:21
  • My sincere apologies. I clearly have not used this forum to post questions very much and I need to test my LaTeX code before posting. I did that with these latest edits. – G. Khanna Nov 6 '17 at 19:30
10

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}
  • 1
    Thank you @Zarko -- that worked. Could I trouble you to explain the details of: 1) \newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} . and 2) column type={@{}C{.2\dimexpr\textwidth-4\tabcolsep}}} ? Although I can cut-and-paste this to use in the future (I keep a lot of my table formatting similar), I'm just trying to learn some of the nuances of LaTeX and pgfplotstable. Really appreciate it! – G. Khanna Nov 6 '17 at 20:12
  • @G.Khanna, see edit of answer – Zarko Nov 6 '17 at 20:52
  • @G.Khanna, see addendum to my answer. and, if you liked answer, you can accept it :-). – Zarko Nov 6 '17 at 23:08
  • thanks. There are a couple of comments/questions from your addendum: 1) I would like the flexibility to use math mode in any cell of the table. Do you still recommend using the "m" column type? 2) I like the \midrule idea 3) I like your suggestion for using the \amsmath package and the aligned instead of the \makecell, The latter came from another StackExchange thread. – G. Khanna Nov 7 '17 at 1:55
  • @G.Khanna, (i) you can use math in any cell as you do do so far. solution in addendum enable shorter code and display style. it is handy when all cells in column contain only math expressions. (ii) :-), (iii) amsmath and its supersede mathtools are powerful packages for writing math. see en.wikibooks.org/wiki/LaTeX/Advanced_Mathematics – Zarko Nov 7 '17 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.