5

I'm working on the following LaTeX code:

\documentclass[a4paper,13pt]{scrartcl}
\usepackage{tikz}
\usepackage{xstring}

\newcommand{\swcase}[1]{
    \IfEqCase{#1}{{a}{1}{b}{2}}
}

\begin{document}
    \begin{tikzpicture}[
    single/.style={draw,circle}
    ]
    \draw node[single,fill=white] at (6,\swcase{a}) {\swcase{a}};
    %\draw node[single,fill=white] at (6,1) {\swcase{a}};
    \end{tikzpicture}
\end{document}

This code fails to compile with pdflatex, and I get the following error message:

! Argument of \XC@definec@lor has an extra }.
<inserted text> 
                \par 
l.14 ... node[single,fill=white] at (6,\swcase{a})
                                                   {\swcase{a}};

The macro \swcase should return 1 when fed with the argument a. You can confirm this by commenting the failing line and uncommenting the one below, where the macro inside the coordinates is substituted with the expected result. The example works as expected and draws a circle with the text 1 (resulting from the macro \swcase) in it. Why is it not working if I embed the \swcase macro inside the coordinate vector?

By the way, it doesn't make any difference if I put the \IfEqCase statement directly into the coordinate parenthesis instead of using the wrapper macro.

3

1 Answer 1

6

In the coordinate argument you need something that expands to a number, but \IfEqCase expands to a series of instructions to print one.

Use an expandable string switch macro, for instance \str_case:nn provided by expl3 (and here loaded by xparse).

\documentclass[a4paper,13pt]{scrartcl}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\swcase}{m}
 {
  \str_case:nn { #1 }
   {
    {a}{1}
    {b}{2}
   }
 }
\ExplSyntaxOff

\begin{document}
    \begin{tikzpicture}[
    single/.style={draw,circle}
    ]
    \draw node[single,fill=white] at (6,\swcase{a}) {\swcase{a}};
    \draw node[single,fill=white] at (6,\swcase{b}) {\swcase{b}};
    \end{tikzpicture}
\end{document}

The syntax is not really different from \IfEqCase, as you see.

enter image description here

If you need it in a \foreach loop, use the “expanded” version:

\documentclass[a4paper,13pt]{scrartcl}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\swcase}{m}
 {
  \str_case_e:nn { #1 }
   {
    {a}{1}
    {b}{2}
   }
 }
\ExplSyntaxOff

\begin{document}

\begin{tikzpicture}[single/.style={draw,circle}]
\foreach \i in {a,b} {
  \draw node[single,fill=white] at (6,\swcase{\i}) {\swcase{\i}};
  \draw node[single,fill=white] at (6,\swcase{\i}) {\swcase{\i}};
}
\end{tikzpicture}

\end{document}
5
  • This works, but if I embed the \draw command inside an \foreach loop, the \swcase macro seems to not yield any result: \foreach \i in {a,b} \draw node[single,fill=white] at (6,\swcase{\i}) {\swcase{\i}};The circles are just drawn above each other at coordinate 0 and the text is missing.
    – Joe Zocker
    Jun 1, 2016 at 14:30
  • 2
    @JoeZocker The fix is easy, done. You can use the \str_case_x:nn function also in the previous case, of course.
    – egreg
    Jun 1, 2016 at 14:34
  • What about if I need to use multiple conditions/variables for the same case, for example CASE1: if a=1 and /orb=2 : then do this and so on.Identifying multiple variables in the same case
    – Silva
    Feb 20, 2020 at 21:11
  • @Silva I believe a new question with the details is in order.
    – egreg
    Feb 20, 2020 at 21:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .