# How to make this pgfkeys code more compact and streamlined?

I have just managed to create my first command with pgfkeys. I defined a command \myset, whose purpose is to make (for example)

\myset[big]{abc}


correspond to

\bigl\{abc\bigr\}


Part of my goal was to do this in a way that would easily let me create variants later, using different delimiters; in particular, I didn't want my \pgfkeys code to refer to the \{ and \}.

Working from this TeX.SE thread and the relevant section of the PGF manual, here is what I came up with:

\documentclass{article}
\usepackage{pgfkeys}
\pgfkeys{/delimitersizes/.is family, /delimitersizes,
default/.style = {auto},
auto/.style = {leftsize = auto, rightsize = auto},
big/.style = {leftsize = big, rightsize = big},
bigg/.style = {leftsize = bigg, rightsize = bigg},
Big/.style = {leftsize = Big, rightsize = Big},
Bigg/.style = {leftsize = Bigg, rightsize = Bigg},
leftsize/.style = {leftdictionary/#1/.get = \leftdelimitersize},
rightsize/.style = {rightdictionary/#1/.get = \rightdelimitersize},
leftdictionary/.cd,
big/.initial = \bigl,
bigg/.initial = \biggl,
Big/.initial = \Bigl,
Bigg/.initial = \Biggl,
auto/.initial = \left, }
\pgfkeys{/delimitersizes, rightdictionary/.cd,
big/.initial = \bigr,
bigg/.initial = \biggr,
Big/.initial = \Bigr,
Bigg/.initial = \Biggr,
auto/.initial = \right, }

\newcommand{\myset}[2][]{
\pgfkeys{delimitersizes, default, #1}
\leftdelimitersize\{#2\rightdelimitersize\} }

\begin{document}
$\myset[big]{abc}$
\end{document}


This functions as it is supposed to, but considering the finished product, it seems like there's a significant amount of "code duplication", and shuffling around of information before getting to use it. I realize this is a rather open-ended question, but can people suggest how to improve this code to make it more compact and streamlined?

-
You are re-inventing the wheel, with squares. Take a look at \DeclarePairedDelimiter from the mathtools package. – Qrrbrbirlbel Apr 3 '13 at 19:07

This is more streamlined but I'm not sure if using pgf was a requirement in this case.

\documentclass{article}

\newcommand\myset[2][big]{\csname#1l\endcsname\{#2\csname#1r\endcsname\}}

\begin{document}

$\myset{abc}$  $\myset[Bigg]{xyz}$

\end{document}


-

I think David has the right of it, but if you want to do something like this with pgfkeys, here is the style I like. This took me way too long to get right, though I think a year ago, before I got out of practice, it wouldn't have. This is also an argument against doing things this way for something so simple.

\documentclass{standalone}
\usepackage{pgfkeys,pgffor}

\pgfkeys{/delimitersizes/.cd,
util/create/.style = {
util/name = #1,
util/dict =
{#1}
{\csname#1l\endcsname}
{\csname#1r\endcsname}
},
util/name/.style = {
#1/.style = {
leftsize = #1,
rightsize = #1
}
},
util/dict/.style n args = {3}{
leftdictionary/#1/.initial/.expand once = #2,
rightdictionary/#1/.initial/.expand once = #3
},
leftsize/.style = {
leftdictionary/#1/.get =  \leftdelimitersize
},
rightsize/.style = {
rightdictionary/#1/.get = \rightdelimitersize
},
default/.style = auto,
util/create/.list = {big,bigg,Big,Bigg},
% Treat auto specially
util/name = auto,
util/dict = {auto}{\noexpand\left}{\noexpand\right}
}

\newcommand{\myset}[2][]{
\pgfkeys{/delimitersizes/.cd,default,#1}
\leftdelimitersize\{#2\rightdelimitersize\}
}
\begin{document}

$\myset[big]{abc}$
$\myset[Big]{abc}$
$\myset[bigg]{abc}$
$\myset[Bigg]{abc}$

\end{document}


Basically, you create a bunch of utility keys that do the work of defining the keys you actually use, and then call the utility keys repeatedly. This is facilitated by the ever-useful .list handler, which I guess is the core of the code-repetition reduction strategy. Note that if you were going to call keys that made assignments via .loop, those assignments would have to be made globally or else (since \foreach is run in a group) they would be lost.

-