1

I'm trying to read two variables from a csv file.

function,value
1. pos,some text
2. pos,some other text

This runs before \begin{document} and I wan't the value of 1. pos in \firstvar and the value of 2. pos in \secondvar. Edit: By value I mean the value-column, not the string 1. pos itself.

\usepackage{csvsimple}

\newcommand{\firstvar}{tmpvalone}
\newcommand{\secondvar}{tmpvaltwo}
\csvreader[head to column names]{file.csv}{}%
{
    \ifstrequal{\function}{1. pos}
    {\renewcommand{\firstvar}{\value}}
    {
        \ifstrequal{\function}{2. pos}
        {\renewcommand{\secondvar}{\value}}
        {}
    }
}

Currently, there are two problems:

  1. \ifstrequal doesn't work. I know it has something to do with the expansion of the commands, but I couldn't get it to work, even with \expandafter\ifstrequal\expandafter.
  2. Even if the comparison would work, the \renewcommand would be empty. I tested this, by using \ifnumequal{\thecsvrow}{x} instead of \ifstrequal.

I feel like, I'm really close, but I don't really understand what I'm doing.

2 Answers 2

1

The following code works:

\documentclass{article}

\usepackage{csvsimple}

\begin{filecontents*}[overwrite]{630556.csv}
function,value
1. pos,some text
2. pos,some other text
\end{filecontents*}

\begin{document}

\newcommand{\firstvar}{tmpvalone}
\newcommand{\secondvar}{tmpvaltwo}

\csvreader[head to column names]{630556.csv}{}{
  \ifcsvstrequal{\function}{1. pos}{ \edef\firstvar{\value} } { }
  \ifcsvstrequal{\function}{2. pos}{ \edef\secondvar{\value} }{ }
}

\firstvar \quad \secondvar

\end{document}
1
  • Oh, wow. Thx. I must have overlooked \ifcsvstrequal when reading the docs of csvsimple earlier. Commented Jan 18, 2022 at 2:32
3

Here, I use facilities of the readarray package, as an alternative. EDITED answer.

\begin{filecontents*}{file.csv}
function,value
1. pos,some text
2. pos,some other text
\end{filecontents*}

\documentclass{article}
\usepackage{readarray,forloop}
\newcounter{loopindex}
\readarraysepchar{,}
\readdef{file.csv}\filedef
\readarray*\filedef\var[-,2]
\def\onepos{1. pos}
\def\twopos{2. pos}
\forloop{loopindex}{2}{\value{loopindex}<\numexpr\varROWS+1}{
  \arraytomacro\var[\theloopindex,1]\tmp
  \ifx\tmp\onepos\xdef\firstvar{\var[\theloopindex,2]}\fi
  \ifx\tmp\twopos\xdef\secondvar{\var[\theloopindex,2]}\fi
}
\begin{document}
``\firstvar'' and ``\secondvar''
\end{document}

enter image description here

Furthermore, the 2-D array \var contains all the bits of the file, \var[3,2] contains some other text, while \var[1,1] contains function, and so on.

5
  • I think I made it not clear enough, that I need to select some text based on the row, where 1. pos is, and some other text based on 2. pos. That's the only reason why I'm trying to use \ifstrequal. Commented Jan 18, 2022 at 2:28
  • @Darkproduct It is still not clear to me what syntax you hope to use to achieve what resulting string. Commented Jan 18, 2022 at 2:31
  • I'm generating several forms and I need to change the footer on one single source of truth, because changing this in all documents is tidies. For convenience, I wont to read those from a csv, so this can be changed in the future, from someone who doesn't know much about latex. This means, that the order of the values in the csv file could change. Commented Jan 18, 2022 at 2:38
  • @Darkproduct Please see my edit, if this is what you are looking for. Commented Jan 18, 2022 at 2:42
  • 1
    Yes, that's perfect. +1 I'll still keep the accepted answer, because it was earlier and - for me at least - it is easier to understand and I already use csvsimple in some other parts anyway. Commented Jan 18, 2022 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .