[Tex/LaTex] new command with cases

conditionalsmacros

This has been answered here for text outputs with the use of \IfEqCase from the xstring package. But if I want to use the output as a value to set a parameter in a function, it fails.

Here is my MWE:

\documentclass[a4paper]{article} 

\usepackage{lettrine,xstring}

\newcommand*{\Lettrine}[1]{%
    \lettrine[lines=1, findent=\IfEqCase{#1}{%
        {P}{-0.8em}%
        {Q}{1em}%
    }[\PackageError{Lettrine}{Undefined option to Lettrine: #1}{}]%
    ]{\textit{#1}}{}%
}

\newcommand*{\LettrineSimply}[1]{\lettrine[lines=1, findent=-0.8em]{\textit{#1}}{}}

\begin{document} 
\LettrineSimply{P} erfectly working !
\Lettrine{P} ossibly it will work !
\end{document}

\LettrineSimply works, but I want to use different findent depending on the letter. In my attempt with the use of \IfEqCase, I have the following compilation error:

$ pdflatex MWE_lettrine_command.tex 
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
(./MWE_lettrine_command.tex
LaTeX2e <2011/06/27>
Babel <3.9f> and hyphenation patterns for 15 languages loaded.
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/lettrine/lettrine.sty
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
Loading lettrine.cfg
(/etc/texmf/tex/latex/lettrine.d/lettrine.cfg))
(/usr/share/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/share/texmf-dist/tex/generic/xstring/xstring.tex))
(./MWE_lettrine_command.aux)
! Missing number, treated as zero.
<to be read again> 
                   \let 
l.17 \Lettrine{P}
                  ossibly it will work !
? 

If I just ENTER:

! Illegal unit of measure (pt inserted).
<to be read again> 
                   \let 
l.17 \Lettrine{P}
                  ossibly it will work !

Do I try to assign a string to the findent parameter? How can I solve that?

Otherwise, is it possible to define tables or associative maps inside commands?

Best Answer

As your \Lettrine command certainly does some un-expandable things, we don't really need a fully expandable version of \IfEqCase.

lettrine

\documentclass[a4paper]{article} 

\usepackage{lettrine,xstring}

\makeatletter
\newcommand*{\Lettrine}[1]{%
    \IfEqCase{#1}{%
        {P}{\def\@@temp{findent=-0.8em,}}%
        {Q}{\def\@@temp{findent=1em,}}%
    }[\PackageError{Lettrine}{Undefined option to Lettrine: #1}{}%
      \def\@@temp{}]%
    \expandafter\lettrine\expandafter[\@@temp lines=1]{\textit{#1}}{}%
}
\makeatother

\newcommand*{\LettrineSimply}[1]
   {\lettrine[lines=1, findent=-0.8em]{\textit{#1}}{}}

\begin{document}\thispagestyle{empty}

\LettrineSimply{P} erfectly working !
\Lettrine{P} ossibly it will work !
\Lettrine{Q} uite certainly it does work !
%\Lettrine{R} arely are errors encountered ! % generates error as expected
\end{document}
Related Question