如何通过命名或类似方式访问行

如何通过命名或类似方式访问行

我想为表格创建类似以下样式

\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{cc}
A & B \\
a & b \\
c & d \\
\multicolumn{2}{l}{\emph{C}} \\
e & f \\
\multicolumn{2}{l}{\emph{D}} \\
g & h \\
\end{tabular}
\end{table}
\end{document}

主要思想如下:

  • 我有“A B”这个数字,然后我可以在下面输入任意数量的行。我可以用 来解决这个问题pgfplotstable
  • 然后,我想在表格中放置一些“子部分”。这些是位于数据之间并跨越多列的“C”和“D”行。
  • 但是,我希望能够排版任何数据。因此,我不能总是假设子部分的数量是给定的,所以我无法使用 访问行every row no

我的想法是生成一个数据文件,以便

# Header
A  B
a  b
c  d 
# here I want to mark this row using a name or something to mark it as a subheader
C
e  f
# here again another marker
D
g  h

然后,我希望能够将该表加载到我的数据库中\pgfplotstabletypeset,并获得与开始时类似的结果。请注意,另一个数据集可能有更多的子标题,或者中间有更多行。因此,我想标记行,或按名称访问它。我没有找到在手动的

我如何使用来实现这一点pgfplotstable

例如,我正在做的是使用 手动将子部分放入表格中\emph。但是,我想标记这些行以添加一些阴影,甚至用不同的颜色(例如深色亮色,阴影样式)为整个部分添加阴影。主要想法是以某种方式标记这些行(或通过名称访问)以定义它们的格式。

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{booktabs}% this includes the rulers
\pgfplotsset{compat=newest}%

\pgfplotstableset{%
  col sep=&,
  string type,
  every head row/.style={before row=\toprule, after row=\midrule},
  % we need to set the first row by hand
  every first row/.style={after row=\midrule},
  % this takes care of the rest
  every nth row={1}{after row=\midrule}
}

\begin{document}

\pgfplotstabletypeset{
Foo & Bar & Get
\emph{Subsection} & &
A & B &
C & D &
\emph{Subsection 2} & &
E & F &
}

\end{document}

答案1

下面所示的策略之一或许能提供部分答案。

  • 如果您想要对表格内容进行一致的样式设置,则可以定义类似于 HTML 和 CSS 的宏命令。

  • 如果您想要计算章节标题或行数,您可以为它们创建计数器并通过每次调用命令来增加它们。

我展示了每种方法的几种变体。我在普通tabular环境中尝试这些变体时取得了更大的成功。图片中的第一个表是版本pgfplotstable,第二个表是版本tabular

这并没有解决如何自动输入数据并将这种格式应用于数据的问题,这可能是一个使用的Lua机会LuaLaTeX

\documentclass{article}

\usepackage{booktabs}% this includes the rulers

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

\pgfplotstableset{%
  col sep=&,
  string type,
  every head row/.style={before row=\toprule, after row=\midrule},
  % we need to set the first row by hand
  every first row/.style={after row=\midrule},
  % this takes care of the rest
  every nth row={1}{after row=\midrule}
}

% (1) Simple text-substitution macro: 
%       Input any header text followed by correct number of & separators
\newcommand{\tableHeader}[1]{\textit{#1}}

% (2) Subsection counter: 
%       Prints "Subsection" and the number, which increments each time the command is called
\newcounter{tableSubsection}
\setcounter{tableSubsection}{0}
\newcommand{\tableSubsection}{\stepcounter{tableSubsection} \textit{Subsection~\thetableSubsection}}

% (3) Row counting
\newcounter{rowNumber}
\setcounter{rowNumber}{0}
\newcommand{\row}{\stepcounter{rowNumber}}


%*******************
% Similar functions using tabular instead of pgfplots

% Format the end of line (e.g, add \midrule) 
\newcommand{\eol}{\\} 

% Wrap each table row to be numbered in \tr as in HTML; 
%  this allows each line to be counted and the same style to be applied at the end of each
\newcommand{\tr}[1]{\row #1 \eol}

% Multicolumn header: like (1) but extends across columns
%   Must input the number of columns with \columns{#}
\newcounter{columnNumber}
\newcommand{\columns}[1]{\setcounter{columnNumber}{#1}}
\newcommand{\heading}[1]{\midrule \multicolumn{\value{columnNumber}}{c}{\textit{#1}} \\ \midrule}

% Create an environment for table so that the counters can be reset
%  Use the parameter to set the column number (\column command above)
\newenvironment{numberedTable}[1]{\setcounter{rowNumber}{0}\columns{#1}}{}

%*************************************
\begin{document}

%*******************
\pgfplotstabletypeset{
Foo & Bar & Get
% Strategy (1)
\tableHeader{Letters} & &
% Strategy (3)
\row A & B &
\row C & D &
% Strategy (2)
\tableSubsection & & 
\row E & F &
\tableSubsection & &
\row G & H & I
% Strategy (1) and (3)
\tableHeader{There have been \therowNumber{} rows} & &
J &  & K
}
%*******************

\vspace{2\baselineskip}

%*******************
% TABULAR approach

\begin{numberedTable}{3}
%
\begin{tabular}{ccc}\toprule
%
% Un-numbered row (\row not needed)
Foo & Bar & Get \eol
\heading{Subsection}
% Numbered rows
\row A & B & \eol
\row C & D & E \eol
\heading{That was row \therowNumber}
% Or, using \tr for numbered rows
\tr{F & G}
\tr{H & I & J}
\heading{That was row \therowNumber}
\row K & L & \eol\bottomrule
%
\end{tabular}
%
\end{numberedTable}

%****************************************
\end{document}

在此处输入图片描述

相关内容