如果 CSV 列為空,請勿執行任何操作 -- 如何實現此目的?

如果 CSV 列為空,請勿執行任何操作 -- 如何實現此目的?

MWE(實際上不完全是MWE,但我認為這樣更容易看出發生了什麼):

\documentclass{article}
\usepackage{csvsimple}
\usepackage{graphicx}

\begin{document}
\setlength\unitlength{1cm}

\csvreader[head to column names]{jobname2.csv}{}{%
\vfill
\artist

\includegraphics[height=37mm]{\filenameone}

\includegraphics[height=37mm]{\filenametwo}

\comment
}
\end{document}

內容jobname2.csv

artist,filenameone,filenametwo,comment
Foo,example-image-a,example-image-b,Nothing
Foobar,example-image-a,,Something

當專欄中沒有任何內容時filenametwo,我不想做任何事情(我不知道如何正確表達)。

答案1

csvsimple提供一個宏,\ifcsvstrequal{X}{Y}{equal}{not equal}如果兩個參數XY相等或不相等(基本上\ifx是兩個\edef-ed 標記列表),則有條件地執行程式碼。您可以在範例中使用該宏,而不需要額外的套件,例如xstring

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
artist,filenameone,filenametwo,comment
Foo,example-image-a,example-image-b,Nothing
Foobar,example-image-a,,Something
Bar,,example-image-b,Anything
\end{filecontents*}

\usepackage{csvsimple}
\usepackage{graphicx}

\begin{document}
\setlength\unitlength{1cm}

\csvreader[head to column names]{\jobname.csv}{}{%
\vfill
\artist

\ifcsvstrequal{\filenameone}{}{(no first image)}{%
    \includegraphics[height=37mm]{\filenameone}
}
\ifcsvstrequal{\filenametwo}{}{(no second image)}{%
    \includegraphics[height=37mm]{\filenametwo}
}

\comment
}
\end{document}

在此輸入影像描述

相關內容