Possible Duplicate:
Programming with LaTeX3
I was going to post a question about creating a switch within a command to determine formatting for a command and came across the following link
String test, with complicated arguments in which I discovered the expl3
equivalents of \makeatletter
and \makeatother
in \ExplSyntaxOn
and \ExplSyntaxOff
. So I tried to make my command as follows:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mytestcmd}{ O{a} m}
{
\prg_case_str:nnn { #1 }
{
{ a } { A }
{ b } { B }
{ c } { CC }
{ d } { DED }
}
:#2
}
\ExplSyntaxOff
\begin{document}
\mytestcmd{Hello}
\mytestcmd[b]{Hello}
\mytestcmd[c]{Hello}
\mytestcmd[d]{Hello}
\end{document}
But then I got the unexpected result that my colon from :#2
was not showing up in the actual rendered text. I'm sure there's a simple solution for this. I'm not actually so interested in someone telling me how to fix is particular issue with the colon.
However, I do find the expl3 documentation quite daunting. Is there an expl3 primer or some kind of expl3 for dummies around where I can learn some thing without feeling like I'm drowning?
Incidentally, normally I would handle the switch as in the following MWE:
\documentclass{article}
\usepackage{xparse}
\makeatletter
\def\my@test@cmd@a{a}
\def\my@test@cmd@b{b}
\def\my@test@cmd@c{c}
\def\my@test@cmd@d{d}
\NewDocumentCommand{\mytestcmd}{ O{a} m}
{%
\def\my@test@cmd{#1}%
\ifx\my@test@cmd\my@test@cmd@a{A}\fi%
\ifx\my@test@cmd\my@test@cmd@b{B}\fi%
\ifx\my@test@cmd\my@test@cmd@c{C}\fi%
\ifx\my@test@cmd\my@test@cmd@d{D}\fi%
:#2%
}
\makeatother
\begin{document}
\mytestcmd{Hello}
\mytestcmd[b]{Hello}
\mytestcmd[c]{Hello}
\mytestcmd[d]{Hello}
\end{document}
What I like about the expl3
syntax is it seems to be much kinder in building a true switch than my work around.
latex3
\IfEqCase
from thexstring
package can be used to case type statements. See for example Making switch/case with etoolbox's \ifdefequal. BTW, I'd recommend you not delete the question as others may come across this in their search and then will can follow the duplicate link.\prg_case_str:nnn
got renamed as\str_case:nnn
a while ago. Second, this function requires three arguments (nnn
) and you've only given two. So the:
is taken as the third one, which is why it vanishes. You need an empty argument ({ }
) after the end of the cases.\str_case:nnn
it complains about an undefined control sequence. Despite this, I appreciate your help