2

In one of my setup, I'm making simple setup for my users, meaning that most of the strings are saved in a data tool DB, and they can retrieve it by activating a command.

So I've created simple hardcoded key-value model in a defined DB. Now I've got an issue with Datatool, as I'd like that my "keys" to be case-insensitive. I don't want to debug in case a user select "sp" or "Sp" instead of "SP". This is simpler for me to spend some time on input validation.

However, my MWE belo is throwing a bunch of error (among others "Undefined sequence"). What am I doing wrong ?

\documentclass{article}
\usepackage{datatool}

\DTLnewdb{dbSemester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SP}
\DTLnewdbentry{dbSemester}{value}{Spring semester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SA}
\DTLnewdbentry{dbSemester}{value}{Autumn semester}

\newcommand{\getSemester}[1]{%
    \DTLfetch{dbSemester}{key}{\MakeUppercase{#1}}{value}
}


\begin{document}
    \getSemester{sa}
    \getSemester{SP}
\end{document}

Update 20/08/2018

I found some answers here and there, about some formatting of the output produced by DTLfetch, but nothing related to passing a non expandable parameters to the DTLfetch command. I'm starting to believe that this is not poassible to achieve.

Otherwise, I've thought of a potential workaround would be to add the uppercase version inside of the table and to duplicate the entries

2

The issue is that typical upper-case macros in LaTeX are not expandable.

See Expandably change letter case and use inside \csname, without a package

\documentclass{article}
\usepackage{datatool,stringstrings}

\DTLnewdb{dbSemester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SP}
\DTLnewdbentry{dbSemester}{value}{Spring semester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SA}
\DTLnewdbentry{dbSemester}{value}{Autumn semester}

\newcommand{\getSemester}[1]{%
    \caseupper[q]{#1}%
    \def\tmpA{\DTLfetch{dbSemester}{key}}%
    \expandafter\tmpA\expandafter{\thestring}{value}
}
\begin{document}
    \getSemester{sa}
    \getSemester{SP}
\end{document}

enter image description here

Here's a version without the use of stringstrings, based on methods provided in the cited question:

\documentclass{article}
\usepackage{datatool}

\def\expupperchar#1{%
  \ifcase\numexpr`#1-`a\relax
   A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or K\or L\or M\or
   N\or O\or P\or Q\or R\or S\or T\or U\or V\or W\or X\or Y\or Z\else
   #1\fi
}

\def\expupper#1{%
  \doexpupperchar#1\relax
}

\def\doexpupperchar#1{%
  \ifx#1\relax
  \else
    \expupperchar{#1}\expandafter\doexpupperchar
  \fi
}

\DTLnewdb{dbSemester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SP}
\DTLnewdbentry{dbSemester}{value}{Spring semester}
\DTLnewrow{dbSemester}
\DTLnewdbentry{dbSemester}{key}{SA}
\DTLnewdbentry{dbSemester}{value}{Autumn semester}

\newcommand{\getSemester}[1]{%
    \edef\thestring{\expupper{#1}}%
    \def\tmpA{\DTLfetch{dbSemester}{key}}%
    \expandafter\tmpA\expandafter{\thestring}{value}
}
\begin{document}
    \getSemester{sa}
    \getSemester{SP}
\end{document}
  • Hi Steven, thanks for the answer, this worked fine! Quick question, is there a way to achieve the same without the additionale "stringstrings" package ? – XaWin Aug 21 '18 at 5:58
  • 1
    @XaWi See revision. – Steven B. Segletes Aug 21 '18 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.