# pgfkeys: Different output depending on a keyword

I need an output like

\cmd --> "S0" (output)

\cmd[] --> "S0" (output)

\cmd[map] ----> "S1 S2 S3" (output)

\cmd[map=true] ----> "S1 S2 S3" (output)

\cmd[map=false] ----> "S0" (output)

I looked here and here. How can I get a command with pgfkey for that?

\documentclass[a4paper]{article}
\usepackage{tikz}
\pgfkeys{/tikz/.cd,
/map/.is family,
map/.is choice,
map/.default=false,
map/true/.code={S1 S2 S3},
map/false/.code={S0},
}
\newcommand{\cmd}[1][]{%
%\tikzset{#1}%
\pgfkeys{map=true}
}

\begin{document}
Test:

\cmd[map]

\cmd[]
\end{document}


\documentclass[a4paper]{article}
\usepackage{pgfkeys} %no need to full-load tikz
\pgfkeys{%/tikz/.cd, you want your top-family, no?
% /map/.is family,
/map/.is choice,
% /map/.default=false, % not useful in choices, you have to call one of them anyway
/map/true/.code={S1 S2 S3},
/map/false/.code={S0},
}
\newcommand{\cmd}[1][false]{%
\pgfkeys{/map=#1}
}

\begin{document}
Test:

\cmd[true]

\cmd % if you use \cmd[] you ends with /map= which is not a choice
\end{document}


Notice that true and false are fully customizable here --- you can call them whatever you want, they're just names.

Anyway, I would rather separate the key management and the code. So I will

1. define a top family for my application
2. add a specific function to set keys in this family
3. use the keys to switch/set things

Like this:

\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap\domapfalse
\pgfkeys{
/cis/.is family, /cis/.cd,
map/.is choice,
map/true/.code={\domaptrue},
map/false/.code={\domapfalse},
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}

\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}

\begin{document}

Test:
\par No map: \cmd
\par Map (locally): \cmd[map=true]
\par No map: \cmd
\par Set globally to true \cisset{map=true}
\par Map: \cmd
\par No map (locally):  \cmd[map=false]
\par Set globally to false \cisset{map=false}
\par No map: \cmd

\end{document}


This is a bit low level, you can use xifthen package if you are addressing just LaTeX.

If you want a simple map key, you just do not use the is choice handler:

\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap
\pgfkeys{
/cis/.is family, /cis/.cd,
map/.code={\domaptrue},
no map/.code={\domapfalse},
no map % set the default
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}

\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}

\begin{document}

Test:
\par No map: \cmd
\par Map (locally): \cmd[map]
\par No map: \cmd
\par Set globally to true \cisset{map}
\par Map: \cmd
\par No map (locally):  \cmd[no map]
\par Set globally to false \cisset{no map}
\par No map: \cmd

\end{document}


And finally, this allows to have both the map switch and a map=... switch, similar to lot of options in TikZ. This uses an auxiliary key and a style with an (optional, thanks to the .default) argument.

\documentclass[a4paper]{article}
\usepackage{pgfkeys}
\newif\ifdomap
\pgfkeys{
/cis/.is family, /cis/.cd,
domap/.is choice,
domap/false/.code={\domapfalse},
domap/true/.code={\domaptrue},
map/.style={domap=#1},
map/.default=true, % for this key
domap=false, % global default
}
\newcommand{\cisset}[1]{\pgfkeys{/cis/.cd, #1}}

\newcommand{\cmd}[1][]{%
\begingroup % change the keys locally
\cisset{#1}%
\ifdomap
S1 S2 S3
\else
S0
\fi
\endgroup
}

\begin{document}

Test:
\par No map: \cmd
\par Map (locally): \cmd[map]
\par Map (locally): \cmd[map=true]
\par No map (locally, forced): \cmd[map=false]
\par No map (default): \cmd
\par Set globally to true \cisset{map} % or \cisset{map=true}
\par Map: \cmd
\par No map (locally):  \cmd[map=false]
\par Set globally to false \cisset{map=false}
\par No map: \cmd

\end{document}


A very nice answer is also https://tex.stackexchange.com/a/114017/38080

• Ahh, good. Is it possible to create that like \cmd[map] or \cmd[] = \cmd[map=false]? Because, there should come lots of other parameters / keys, so "\cmd[true]" is not a good syntax. – cis Jul 3 at 8:25
• @cis like this? This is basically how I use the keys in circuitikz. There is even a special key handler to automatically associate to an if, but I find this easier to read. – Rmano Jul 3 at 8:52
• I have two problems here: - \cmd[map] alone seems not to work. - But fundamentally I wonder why this seems not to be possible with "plain pgfkeys syntax" – cis Jul 3 at 9:23
• Because: It is clear that this is an abstract, simplified MWE from me. I don't just want to write "S1 S2 S3". I can hardly list the 17 lines that are hidden behind "S2" in the "MWE". That I basically need numerous other keys - that means: If this is only possible in such a complicated way, I cannot use it. But it has to be easier. Mmhhh ... I should basically understand this pgfkey syntax. Then I could do the rest alone. But, as I said, if this is only possible with all possible other "plain TeX" methods, then I can not deduce from it. – cis Jul 3 at 9:23