我正在尝试解析字符串数组,但是当我创建包含单个字符串的数组(假设该字符串包含 n 个字符)时,它无法正确解析,而是被解析为包含 n 个项目,每个项目都是一个字符。有人能解释一下为什么会发生这种情况吗?提前致谢!代码和输出如下:
\documentclass{article}
\usepackage{pgfmath,pgffor}
\begin{document}
\noindent Array 1 is \{"this","is","cool"\}
\def\myArray{{"this","is","cool"}}
\pgfmathparse{dim(\myArray)}
\edef\myArrayLength{\pgfmathresult}
\pgfmathparse{\myArrayLength-1}
\edef\lastIndex{\pgfmathresult}
\foreach \i in {0,...,\lastIndex}
{
\noindent Array 1, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult \newline
}
\noindent Array 2 is \{"not cool"\}
\def\singleItemArray{{"not cool"}}
\pgfmathparse{dim(\singleItemArray)}
\edef\singleItemArrayLength{\pgfmathresult}
\pgfmathparse{\singleItemArrayLength-1}
\edef\lastIndex{\pgfmathresult}
\foreach \i in {0,...,\lastIndex}
{
\noindent Array 2, item \i: \pgfmathparse{\singleItemArray[\i]}\pgfmathresult \newline
}
\end{document}
答案1
我找到了一个解决方案,使用其他数组长度命令和一些 if...else 语句来处理导致问题的单个项目数组。
\def\getlen#1{%
\pgfmathsetmacro{\lenarray}{0}%
\foreach \i in #1{%
\pgfmathtruncatemacro{\lenarray}{\lenarray+1}%
\global\let\lenarray\lenarray}%
}
例子:定义数组
\def\mar{1,2}
,计算长度\getlen{\mar}
并获取值\lenarray %=> '2'
笔记:数组的长度存储在\r
中\edef\r{\lenarray}
。
第2步:扩展单个数组:
\ifnum\r=1
\def\myArray{{\myArrayRoh,"\relax "}}
\else
\def\myArray{{\myArrayRoh}}
\fi
步骤3:...因此,对于每个数组, 您必须使用2
而不是来减少最后一个项目的索引:1
\ifnum\r=1
\pgfmathparse{\myArrayLength-2} % single item
\else
\pgfmathparse{\myArrayLength-1} % multiple items
\fi
结果:
梅威瑟:
\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
% https://tex.stackexchange.com/a/66159/124842 (@Alain Matthes)
\def\getlen#1{%
\pgfmathsetmacro{\lenarray}{0}%
\foreach \i in #1{%
\pgfmathtruncatemacro{\lenarray}{\lenarray+1}%
\global\let\lenarray\lenarray}%
}
\begin{document}
\def\myArrayRoh{"this"} % singleItemArray
\getlen{\myArrayRoh}
\edef\r{\lenarray}
\ifnum\r=1
\def\myArray{{\myArrayRoh,"\relax "}}
\else
\def\myArray{{\myArrayRoh}}
\fi
%
\pgfmathparse{dim(\myArray)}
\edef\myArrayLength{\pgfmathresult}
\ifnum\r=1
\pgfmathparse{\myArrayLength-2}
\else
\pgfmathparse{\myArrayLength-1}
\fi
\edef\lastIndex{\pgfmathresult}
\foreach \i in {0,...,\lastIndex}
{
\noindent Array 1, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult \newline
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\myArrayRoh{"this","is","cool"} % multiItemArray
\getlen{\myArrayRoh}
\edef\r{\lenarray}
\ifnum\r=1
\def\myArray{{\myArrayRoh,"\relax "}}
\else
\def\myArray{{\myArrayRoh}}
\fi
\pgfmathparse{dim(\myArray)}
\edef\myArrayLength{\pgfmathresult}
\ifnum\r=1
\pgfmathparse{\myArrayLength-2}
\else
\pgfmathparse{\myArrayLength-1}
\fi
\edef\lastIndex{\pgfmathresult}
\foreach \i in {0,...,\lastIndex}
{
\noindent Array 2, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult \newline
}
\end{document}
答案2
我建议使用 xstring 作为简单字符串项或使用 listofitems 作为分隔项:
\documentclass{article}
\usepackage{xstring}
\usepackage{pgffor}
\usepackage{listofitems}
\begin{document}
{\bfseries xstring on list:}\vspace{10pt}
\def\myarray{not cool}
\StrLen{\myarray}[\MyStrLen]
Items of "\myarray": \MyStrLen\par
\foreach \i in {1,...,\MyStrLen}{Item \i: ``\StrMid{\myarray}{\i}{\i}''\par}\vspace*{20pt}
{\bfseries listofitems on list:}\vspace{10pt}
\setsepchar{,}
\def\mylist{test item, {another item}, third item}
\readlist\commalist\mylist
Items of "\mylist":\commalistlen\par
\foreachitem\x\in\commalist{Item \xcnt: ``\x''\par}
\end{document}
输出:
评论后编辑:
对于一个项目列表,listofitems
仍然是一个选项。
在上述文件中添加以下代码:
\setsepchar{,}
\def\mylist{"not cool"}
\readlist\commalist\mylist
Items of "\mylist":\commalistlen\par
\foreachitem\x\in\commalist{Item \xcnt: ``\x''\par}\vspace{20pt}
我们得到了想要的结果:
附言:如果我们将物品封装在里面的{}
话就会出现问题,我认为必须向维护人员报告。
答案3
我不确定这是 PGF 的一个错误,因为没有关于数组的正式定义;但事实证明数组一定不长度为 1:
\documentclass{article}
\usepackage{pgfmath,pgffor}
\begin{document}
\verb|\def\myArray{{"this","is","cool"}}|
\def\myArray{{"this","is","cool"}}
\pgfmathsetmacro\myArrayLength{dim(\myArray)}
\pgfmathsetmacro\lastIndex{\myArrayLength-1}
\foreach \i in {0,...,\lastIndex}
{
Array 1, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult\par
}
\verb|\def\myArray{{"notcool"}}|
\def\myArray{{"notcool"}}
\pgfmathsetmacro\myArrayLength{dim(\myArray)}
\pgfmathsetmacro\lastIndex{\myArrayLength-1}
\foreach \i in {0,...,\lastIndex}
{
Array 2, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult\par
}
\verb|\def\myArray{{12}}|
\def\myArray{{12}}
\pgfmathsetmacro\myArrayLength{dim(\myArray)}
\pgfmathsetmacro\lastIndex{\myArrayLength-1}
\foreach \i in {0,...,\lastIndex}
{
Array 3, item \i: \pgfmathparse{\myArray[\i]}\pgfmathresult\par
}
\end{document}
最后一个例子给出与 相同的结果\def\myArray{{1,2}}
。