0

I want to extract the year (or day or month) of a date which is stored as ISO 8601 in a database.

My MWE looks like:

\documentclass{article}
\usepackage{xstring}

\usepackage{datatool}

\newcommand{\myload}[3][1]{% \DTLgetvalue {\thevalue} {#2} {#1} {\dtlcolumnindex {#2} {#3}}% }

%\get{<DATABASE>}{<FIELD>} \newcommand{\get}[3][1]{% \myload[#1]{#2}{#3} \thevalue% }

% https://tex.stackexchange.com/a/42027/98739 \newcommand{\dfgExtractYear}[1]{\StrBefore[1]{#1}{-}}% \newcommand{\dfgExtractMonth}[1]{\StrBetween[1,2]{#1}{-}{-}}% \newcommand*{\dfgExtractDay}[1]{\StrBehind[2]{#1}{-}}%

% database

\DTLnewdb{name} \DTLnewrow{name} \DTLnewdbentry{name}{dateBirth}{1980-12-04}

\begin{document}

date: \get{name}{dateBirth}\ % works Year: \dfgExtractYear{YYYY-MM-DD}\% works Day: \dfgExtractDay{YYYY-MM-DD}\% works Month: \dfgExtractMonth{YYYY-MM-DD}\% works

Only Year: \dfgExtractMonth{\get{name}{dateBirth}}% does not work

\end{document}

This is the result:

enter image description here

How can I combine the extraction code of the date-part with the entry of the database?

lukascbossert
  • 3,015
  • 1
  • 16
  • 37

1 Answers1

1

The explanation can be found in e.g. https://tex.stackexchange.com/a/632508/250119, or my answer in https://tex.stackexchange.com/a/646000/250119 (case 2.1.1), namely your defined \get is not fully expandable.

To fix this particular case, you must move \myload outside:

\documentclass{article}
\usepackage{xstring}

\usepackage{datatool}

\newcommand{\myload}[3][1]{% \DTLgetvalue {\thevalue} {#2} {#1} {\dtlcolumnindex {#2} {#3}}% }

%\get{<DATABASE>}{<FIELD>} \newcommand{\get}[3][1]{% \myload[#1]{#2}{#3} \thevalue% }

% https://tex.stackexchange.com/a/42027/98739 \newcommand{\dfgExtractYear}[1]{\StrBefore[1]{#1}{-}}% \newcommand{\dfgExtractMonth}[1]{\StrBetween[1,2]{#1}{-}{-}}% \newcommand*{\dfgExtractDay}[1]{\StrBehind[2]{#1}{-}}%

% database

\DTLnewdb{name} \DTLnewrow{name} \DTLnewdbentry{name}{dateBirth}{1980-12-04}

\begin{document}

date: \get{name}{dateBirth}\ % works Year: \dfgExtractYear{YYYY-MM-DD}\% works Day: \dfgExtractDay{YYYY-MM-DD}\% works Month: \dfgExtractMonth{YYYY-MM-DD}\% works

Only Year: \myload{name}{dateBirth}\dfgExtractMonth{\thevalue} \end{document}

(only the second-last line is changed). It's rather inconvenient.

Alternatively, for a convenient option, use the functional package to define both \get and \dfgExtractMonth, that way you'll be able to use \get inside an argument of \dfgExtractMonth, but currently it doesn't support optional argument (see Use \input with siunitx's \num and there are some workarounds)

user202729
  • 7,143