6

I want to create a style for tables similar to

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

The main idea is as following:

  • I have the hearder "A B", and then I can put any number of rows bellow. That I can solve with pgfplotstable.
  • Then, I want to put some kind of "subsections" within the table. Those are the "C" and "D" rows that are in between the data and span several columns.
  • However, I want to be able to typeset any data. So, I can't always assume that the subsections will be in a given number, so I can't access the rows using every row no.

My idea was to generate a data file such that

# 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

Then, I want to be able to load that table into my \pgfplotstabletypeset and get something similar to what I have at the begining. Note that another data set may have more subheaders, or more rows in between. Thus, I would like to mark the row, or access it by name. I didn't find a way of doing this in the manual.

How can I achieve this using pgfplotstable?

For example, what I'm doing is to manually put the subsection in the table using \emph. However, I would like to mark those rows to put some shading or even shade the entire section with different colors (something like dark bright dark, shading style). The main idea is to have those rows marked somehow (or access by name) to define a format for them.

\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
    It might help others to help you if you could provide a real MWE (already using pgfplotstable; applying your intended formatting to the hard-coded row numbers). – Daniel Nov 12 '13 at 21:40
  • I added a MWE with a work around. Although is really far from what I intended to do. – adn Nov 14 '13 at 19:09
3

One of the strategies shown below might provide part of the answer.

  • If you want to style tabular content consistently, you can define macro commands similarly to HTML and CSS.

  • If you want to count section titles or rows, you can create counters for these and increment them by calling a command each time.

I've shown several variations on each method. I had more success trying these out in a plain old tabular environment. The first table in the image is the pgfplotstable version, and the second is the tabular one.

This does not address the question of how to automatically input the data and apply this formatting to it, which might be an opportunity to use Lua in 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}

enter image description here

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.