2

I managed to confuse myself with filtering data from a CSV file. For example, in the below example, how do I insert the relevant text?

\documentclass{article}
\usepackage{fontspec}
\usepackage{csvsimple-l3}

\begin{filecontents}{bible.csv}
chapter,verse,text
1,1,{test chapter 1, verse 1}
2,1,{test chapter 2, verse 1}
2,2,{test chapter 2, verse 2}
2,3,{test chapter 2, verse 3}
\end{filecontents}

\begin{document}
\begin{quote}
    <the text of chapter 2 verse 3 to be inserted here>
\end{quote}

\begin{quote}
    <the text of chapter 2 verse 1 to 3 to be inserted here>
\end{quote}
\end{document}

2 Answers 2

3

by checking the manual of csvsimple13, sec. 4.5 Line range p27, we can specify the rows to be displayed. In your case, you want a single row and column named "text". So:

\csvreader[
head to column names,
range = 2
]{bible.csv}{}{%
    \text
}

The command \text allows selecting the column, range = 2 allows specifying the row.

\documentclass{article}
\usepackage{fontspec}
\usepackage{csvsimple-l3}

\begin{filecontents}{bible.csv}
chapter,verse,text
1,1,{test chapter 1, verse 1}
2,1,{test chapter 2, verse 1}
2,2,{test chapter 2, verse 2}
2,3,{test chapter 2, verse 3}
\end{filecontents}

\begin{document}
\begin{quote}
\csvreader[
head to column names,
range = 2
]{bible.csv}{}{%
    \text
}
\end{quote}

\begin{quote}
\csvreader[
head to column names,
range = 1-3
]{bible.csv}{}{%
    \text
}
\end{quote}
\end{document}
2

Here is an approach using the readarray and listofitems packages. I'm not sure if the format of your input file is rigid or not. The code could be streamlined significantly if the input file were in a simpler format. Nonetheless, using the OP's specified format, the result can be achieved. Note that this approach allows verse ranges to be specified across chapter boundaries.

\documentclass{article}
%\usepackage{fontspec}
\usepackage{readarray,listofitems}
\ignoreemptyitems
\begin{filecontents}{bible.csv}
chapter,verse,text
1,1,{test chapter 1, verse 1}
2,1,{test chapter 2, verse 1}
2,2,{test chapter 2, verse 2}
2,3,{test chapter 2, verse 3}
3,1,{test chapter 3, verse 1}
3,2,{test chapter 3, verse 2}
\end{filecontents}

\def\chapvrses[#1,#2]-[#3,#4]{%
  \ifnum#1=#3
    \inchapvrses{#1}{#2}{#4}%
  \else
    \foreachitem\zz\in\chapvrs[]{%
      \ifnum\zzcnt<#1\else
        \ifnum\zzcnt>#3\else
          \ifnum\zzcnt=#1
            \inchapvrses{#1}{#2}{\listlen\chapvrs[#1]}\
          \else
            \ifnum\zzcnt=#3
              \inchapvrses{#3}{1}{#4}%
            \else
              \inchapvrses{\zzcnt}{1}{\listlen\chapvrs[\zzcnt]}\
            \fi
          \fi
        \fi
      \fi
    }%
  \fi 
}
\newcommand\inchapvrses[3]{%
  \foreachitem\z\in\chapvrs[#1]{%
    \ifnum\zcnt<#2\else
      \ifnum\zcnt>#3\else
        \ifnum\zcnt=#2\else\ \fi
        \z
      \fi
    \fi
  }%
}
\begin{document}
% Read file into macro
\readarraysepchar{;}
\readdef{bible.csv}\Bible

% Convert format into something easier: verse, verse; new-chapter-verse, verse, etc.
\newtoks\zzz
\newcommand\addtozzz[1]{\global\zzz\expandafter{\the\zzz#1}}
\newcommand\xaddtozzz[1]{\expandafter\addtozzz\expandafter{#1}}
\newcommand\xxaddtozzz[1]{\expandafter\xaddtozzz\expandafter{#1}}
\newcounter{Bibchap}
\setsepchar{;/,}
\readlist\chapvrs{\Bible}
\foreachitem\z\in\chapvrs[]{%
  \ifnum\zcnt=1\else
    \ifnum\theBibchap=0 \stepcounter{Bibchap}\else
      \ifnum\chapvrs[\zcnt,1]=\theBibchap\relax
        \addtozzz{,}%
      \else
        \stepcounter{Bibchap}%
        \addtozzz{;}%
      \fi
    \fi
    \xxaddtozzz{\chapvrs[\zcnt,3]}%
  \fi
}
\expandafter\def\expandafter\Bible\expandafter{\the\zzz}

% Read \Bible (in preferred format) as a list
\readlist\chapvrs{\Bible}
\begin{quote}
    \chapvrs[2,3]
\end{quote}

\begin{quote}
    \chapvrses[2,2]-[3,1]
\end{quote}
\end{document}

enter image description here

If one instead chose a simpler file format, with no indexing numbers in the file, just comma verse separators and semicolon chapter separators:

{verse}, 
{verse}; 
{new-chapter-verse}, 
{verse}, 
etc.

the code would simplify as

\documentclass{article}
%\usepackage{fontspec}
\usepackage{readarray,listofitems}
\ignoreemptyitems
\begin{filecontents}{bible.csv}
{test chapter 1, verse 1};
{test chapter 2, verse 1},
{test chapter 2, verse 2},
{test chapter 2, verse 3};
{test chapter 3, verse 1},
{test chapter 3, verse 2};
\end{filecontents}

\def\chapvrses[#1,#2]-[#3,#4]{%
  \ifnum#1=#3
    \inchapvrses{#1}{#2}{#4}%
  \else
    \foreachitem\zz\in\chapvrs[]{%
      \ifnum\zzcnt<#1\else
        \ifnum\zzcnt>#3\else
          \ifnum\zzcnt=#1
            \inchapvrses{#1}{#2}{\listlen\chapvrs[#1]}\
          \else
            \ifnum\zzcnt=#3
              \inchapvrses{#3}{1}{#4}%
            \else
              \inchapvrses{\zzcnt}{1}{\listlen\chapvrs[\zzcnt]}\
            \fi
          \fi
        \fi
      \fi
    }%
  \fi 
}
\newcommand\inchapvrses[3]{%
  \foreachitem\z\in\chapvrs[#1]{%
    \ifnum\zcnt<#2\else
      \ifnum\zcnt>#3\else
        \ifnum\zcnt=#2\else\ \fi
        \z
      \fi
    \fi
  }%
}
\begin{document}
\readarraysepchar{}
\readdef{bible.csv}\Bible
\setsepchar{;/,}
\readlist\chapvrs{\Bible}
\begin{quote}
    \chapvrs[2,3]
\end{quote}

\begin{quote}
    \chapvrses[2,2]-[3,1]
\end{quote}
\end{document}

You must log in to answer this question.

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