3

I wanted to create a pgfplotstable in which one column would be treated as verbatim. I first tried to straightforwardly use \verb but I almost expected this to fail spectacularly and it did. I found Multicolumn verbatim but it was a different problem.

The best solution I found myself was to use \lstinline and collcell. The next big problem now was how to get the header back to normal since the cell content was verbatim after all.

I ran into a lot of other smaller problems with this table and I'm convinced there is a better solution than what I found, so I wanted to share this hoping it might help others and also to see if someone has a better solution.

So if you know a more elegant way to have a verbatim column with a non-verbatim header in pgfplotstable, I'll gladly accept it.

  • The verbatimbox package is also good for getting verbatim into a table. It puts the verbatim text into a box (outside of the table), and the box can be recalled (typeset) inside the table. – Steven B. Segletes Jul 24 '13 at 1:10
3

First thing that eases up is the column property verb string type instead of string type. It will try its best to keep the special characters. But unfortunately backslashes and underscores are detokenized. So by postprocessing, we want to wrap the @cell content with a proper delimited macro either \lstinline or \verb or etc.

This is probably a design choice by Chrisitian Feuersanger for easing up prefixing control sequences to the @cell content but in fact ....../.add={\verb|}{|} would have been enough for the functionality. But unfortunately, it comes out as \verb | .... | and then verbatim delimiter is set to whitespace. So that's a bummer. It seems that every control sequence is separated with a whitespace. This probably saves us from lots of trouble but here it's working against us. I've also tried \csname verb\endcsname| but nope. Instead you can add the debug key and see that when those spaces are removed it really works without any problems.

If you have problems with the headers, just supress them and add manually. It's way easier than escaping them after 40 somersaults.

So I've removed a few things to make it clearer but I don't have any solution for the temporary change lst settings other than directly issuing at that particular entry.

\documentclass[a5paper]{article}
\usepackage{colortbl,pgfplotstable,listings}

\lstset{basicstyle=\ttfamily,breaklines=true,breakatwhitespace=true}
\pgfplotsset{compat=1.8}

\definecolor{cellshade}{rgb}{0.42, 0.55, 0.84}
\definecolor{lightcellshade}{rgb}{0.77, 0.82, 0.93}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\begin{table}%
\pgfplotstabletypeset[col sep=ampersand,row sep=\\,
every head row/.style={output empty row,before row={
             \rowcolor{cellshade}\textcolor{white}{Name} &\textcolor{white}{Description}\\
         }%
      },
columns/Name/.style={verb string type,
                     column type=p{0.4\linewidth},
                     postproc cell content/.code={\edef\temp{\noexpand\lstinline\noexpand{##1}}%
                     \pgfkeyslet{/pgfplots/table/@cell content}{\temp}%
                            },
    },
columns/Description/.style={column type=p{0.6\linewidth},string type},
/pgfplots/table/every nth row={4[-2]}{before row={\rowcolor{lightcellshade}}},
/pgfplots/table/every nth row={4[-1]}{before row={\rowcolor{lightcellshade}}},
every row no 3 column no 0/.style={postproc cell content/.append style={
       @cell content/.add={\lstset{breakatwhitespace=false}}{}}
       },
]{
Name                                &Description\\
boring test                         &Shouldn't be problematic.\\
flat_worm_test                      &The underscore causes a missing \$ if \texttt{verb} isn't used.\\
advanced token([0-9]*)_(.*)         &Now this can really blow everything to pieces.\\
very-long-keyword_without_space     &For cosmetic reasons lines are only wrapped at spaces but sometimes this is not possible and we need an exception.\\
other advanced token([0-9]*)_(.*)   &But this time we also have some inline code in this column.\\
}
\caption[Test]{This is a test table. \texttt{verb} stuff is always a pain.}
\label{}
\end{table}
\end{document}

enter image description here

  • Every time I see someone use pgfplotstable who has a thorough understanding of it, I hear my inner Ygritte, telling me that really, I know nothing. Thanks for delving into how the cell content and its surroundings are handled. Very insightful :) – Christian Jul 24 '13 at 2:46
  • @Christian Thank you for your kind words. However I only have a thorough understanding of the manual :-) These verbatim handling issues are at border or beyond my understanding too. – percusse Jul 24 '13 at 6:02
5

