pgfplotstable 中的一个逐字列和一个非逐字标题

pgfplotstable 中的一个逐字列和一个非逐字标题

我想创建一个pgfplotstable,其中有一列将被视为逐字处理。我首先尝试直接使用\verb,但我几乎预料到这会失败,结果确实如此。我发现多列逐字记录但这是另一个问题。

我发现最好的解决方案是使用\lstinlinecollcell。现在的下一个大问题是如何让标题恢复正常,因为单元格内容毕竟是逐字逐句的。

我在这张表上遇到了很多其他小问题,我确信有比我找到的更好的解决方案,所以我想分享这个,希望它可以帮助其他人,同时也看看是否有人有更好的解决方案。

因此,如果您知道一种更优雅的方式来拥有一个带有非逐字标题的逐字列pgfplotstable,我会很乐意接受它。

答案1

首先要简化的是使用 column 属性verb string type而不是string type。它会尽力保留特殊字符。但不幸的是,反斜杠和下划线去标记化。因此,通过后处理,我们希望@cell content用适当的分隔宏来包装或\lstinline\verb等。

这可能是 Chrisitian Feuersanger 的设计选择,目的是简化将控制序列添加到前缀的操作,@cell content但实际上这....../.add={\verb|}{|}对于功能来说已经足够了。但不幸的是,它显示为,\verb | .... |然后逐字分隔符设置为空格。所以这很令人沮丧。似乎每个控制序列都用空格分隔。这可能为我们省去了很多麻烦,但在这里它对我们不利。我也试过了,\csname verb\endcsname|但没有成功。相反,您可以添加debug键并看到当删除这些空格时它确实可以正常工作,没有任何问题。

如果标题有问题,只需抑制它们并手动添加。这比翻了 40 个跟头后再逃避它们要容易得多。

因此,我删除了一些东西以使其更清晰,但除了直接在特定条目上发出之外,我没有任何解决方案来解决临时更改 lst 设置的问题。

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

在此处输入图片描述

答案2

所以这个表的属性是

  • 第一列是逐字(或伪逐字),右边一列是\RaggedRight
  • 标题不是逐字逐句的。
  • 该表是longtable(在这个简短的例子中显然并不重要,但在我的文档中它很重要)。
  • 两行块交替着色和不着色,不同颜色标题后的前两行是白色的(这花费了我大量的摆弄时间)。
  • 有一些逐字单元使用特殊代码。
  • 标题位于表格下方。
  • 标题用于pgfplotstable列命名并longtable显示在每一页上。
  • \右列中有一个\\,虽然\\也是行分隔符,但被转义为 。我说这话好像是件很特别的事情,但与我提到的所有其他事情相反,令我惊讶的是,这真的有效™。

代码如下:

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

表格截图

采用以下代码生成的相同表格来自打击乐的创意。除了他的代码之外,以下longtable内容还保留在:

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

结果与已经显示的结果完全相同,因此没有新图像。

相关内容