I am trying to create a table with subheading and automatic row numbers. The original table looks like this:
The latex to generate the table is as follows:
\documentclass{article}
\usepackage{amssymb}
\usepackage{unicode-math}
\setmainfont[Mapping=tex-text, Numbers=OldStyle]{TeX Gyre Pagella}
\setmathfont[math-style=ISO]{TeX Gyre Pagella Math}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{booktabs,colortbl, array}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\definecolor{rulecolor}{RGB}{0,71,171}
\definecolor{tableheadcolor}{gray}{0.92}
% Following is taken from Werner: http://tex.stackexchange.com/a/33761/3061
% and modified for my needs
%
% Command \topline consists of a (slightly modified)
% \toprule followed by a \heavyrule rule of colour tableheadcolor
% (hence, 2 separate rules)
\newcommand{\topline}{ %
\arrayrulecolor{rulecolor}\specialrule{0.1em}{\abovetopsep}{0pt}%
\arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{rulecolor}}
% Command \midline consists of 3 rules (top colour tableheadcolor, middle colour black, bottom colour white)
\newcommand{\midtopline}{ %
\arrayrulecolor{tableheadcolor}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{rulecolor}\specialrule{\lightrulewidth}{0pt}{0pt}%
\arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{0pt}%
\arrayrulecolor{rulecolor}}
% Command \bottomline consists of 2 rules (top colour
\newcommand{\bottomline}{ %
\arrayrulecolor{white}\specialrule{\aboverulesep}{0pt}{0pt}%
\arrayrulecolor{rulecolor} %
\specialrule{\heavyrulewidth}{0pt}{\belowbottomsep}}%
\newcommand{\midheader}[2]{%
\midrule\topmidheader{#1}{#2}}
\newcommand\topmidheader[2]{\multicolumn{#1}{l}{\textbf{\textsc{#2}}} \\%
\addlinespace[0.5ex]}
\newcommand{\ticks}{\makebox[0pt][l]{$\square$}\raisebox{.15ex}{\hspace{0.1em}}}
\pgfplotstableset{create on use/new/.style={
create col/expr={\pgfplotstablerow+1},
fixed,precision=0,
}
}
\pgfplotstableset{normal/.style ={%
header=true,
string type,
font=\addfontfeature{Numbers={Monospaced}}\small,
column type=l,
every odd row/.style={
before row=
},
every head row/.style={
before row={\topline\rowcolor{tableheadcolor}},
after row={\midtopline}
},
every last row/.style={
after row=\bottomline
}
}
}
\begin{document}
\begin{table}
\pgfplotstabletypeset[normal,
col sep=&,
row sep=\\,
%columns={new,Question,Check,Comments},
%columns/new/.style={column type= {p {2cm} }},
columns/Question/.style={column type= {p {7cm} }},
columns/Check/.style={column type=c},
columns/Comments/.style={column type={p{4cm}}},
]{ %
Question & Check & Comments \\
\topmidheader{3}{Heading}
This is row one & \ticks & \\
This is another row & \ticks & \\
\midheader{3}{Heading}
This is another row & \ticks & \\
This is another row & \ticks & \\
This is another row & \ticks & \\
\midheader{3}{Heading}
This is another row & \ticks &\\
This is another row & \ticks & \\
\midheader{3}{Heading}
This is another row & \ticks & \\
This is another row & \ticks & \\
This is another row & \ticks & \\
}
\end{table}
\end{document}
What I want to be able to do is insert a column in front of the Question column, which add the row number but does not count the 'heading' rows for the number count. For example, the last row of the second sub section would be 5 i.e. 2 rows in the first sub section and three rows in the second sub section.
I have created a new column with the command:
\pgfplotstableset{create on use/new/.style={
create col/expr={\pgfplotstablerow+1},
fixed,precision=0,
}
}
and then use the columns={new,Question,Check,Comments}
within the pgfplotstablestypeset
to show the new columns but this results in the following error:Misplaced \noalign.
Can anyone shed any light on what I am doing wrong.
Best Answer
I'm not sure where you want the row numbers to appear. There may be an approach native to
pgfplotstable
.One approach would be to manual insert a command at the beginning of each row for which you want such numbers. Alternatively, you could somewhat automate the process of building the table using the following code:
Row numbers in a
\makebox
of zero widthI put your preamble in its own file which I called
original_preamble.tex
and placed it in its own directorylib
. You'll want to change that. Otherwise, here's a complete solution with an additional keywordrow
that gives you finer control over how and what is placed in each row.Row numbers in their own column
There are five changes to the code above. At the end of each line which I've modified I added the comment
to you easily identify the changes and compare with the original above
Same details that applied to your original preamble also apply here
A few points about the details of the code
Perhaps the most important feature that I use throughout my solution is the method of keywords provided by pgf. In the pgf/TikZ manual, there's an entire chapter dedicated to working with keys. If you're new to LaTeX, it is probably not that important to understand how to create keys as it is to know how to use them. There are two main advantage to keys: (1) they get around the limitation imposed via TeX that macros can have at most nine arguments; (2) they make your code cleaner and easier to read.
Also from the
pgf/TikZ
bundle is the\foreach
loop which I use in this code too. This is well worth your time to read up on and experiment with. For processes that you can easily iterate over in some fashion, you can often simplify things by creative use ofpgf
's\foreach
.The second important feature I'm using is controlling how and when the macros are expanded. This is a very important concept that you'll eventually want to master. But as a beginner, it's not likely to be a pressing issue until you begin to write your own macros. The key to controlling expansion here is the judicious use of
\expandafter
,\noexpand
, and\expandonce
(this last one comes from theetoolbox
package). The best place to read up on expansion is, of course, the TeX Book. But you might also peruse TeX by Topic. This last resource you can access from the command line viaThe
etoolbox
package also provides a fairly nice interface for creating and using booleans. If you have code in which you want to conditionally perform certain actions,etoolbox
boolean macros are nice and easy to understand and use.To read up on
etoolbox
, from the command line you can enter