So properties of this table are

  • The first column is verbatim (or pseudo-verbatim), the right column is \RaggedRight.
  • The header isn't verbatim.
  • The table is a longtable (which obviously isn't important in this short example but in my document it is).
  • Blocks of two rows are alternatingly coloured and not coloured, with the first two rows after the differently coloured header being white (this cost me a surprising amount of fiddling around).
  • There are verbatim cells that use special code.
  • The caption is below the table.
  • The header is used by pgfplotstable for the column naming and by longtable to be displayed on every page.
  • There's a \ in the right column which is escaped as \\, even though \\ is also the line delimiter. I'm saying this as if this was something special but in contrast to all the other things I mentioned, this to my astonishment Just Worked™.

Here's the code:

\documentclass[a5paper]{article}
\usepackage{colortbl}
\usepackage{longtable}
\usepackage{pgfplotstable}
\usepackage{collcell}
\usepackage{listings}
% Breaking lines not only at whitespaces can lead to ugly results
\lstset{basicstyle=\ttfamily,breaklines=true,breakatwhitespace=true}
\usepackage{array}
\usepackage{ragged2e}
% So the easiest way I found (that was a lie, the *only* way I found)
% to get a whole column verbatim was via a pseudo-verbatim command
% such as provided by the listings package.
\newcommand{\tablecollisting}[1]{\lstinline{#1}}
\newcolumntype{V}[1]{>{\collectcell\tablecollisting}p{#1}<{\endcollectcell}}
\newcolumntype{y}[1]{>{\RaggedRight\arraybackslash}p{#1}}
% Define some nice colours:
\definecolor{cellshade}{rgb}{0.42, 0.55, 0.84}
\definecolor{lightcellshade}{rgb}{0.77, 0.82, 0.93}
% Sans serif
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\thispagestyle{empty}
% Some calculations to get to exactly one line width with the two columns.
\newlength{\halfacolumnwidth}
\setlength{\halfacolumnwidth}{.5\linewidth}
\addtolength{\halfacolumnwidth}{-2\tabcolsep}
% Here comes the table
\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
every head row/.append style={after row=\endhead, after row=\endhead},
}
\pgfplotstabletypeset[
col sep=ampersand,
row sep=\\,
header=true,
columns/Name/.style={column type=V{\halfacolumnwidth}, string type, column name={\multicolumn{1}{y{\halfacolumnwidth}}{\normalfont\color{white} Name}}
},
columns/Description/.style={column type=y{\halfacolumnwidth},string type,column name={{\color{white} Description}}},
every head row/.style={before row=\rowcolor{cellshade}},
/pgfplots/table/every nth row={4[-2]}{before row={\rowcolor{lightcellshade}}},
/pgfplots/table/every nth row={4[-1]}{before row={\rowcolor{lightcellshade}}},
every last row/.style={after row=\caption[Test]{This is a test table. \texttt{verb} stuff is always a pain.}\\}
]{
Name&Description\\
boring test&Shouldn't be problematic.\\
flat_worm_test&The underscore causes a missing \$ if \texttt{verb} isn't used.\\
advanced token([0-9]*)_(.*)&Now this can really blow everything to pieces.\\
\multicolumn{1}{y{\halfacolumnwidth}}{{\lstinline[breakatwhitespace=false]|very-long-keyword_without_space|}}&For cosmetic reasons lines are only wrapped at spaces but sometimes this is not possible and we need an exception.\\
other advanced token([0-9]*)_(.*)&But this time we also have some inline code in this column: {\lstinline|'\\2' (0 = false, 1 = true)|}.\\
}
\end{document}

table screenshot

The same table produced with code that adopts ideas from percusse. Other than his code, this keeps the longtable stuff in:

\documentclass[a5paper]{article}
\usepackage{colortbl}
\usepackage{longtable}
\usepackage{pgfplotstable}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,breaklines=true,breakatwhitespace=true}
\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\definecolor{cellshade}{rgb}{0.42, 0.55, 0.84}
\definecolor{lightcellshade}{rgb}{0.77, 0.82, 0.93}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\newlength{\halfalinewidth}
\setlength{\halfalinewidth}{.5\linewidth}
\addtolength{\halfalinewidth}{-2\tabcolsep}
\thispagestyle{empty}
\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}
\pgfplotstabletypeset[%
col sep=ampersand,
row sep=\\,
every head row/.style={output empty row, before row={
             \rowcolor{cellshade}\textcolor{white}{Name} &\textcolor{white}{Description}\endhead
         }%
      },
columns/Name/.style={verb string type,
                     column type=p{\halfalinewidth},
                     postproc cell content/.code={\edef\temp{\noexpand\lstinline\noexpand{##1}}%
                     \pgfkeyslet{/pgfplots/table/@cell content}{\temp}%
                            },
    },
columns/Description/.style={column type=L{\halfalinewidth},string type},
/pgfplots/table/every nth row={4[-2]}{before row={\rowcolor{lightcellshade}}},
/pgfplots/table/every nth row={4[-1]}{before row={\rowcolor{lightcellshade}}},
every row no 3 column no 0/.style={postproc cell content/.append style={
       @cell content/.add={\lstset{breakatwhitespace=false}}{}}},
every last row/.style={after row=\caption[Test]{This is a test table. \texttt{verb} stuff is always a pain.}\\},
]{
Name&Description\\
boring test&Shouldn't be problematic.\\
flat_worm_test&The underscore causes a missing \$ if \texttt{verb} isn't used.\\
advanced token([0-9]*)_(.*)&Now this can really blow everything to pieces.\\
very-long-keyword_without_space&For cosmetic reasons lines are only wrapped at spaces but sometimes this is not possible and we need an exception.\\
other advanced token([0-9]*)_(.*)&But this time we also have some inline code in this column: {\lstinline|'\\2' (0 = false, 1 = true)|}.\\
}
\end{document}

The result is exactly the same as the one already shown so no new image.

  • @Jake You're very welcome :) But do you mean I should open a new question for each of the comparatively minor problems I solved? I'm not sure, I thought that to be overkill ... – Christian Jul 23 '13 at 19:29
  • 1
    @Jake I see. I hope it's a bit clearer now, even though I still don't follow a strict question/answer scheme. – Christian Jul 23 '13 at 20:38

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.