我有一个这样的命令:
\newcommand{\contactdetails}[1]{%
\csvloop{%
file={authors.csv},%
filter equal={#1}{\name},%
command=\printcontact,%
head=true,%
head to column names=true%
}%
}%
它过滤 authors.csv 中与第一个参数同名的条目,然后调用 \printcontact。
这可以正常工作,但前提是大小写完全相同。例如,“John McDonald”与“John Mcdonald”不匹配。有没有办法让过滤器不区分大小写?
我试过
\newcommand{\contactdetails}[1]{%
\csvloop{%
file={authors.csv},%
filter equal={\uppercase{#1}}{\uppercase{\name}},%
command=\printcontact,%
head=true,%
head to column names=true%
}%
}%
但仍然过滤区分大小写。
为什么这不起作用,我应该怎么做才能使csvloop
过滤器不区分大小写?
最小示例:
\documentclass{article}
\usepackage{csvsimple}
\begin{filecontents*}[overwrite]{authors.csv}
name,phone,mail
John McDonald,123-456-789,[email protected]
\end{filecontents*}
\newcommand{\printcontact}[1]{%
\name: \phone
}%
\newcommand{\contactdetails}[1]{%
\csvloop{%
file={authors.csv},%
filter equal={#1}{\name},%
command=\printcontact,%
head=true,%
head to column names=true%
}%
}%
\begin{document}
\contactdetails{John McDonald} % Works
\contactdetails{John Mcdonald} % Doesn't work :(
\end{document}
答案1
这可以通过稍微扩展一下l3
版本来实现csvsimple
:
\begin{filecontents*}[overwrite]{\jobname.csv}
name,phone,mail
John McDonald,123-456-789,[email protected]
\end{filecontents*}
\documentclass{article}
\usepackage[l3]{csvsimple}
\ExplSyntaxOn
\cs_generate_variant:Nn \str_foldcase:n { e }
\NewExpandableDocumentCommand \ifcsvstrcmpfoldcase { m m }
{
\str_compare:eNeTF {\str_foldcase:e { #1 }} = {\str_foldcase:e { #2 }}
}
\keys_define:nn { csvsim }
{
filter~strcmp~foldcase .meta:n = { filter~test = \ifcsvstrcmpfoldcase #1 },
}
\ExplSyntaxOff
\newcommand{\printcontact}[1]{%
\name: \phone
}
\newcommand{\contactdetails}[1]{%
\csvloop{
file=\jobname.csv,
filter strcmp foldcase={#1}{\name},
command=\printcontact,
head=true,
head to column names=true
}%
}
\begin{document}
\contactdetails{John McDonald}
\contactdetails{John Mcdonald}
\end{document}
如果你不想扩展可用的键,你可以折叠参数中的大小写:
\begin{filecontents*}[overwrite]{\jobname.csv}
name,phone,mail
John McDonald,123-456-789,[email protected]
\end{filecontents*}
\documentclass{article}
\usepackage[l3]{csvsimple}
\ExplSyntaxOn
\cs_generate_variant:Nn \str_foldcase:n { e }
\cs_new_eq:NN \foldcase \str_foldcase:e
\ExplSyntaxOff
\newcommand{\printcontact}[1]{%
\name: \phone
}
\newcommand{\contactdetails}[1]{%
\csvloop{
file=\jobname.csv,
filter strcmp={\foldcase{#1}}{\foldcase{\name}},
command=\printcontact,
head=true,
head to column names=true
}%
}
\begin{document}
\contactdetails{John McDonald}
\contactdetails{John Mcdonald}
\end{document}