# Passing a command as option in pgfkeys

I want to pass a command as an option, so that the command is applied to the argument, something like

\PrintSomething{opt = \makebox[5cm]{\textbf{\sffamily #1}}}{Hello}


which should produce the word Hello in a box, sffamily, bold.

I don't know how to do it, so here is M(non-W)E

\documentclass{article}

\usepackage{xparse}
\usepackage{pgfkeys}

\begin{document}

\pgfkeys{
/Section/.is family,
/Section,
%
default/.style = {
opt         =   {},
},
opt = \Format
}

\NewCommandCommand{\PrintSomething}{ m m }{%
\pgfkeys{/Section, default, #1}
\Format{#2}
}

\PrintSomething{opt = \makebox[5cm]{\textbf{\sffamily #1}}}{Hello}

\end{document}


You can use an auxiliary macro that gets defined by your opt key. Note that inside of the first \pgfkeys call you need to double the # for those parameters which should not mean the actual parameter of that .code or .style.

Also I removed a few spurious spaces at in your \PrintSomething, and made the first argument an optional one that defaults to being empty.

\documentclass{article}

\usepackage{xparse}
\usepackage{pgfkeys}

\pgfkeys{
/Section/.is family,
/Section,
%
default/.style = {
% will define \PrintSomethingAUX to just directly output its argument
opt = ##1,
},
opt/.code = {\long\def\PrintSomethingAUX##1{#1}},
}

\NewDocumentCommand{\PrintSomething}{ O{} m }{%
\pgfqkeys{/Section}{default,#1}%
\PrintSomethingAUX{#2}%
}

\begin{document}

\PrintSomething[opt = \makebox[5cm]{\textbf{\sffamily #1}}]{Hello}

\end{document}