我有一个我创建的命令(我在这里找到的一个变体)。
\newcounter{magicrownumbers}
\newcommand\contactnumber{Nr: \stepcounter{magicrownumbers}\arabic{magicrownumbers}}
它只是一个简单的计数器。使用时,只需将最后一个值加 1。它在下面使用。
现在我遇到的问题是文件内容部分。多次读取时,会重新调用此命令。我想获取文件内容部分以在创建时维护命令的结果。这样每次使用该部分的值时都无法更改。
第一次运行该示例时,您会更清楚地看到这个问题,然后我就能解释清楚。
\documentclass[a4paper, 10pt]{article} % for A4 size paper
\usepackage{array,etoolbox}
\usepackage{datetime2}
\usepackage{array}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{etoolbox}
\usepackage[yyyymmdd]{datetime}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{xmpincl}
\pgfplotsset{compat=1.15}
\pgfplotstableset{
string type,
column type=l,
every head row/.style={
before row=\toprule,
after row=\midrule,
},
every last row/.style={
after row=\bottomrule
},
}
\begin{document}
\newcounter{magicrownumbers}
\newcommand\contactnumber{Nr: \stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\begin{filecontents}{mytable.dat}
Moment Date Type subject
{\contactnumber} {\today} RED Pie
{\contactnumber} {\today} BLUE Pie
{\contactnumber} {\today} RED Apple
{\contactnumber} {\today} BLUE Apple
\end{filecontents}
\pgfplotstableread{mytable.dat}{\mytable}
Whole table:\bigskip
\pgfplotstabletypeset{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=BLUE:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{BLUE}{}{\pgfplotstableuserowfalse}%
},
]{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=RED:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{RED}{}{\pgfplotstableuserowfalse}%
},
]{\mytable}
\end{document}
答案1
我会避免在每一行中放入,而是用带有 的{\contactnumber}
计数器创建一个新列。create col
pgfplotstable
您\pgfplotstablesave
可以保存创建的表以选择之后要显示的行。
\documentclass[a4paper, 10pt]{article} % for A4 size paper
\usepackage[T1]{fontenc}
\usepackage{etoolbox}
\usepackage{array}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage[yyyymmdd]{datetime}
\usepackage{filecontents}
\pgfplotsset{compat=1.15}
\pgfplotstableset{
column type=l,
every head row/.style={
before row=\toprule,
after row=\midrule,
},
every last row/.style={
after row=\bottomrule
},
columns/Date/.style={string type},
columns/Type/.style={string type},
columns/subject/.style={string type},
}
\begin{document}
\begin{filecontents}{mytable.dat}
Date Type subject
{\today} RED Pie
{\today} BLUE Pie
{\today} RED Apple
{\today} BLUE Apple
\end{filecontents}
Whole table:\bigskip
\pgfplotstablesave[col sep=comma,
create on use/Moment/.style={create col/expr={\pgfplotstablerow+1}
},
columns={Moment,Date,Type,subject}
]{mytable.dat}{mytableout.dat}
\pgfplotstableread[col sep=comma]{mytableout.dat}{\mytable}
\pgfplotstabletypeset[
col sep=comma,
columns={Moment,Date,Type,subject},
columns/Moment/.style={
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={Nr. }{},
}
}
]{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=BLUE:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{BLUE}{}{\pgfplotstableuserowfalse}%
},
col sep=comma,
columns={Moment,Date,Type,subject},
columns/Moment/.style={
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={Nr. }{},
}
}
]{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=RED:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{RED}{}{\pgfplotstableuserowfalse}%
},
col sep=comma,
columns={Moment,Date,Type,subject},
columns/Moment/.style={
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={Nr. }{},
}
}
]{\mytable}
\end{document}
答案2
您想在文件写出时冻结号码和日期,因此这filecontents
不是正确的工具。
\documentclass[a4paper, 10pt]{article} % for A4 size paper
\usepackage{array,etoolbox}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\pgfplotsset{compat=1.15}
\pgfplotstableset{
string type,
column type=l,
every head row/.style={
before row=\toprule,
after row=\midrule,
},
every last row/.style={
after row=\bottomrule
},
}
\ExplSyntaxOn
\NewDocumentCommand{\makedatafile}{mmm}
{
\samsammer_makedatafile:nnn { #1 } { #2 } { #3 }
}
\NewExpandableDocumentCommand{\contactnumber}{}
{
\int_eval:n { \l__samsammer_row_int }
}
\cs_new:Nn \samsammer_today:
{
\int_eval:n { \c_sys_year_int } /
\samsammer_pad:n { \c_sys_month_int } /
\samsammer_pad:n { \c_sys_day_int }
}
\iow_new:N \g_samsammer_datafile_stream
\seq_new:N \l__samsammer_datafile_seq
\int_new:N \l__samsammer_row_int
\cs_new_protected:Nn \samsammer_makedatafile:nnn
{
\group_begin:
\cs_set_eq:NN \today \samsammer_today:
\iow_open:Nn \g_samsammer_datafile_stream { #1 }
\iow_now:Nn \g_samsammer_datafile_stream { #2 }
\seq_set_split:Nnn \l__samsammer_datafile_seq { \\ } { #3 }
\int_zero:N \l__samsammer_row_int
\seq_map_inline:Nn \l__samsammer_datafile_seq
{
\int_incr:N \l__samsammer_row_int
\iow_now:Nx \g_samsammer_datafile_stream { ##1 }
}
\iow_close:N \g_samsammer_datafile_stream
\group_end:
}
\cs_new:Nn \samsammer_pad:n
{
\int_compare:nT { #1 < 10 } { 0 }
\int_eval:n { #1 }
}
\ExplSyntaxOff
\begin{document}
\makedatafile{mytable.dat}
{Moment Date Type subject}
{
{\contactnumber} {\today} RED Pie \\
{\contactnumber} {\today} BLUE Pie \\
{\contactnumber} {\today} RED Apple \\
{\contactnumber} {\today} BLUE Apple
}
\pgfplotstableread{mytable.dat}{\mytable}
Whole table:\bigskip
\pgfplotstabletypeset{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=BLUE:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{BLUE}{}{\pgfplotstableuserowfalse}%
},
]{\mytable}
\bigskip\bigskip Table with only rows with column ``Type''=RED:\bigskip
\pgfplotstabletypeset[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Type}\of\mytable%
\ifdefstring{\pgfplotsretval}{RED}{}{\pgfplotstableuserowfalse}%
},
]{\mytable}
\end{document}
这是的内容mytable.dat
:
Moment Date Type subject
{1} {2018/07/19} RED Pie
{2} {2018/07/19} BLUE Pie
{3} {2018/07/19} RED Apple
{4} {2018/07/19} BLUE Apple