将表格置于页面中央

将表格置于页面中央

将表格置于页面中央

我有一些使用外部文本文件生成的表格。有很多不同的表格,但它们都有一些相同的要求。

  • 它们需要延伸到页边距。
  • 表格需要多个标题行。此示例显示一个标题行,该行占据全部 14 列。该行需要为彩色背景上的白色文本。
  • 各行需要交替颜色。
  • 有一行或多行需要根据需要拉伸/加宽。

我快完成了,但是还存在一些问题:

  • 表格不尊重
  • 标题行未延伸至表格的整个长度。渲染时,标题行的两侧略短。
  • 为什么第一行以 0 开头?
  • 当 emacs 重新缩进文件时,我得到了下面的内容。这让我认为我缺少了一些语法。

这是上述内容的源代码,以及我在渲染时看到的屏幕截图。

\documentclass[10pt]{article}
\usepackage[table]{xcolor}
\usepackage{xcolor}
\usepackage[letterpaper, inner=18mm, outer=18mm, top=25mm, bottom=25mm]{geometry}
\usepackage{multicol}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{pgfplotstable}

\usepackage{booktabs}
\usepackage{array}
\usepackage{colortbl}

\begin{filecontents}{data.csv}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
\end{filecontents}
\begin{document}

\begin{table}[h]
  \centering
  \pgfplotstabletypeset[col sep=comma, string type, column type=,
  begin table={\begin{tabularx}{\textwidth}{Xccccccccccccc}},
                 end table={\end{tabularx}},
               every head row/.style={ 
                 before row={
                   \multicolumn{14}{l}{\colorbox{black}{\parbox{\textwidth}{\textcolor{white}{Here is a very long and wordy title}}}}\\
                 }
               },
               every even row/.style={before row = {\rowcolor[gray]{0.9}}},
               font=\footnotesize
               ]{data.csv}
             \end{table}

           \end{document}

答案1

使用合适的标题框尺寸(您需要从文本宽度中删除外部填充)。同时使用所有X列。

\begin{filecontents*}{\jobname.csv}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
\end{filecontents*}

\documentclass[10pt]{article}
\usepackage[letterpaper, inner=18mm, outer=18mm, top=25mm, bottom=25mm]{geometry}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{pgfplotstable}

\usepackage{lipsum} % for a very long title

\begin{document}

\begin{table}[!htp]
\centering

\pgfplotstabletypeset[
  col sep=comma,
  string type,
  column type=,
  begin table={
    \begin{tabularx}{\textwidth}{*{14}{X}}
  },
  end table={\end{tabularx}},
  every head row/.style={ 
    before row={%
      \multicolumn{14}{@{}c@{}}{%
        \colorbox{black}{%
          \parbox{\dimexpr\textwidth-2\fboxsep}{%
            \textcolor{white}{Here is a very long and wordy title}%
          }%
        }%
      }\\
    },
  },
  every even row/.style={before row = {\rowcolor[gray]{0.9}}},
  font=\footnotesize
]{\jobname.csv}

\end{table}

\begin{table}[!htp]
\centering

\pgfplotstabletypeset[
  col sep=comma,
  string type,
  column type=,
  begin table={
    \begin{tabularx}{\textwidth}{*{14}{X}}
  },
  end table={\end{tabularx}},
  every head row/.style={ 
    before row={%
      \multicolumn{14}{@{}c@{}}{%
        \colorbox{black}{%
          \parbox{\dimexpr\textwidth-2\fboxsep}{%
            \textcolor{white}{\lipsum[2]}%
          }%
        }%
      }\\
    },
  },
  every even row/.style={before row = {\rowcolor[gray]{0.9}}},
  font=\footnotesize
]{\jobname.csv}

\end{table}

\end{document}

在此处输入图片描述

答案2

在这种情况下,标题很容易容纳,因此不需要\parbox。但是,如果标题太长,您可以测量不带标题的宽度并使用它。

\documentclass[10pt]{article}
\usepackage[table]{xcolor}
\usepackage{xcolor}
\usepackage[letterpaper, inner=18mm, outer=18mm, top=25mm, bottom=25mm]{geometry}
\usepackage{multicol}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{pgfplotstable}

\usepackage{booktabs}
\usepackage{array}
\usepackage{colortbl}

\usepackage{blindtext}% MWE only

\begin{filecontents}{data.csv}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
\end{filecontents}

\begin{document}

\begin{table}[h]
  \sbox0{% measure width without title
  \pgfplotstabletypeset[col sep=comma, string type,
               every head row/.style={output empty row},
               every even row/.style={before row = {\rowcolor[gray]{0.9}}},
               font=\footnotesize
               ]{data.csv}%
  }% end of savebox
  \centering
  \pgfplotstabletypeset[col sep=comma, string type,
               every head row/.style={output empty row,
                 before row={
                   \multicolumn{14}{c}{\colorbox{black}{\parbox{\dimexpr\wd0-2\tabcolsep}{\textcolor{white}{\blindtext}}}}\\
                 }
               },
               every even row/.style={before row = {\rowcolor[gray]{0.9}}},
               font=\footnotesize
               ]{data.csv}
\end{table}

\end{document}

演示

相关内容