我需要这样的东西:
\setfield{record1}{field1}{alpha}
\setfield{record1}{field2}{beta}
\setfield{record2}{field1}{gamma}
\setfield{record2}{field2}{delta}
\assign{record3}{record2}
\getfield{record1}{field1} % should expand to alpha
\getfield{record1}{field2} % should expand to beta
\getfield{record2}{field1} % should expand to gamma
\getfield{record2}{field2} % should expand to delta
\getfield{record3}{field1} % should expand to gamma
\getfield{record3}{field2} % should expand to delta
有这个包吗?没有这个assign
命令我也能活下去。
答案1
本质上\setfield
是使用前两个参数构建控制序列。\def
仅设置请求的控制序列(不执行错误检查;...如果记录/字段不存在则c
返回) 。s
name
\getfield
\csname
\endcsname
\relax
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\setfield}[3]{%
\expandafter\def\csname #1@#2\endcsname{#3}%
\listcsgadd{#1}{#2}%
}
\newcommand{\getfield}[2]{%
\ifcsname #1@alias\endcsname
\expandafter\expandafter\expandafter\csname \csname #1@alias\endcsname @#2\endcsname
\else
\csname #1@#2\endcsname
\fi}
\newcommand{\recordalias}[2]{\expandafter\def\csname #2@alias\endcsname{#1}}
\newcommand{\recordcopy}[2]{%
\renewcommand*{\do}[1]{%
\expandafter\edef\csname #2@##1\endcsname{\csname #1@##1\endcsname}}%
\dolistcsloop{#1}%
}
\begin{document}
\setfield{record1}{field1}{alpha}
\setfield{record1}{field2}{$\beta$}
\setfield{record2}{field1}{gamma}
\setfield{record2}{field2}{$\delta$}
\recordalias{record2}{record3}
\recordcopy{record2}{record4}
\getfield{record1}{field1} % should expand to alpha
\getfield{record1}{field2} % should expand to $\beta$
\getfield{record2}{field1} % should expand to gamma
\getfield{record2}{field2} % should expand to $\delta$
\getfield{record3}{field1} % should expand to gamma
\getfield{record3}{field2} % should expand to $\delta$
\setfield{record2}{field1}{$\gamma$}
\getfield{record2}{field1} % should expand to $\gamma$
\getfield{record3}{field1} % should expand to $\gamma$
\getfield{record4}{field1} % should expand to gamma
\end{document}
添加了两种类型的作业:
- 别名通过
\recordalias{<first>}{<second>}
——这使得该<second>
记录成为的别名<first>
; - 复制via
\recordcopy{<first>}{<second>}
- 这将按顺序复制所有字段,<first>
使用<second>
由提供的列表处理etoolbox
。
答案2
\documentclass[10pt]{article}
\usepackage{readarray}
\begin{document}
\readarraysepchar{ }
\def\mydata{alpha beta gamma delta}
\readarray\mydata\myarray[-,2]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]
\end{document}
如果数据在文件中,也没有问题。请注意\readdef
,在读取文件时,设置\ncols
为第一行中检测到的字段数,并且可以使用,而不是显式的2
。\readarray
我还演示了如何使用不同的分隔符(逗号而不是空格),并展示了选项如何*
删除\readarray
前导和尾随空格。
\documentclass[10pt]{article}
\usepackage{readarray,filecontents}
\begin{filecontents*}{mydatafile}
alpha, beta
gamma, delta force
\end{filecontents*}
\begin{document}
\readarraysepchar{,}
\readdef{mydatafile}{\mydata}
\readarray*\mydata\myarray[-,\ncols]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]
\end{document}
最后,如果需要动态更改单元格数据,我在这里实现宏\setfield\<array name>[<row>,<column>]{<data>}
:
\documentclass[10pt]{article}
\usepackage{readarray}
\makeatletter
\gdef\setfield#1[#2,#3]#4{%
\expandafter\gdef\csname\expandafter\@gobble\string#1[#2,#3]\endcsname{#4}}%
\makeatother
\begin{document}
\readarraysepchar{ }
\def\mydata{alpha beta gamma delta}
\readarray\mydata\myarray[-,2]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]
\setfield\myarray[1,2]{$\beta$}
Cell (1,2) now has \myarray[1,2].
\end{document}