如何使用 csvsimple 之外的宏仅输入某些列?

如何使用 csvsimple 之外的宏仅输入某些列?

我想定义一个命令,例如\newcommand \Fruittype {Strawberries},仅输入包含该命令的列中的数据。有人能帮我吗?

\documentclass[a4paper,12pt,hidelinks]{article}

\usepackage[utf8]{inputenc} 
%\usepackage[utf8x]{inputenc} 
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}

\usepackage{filecontents}
\usepackage{csvsimple}

\begin{filecontents*}{mylist.csv}
Data, Bananas, Strawberries, Melons
Fruit, Bananas, Strawberries, Melons
Color, Yellow, Red, Green
Seeds, No, Yes, Yes
Taste, Good, Very Good, So so
\end{filecontents*}

\begin{document}

\newcommand \Fruittype {Strawberries}

\newcommand{\definekeytovalue}[2]{%
  \expandafter\newcommand
    \csname#1\expandafter\expandafter\expandafter\endcsname
    \expandafter\expandafter\expandafter{#2}%
}

\csvloop{
  file={mylist.csv},
  head to column names,
  command=\definekeytovalue{\Data}{\Strawberries}
}

I like {\Fruit} because they are {\Color}, and they taste {\Taste}.\\

\end{document}

答案1

这似乎就是你所追求的:

\begin{filecontents*}{\jobname.csv}
Data, Bananas, Strawberries, Melons
Fruit, Bananas, Strawberries, Melons
Color, Yellow, Red, Green
Seeds, No, Yes, Yes
Taste, Good, Very Good, So so
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}

\newcommand{\Fruittype}[1]{%
  \csvloop{
    file={\jobname.csv},
    head to column names,
    command=\associatekey{#1}
  }%
}
\newcommand{\associatekey}[1]{%
  \expandafter\def\csname\Data\expandafter\expandafter\expandafter\endcsname
    \expandafter\expandafter\expandafter{#1}%
}

\begin{document}

\Fruittype {Strawberries}

I like {\Fruit} because they are {\Color}, and they taste {\Taste}.

\end{document}

在此处输入图片描述

答案2

我找到了一个解决方案,如下所示:

\newcommand \auxcsv[1]{
\csvloop{
  file={./CSV/gdl.csv},
  separator=semicolon,
  head to column names,
  command=\definekeytovalue{\Dados}{#1}
}
}

\IfStrEqCase{\local}{{1}{\auxcsv{\LocalA}}{2}{\auxcsv{\LocalB}}{3}{\auxcsv{\LocalC}}{4}{\auxcsv{\LocalD}}{5}{\auxcsv{\LocalE}}{6}{\auxcsv{\LocalF}}{7}{\auxcsv{\LocalG}}{8}{\auxcsv{\LocalH}}{9}{\auxcsv{\LocalI}}{10}{\auxcsv{\LocalJ}}{11}{\auxcsv{\LocalK}}{12}{\auxcsv{\LocalL}}{13}{\auxcsv{\LocalM}}{14}{\auxcsv{\LocalN}}{15}{\auxcsv{\LocalO}}{16}{\auxcsv{\LocalP}}{17}{\auxcsv{\LocalQ}}{18}{\auxcsv{\LocalR}}{19}{\auxcsv{\LocalS}}{20}{\auxcsv{\LocalT}}}[]

相关内容