pgfplotstable - 有没有办法使用 \textwidth 并居中文本?

pgfplotstable - 有没有办法使用 \textwidth 并居中文本?

我正在使用 pgfplotsable,但似乎找不到一种简单的方法来同时实现 a) 为我的列设置特定的内容和 b) 将文本置于列内中心。无论我如何尝试,似乎一个都会覆盖另一个。

这是我一直在尝试的一个例子

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

输出看起来像这样(忽略被 6 整除的规则的重复输入 - 我只是在玩 \makecell 和 \Gape 命令!)。

在此处输入图片描述

我所想要做的就是使用全局命令将文本置于每列的中心,而不是在每个单元格中添加 \centering。

我也尝试过

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

并删除了“column type={p{.3\textwidth} }”命令。虽然这完成了将所有内容居中的任务,但它会使整个表格缩小,文本变得太小。

任何帮助,将不胜感激。

答案1

像这样?

在此处输入图片描述

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

编辑:

  • 为列内容的水平居中定义了新的列类型C(您可以选择另一个字母作为其名称)。它是从p{...}添加的选项派生而来的\centering。由于它会干扰\\行尾,\arraybackslash因此添加了恢复的原始含义\\
  • 在计算列宽时使用内核\dimexpr中的定义latex。要使表格宽度等于\textwidth您应该:
    • 确保表格宽度份额的总和在列之间排列,使得它们的总和等于 1(0.2 + 0.5 + 0.3 = 1)
    • 在计算列宽时必须考虑\tabcolsep。表格跨度为六个这样的长度(每列有两个,一个在开始列,一个在末尾),但其中两个(第一个和最后一个)被@{}列规范的开始和末尾占用。因此,列之间共享的宽度是\textwidth - 4\tabcolsep

附录

到目前为止,我还没有关心表格内容。现在我检查它......所以下面是一些如何改进它的建议:

  • 而不是p{...}列类型使用m{..}类型(由包提供array)。
  • 由于m{...}类型与包不兼容makecell,因此应将其删除
  • 而不是\hline使用\midrule
  • 使用环境amsmathaligned

通过这些更改,您的表格将变成:

在此处输入图片描述

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

相关内容