我设法将自己与从 CSV 文件中过滤数据混淆了。例如,在下面的示例中,我如何插入相关文本?
\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}
答案1
通过检查手册csvsimple13
,第 4.5 节线路范围p27,我们可以指定要显示的行。在您的例子中,您需要一行和一列,名为“text”。所以:
\csvreader[
head to column names,
range = 2
]{bible.csv}{}{%
\text
}
该命令\text
允许选择列,range = 2
允许指定行。
\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
readarray
这是使用和包的方法listofitems
。我不确定输入文件的格式是否严格。如果输入文件的格式更简单,代码可以大大简化。尽管如此,使用 OP 指定的格式,结果还是可以实现的。 请注意,这种方法允许跨章节边界指定诗句范围。
\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}
如果选择更简单的文件格式,文件中没有索引号,只有逗号分隔符和分号章节分隔符:
{verse},
{verse};
{new-chapter-verse},
{verse},
etc.
代码将简化为
\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}