Datatool DTLifnull 不起作用

Datatool DTLifnull 不起作用

考虑以下数据库:

Name,ID,Gender,Years in Service
,382473856,M,15
Francesca Joestar,461276432,F,10
Chan Ker Mei,463724631,F,5
Hikaru Yagami,154954739,M,10

第一行的“Name”字段为空。以下代码不起作用。事实上,该函数:

\DTLifnull{\name}{Mister X}{\name}

总是给出结果 FALSE。为什么?

\documentclass[16pt]{scrartcl}
\usepackage[left=3cm,right=3cm,top=6cm,bottom=5cm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgschola}
\usepackage{fourier-orns}
\usepackage{graphicx}
\usepackage{wallpaper}

\usepackage{datatool}
\DTLloaddb{names}{namelist.csv}

\begin{document}
\linespread{2}\selectfont
\pagestyle{empty}
\TileWallPaper{\paperwidth}{\paperheight}{old-border}

\DTLforeach{names}{
\name=Name, \ID=ID, \gender=Gender, \yr={Years in Service}}{
{\centering
    {\LARGE\bfseries Certificate of Appreciation}\par
    {\LARGE\decofourleft\quad{\decoone}\quad\decofourright}\par
    \vskip2em
    We thank\par
    {\large\bfseries\MakeUppercase{\DTLifnull{\name}{Mister X}{\name}}}\par
    {\large (ID: \ID)}\par
    for having been with\par
    {\large XYZ Company}\par
    {for \emph{\LARGE\yr} glorious years}\par
    \vskip1em
    We commend \DTLifeq{\gender}{M}{him}{her} on \DTLifeq{\gender}{M}{his}{her} excellent service.\par
    }

    \vfill

    \hfill
    \begin{minipage}{.4\textwidth}
    Managing Director\par
    \vskip1cm
    \rule{8em}{.5pt}
    \vskip-1em
    (MD's Name)\par
    \end{minipage}

    \pagebreak
}
\end{document}

答案1

这在用户手册的第 66 页datatool(5.5 节结束) 中有记录。

您可以定义一个空性测试:

\newcommand{\myDTLifempty}[3]{%
  \if\relax\detokenize\expandafter{#1}\relax#2\else#3\fi
}

然后说

\DTLforeach{names}{
\name=Name, \ID=ID, \gender=Gender, \yr={Years in Service}}{
{\centering
    {\LARGE\bfseries Certificate of Appreciation}\par
    {\LARGE\decofourleft\quad{\decoone}\quad\decofourright}\par
    \vskip2em
    We thank\par
    {\large\bfseries\MakeUppercase{\myDTLifempty{\name}{Mister X}{\name}}}\par
    {\large (ID: \ID)}\par
    for having been with\par
    {\large XYZ Company}\par
    {for \emph{\LARGE\yr} glorious years}\par
    \vskip1em
    We commend \DTLifeq{\gender}{M}{him}{her} on \DTLifeq{\gender}{M}{his}{her} excellent service.\par
    }
  ...
}

在此处输入图片描述

相关内容