# new command with cases?

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:

• I actually need at least 15 cases. – lalebarde May 20 '14 at 18:21
• @lalebarde: I missed one square backet in the second \newcommand after findent=-0.8em while I copied and pasted my code to stackexchange. Sorry. I have corrected it now. – wipet May 20 '14 at 20:20
• @egreg: You are not right. my macro \ifcaseeq works with arbirtary number of parameters. You can try it... – wipet May 20 '14 at 20:21
• @wipet I was using the first version. Now it works as advertised. – egreg May 20 '14 at 20:48

Here is the final code, from jfbu solution, mixed with what I have learnt from wipet and egreg:

\documentclass{article}
\usepackage{lettrine}

\def\eqifcase #1#2#3{\eqifcaseA #1#2#1{#3}\end }

\def\eqifcaseA #1#2#3{\ifx #1#2\eqifcaseDo{#3}\fi \eqifcaseA #1}

\def\eqifcaseDo #1\fi #2\end{\fi #1}

\def\Lettrine #1{\vspace{-4ex}\lettrine[lines=1,findent=%
\eqifcase {#1}{{P}{-0.8em}{T}{-0.6em}}{-0.1em}%
]{\textit{#1}}{}}

\begin{document}\thispagestyle{empty}
\section{section 1}
\Lettrine{P}owerfull macro !
\section{section 2}
\Lettrine{D}efault.
\section{section 3}
\Lettrine{T}remendous!
\section{section 4}
without lettrine to show that thanks to \textit{\\vspace\{-4ex\}} in the macro, we keep the same vertical space between the text baseline and the section title.
\end{document}


Big thanks to all the three of you, you are amazing ! EDIT: update thanks to remarks.

• you can remove the \expandafter in your \Lettrine... ;-) – user4686 May 21 '14 at 8:02
• compiling the code with \vspace{-4ex} makes the big letters overlap on my installation – user4686 May 21 '14 at 8:05
• @jfbu, yes, I have not changed the snapshot. Lettrines are intended to be used after sections. \vspace{-4ex} enables to keep the same space between the section title and the text, without consideration for the lettrine. – lalebarde May 21 '14 at 9:01
• Updated. Of course, this vspace depends on the sections specifications. Here, 2ex would have been better. – lalebarde May 21 '14 at 9:13
• you could perhaps use \smash to kill the vertical space occupied by the lettrine. No need for a \vspace` then. – user4686 May 21 '14 at 10:28