# Trim braces from macro content

I'm having macros with a content which may or may not have extra curly braces. I want to trim the extra braces but without expanding the rest of the content, if possible.

The following code shows the task:

\documentclass{article}
\usepackage{etoolbox}

\begin{document}

\def\TrimBraces#1#2{\edef#1{\expandonce{#2}}}

\def\a{Test}
\TrimBraces\x\a
\show\x
%-- should be ->Test
% OK

\def\b{{Test}}
\TrimBraces\x\b
\show\x
%-- should be ->Test
% but is {Test}

\def\c{Test \textbf{Z}}
\TrimBraces\x\c
\show\x
%-- should be ->Test \textbf{Z}
% OK

\def\d{{Test \textbf{Z}}}
\TrimBraces\x\d
\show\x
%-- should be ->Test \textbf{Z}
% but is {Test \textbf{Z}}

\end{document}


Currently, \TrimBraces is nothing more like a stub which copies one argument to the other. I tried a lot of things with toks, but nothing worked as expected. I hope, somebody has a better idea. Maybe, it's even a known task.

Update: My question is perfectly answered by the answers of David Carlisle, wipet and egreg, but I can only accept one answer...

-
You should have left the tick to David, the basic idea is his. – egreg Jul 10 '14 at 16:06
@egreg OK, done :-) – Thomas F. Sturm Jul 10 '14 at 16:12
@ThomasF.Sturm order is restored to the Universe – David Carlisle Jul 10 '14 at 16:53
@David: You could borrow some bloke's TARDIS. :) – Paulo Cereda Jul 11 '14 at 10:36

\documentclass{article}
\usepackage{etoolbox}

\begin{document}

\def\TrimBraces#1#2{\expandafter\zzz\expandafter#1\expandafter{\expandafter\zz#2\zz}}

\def\zz#1\zz{#1}
\def\zzz#1#2{\expandafter\def\expandafter#1\expandafter{#2}}

\def\a{Test}
\TrimBraces\x\a
\show\x
%-- should be ->Test
% OK

\def\b{{Test}}
\TrimBraces\x\b
\show\x
%-- should be ->Test
% but is {Test}

\def\c{Test \textbf{Z}}
\TrimBraces\x\c
\show\x
%-- should be ->Test \textbf{Z}
% OK

\def\d{{Test \textbf{Z}}}
\TrimBraces\x\d
\show\x
%-- should be ->Test \textbf{Z}
% but is {Test \textbf{Z}}

\end{document}}

-
this does work with \def\a{{foo}bar} :-) – David Carlisle Jul 10 '14 at 12:33
Yes, it does. Works perfectly! :-) – Thomas F. Sturm Jul 10 '14 at 12:39

My solution is only a little alternative to David's solution. The main point is the same: TeX removes outer braces from separated parameter if they exist:

 \def\aa#1\separator{...} \aa {text}\separator  -- #1 is text
\aa {text}b\separator -- #1 is {text}b


And my solution:

 \def\TrimBraces#1#2{\def\tmp{#1}\expandafter\trimbracesA#2\end}
\def\trimbracesA#1\end{\expandafter\def\tmp{#1}}

-

Here's my take; it is assumed that \@nil doesn't appear in the expansion of the second argument.

\documentclass{article}

\makeatletter
\def\TrimBraces#1#2{\expandafter\TrimBraces@aux#2\@nil{#1}}
\def\TrimBraces@aux#1\@nil#2{\def#2{#1}}
\makeatother

\def\a{Test}
\TrimBraces\x\a
\show\x %-- should be ->Test

\def\b{{Test}}
\TrimBraces\x\b
\show\x %-- should be ->Test

\def\c{Test \textbf{Z}}
\TrimBraces\x\c
\show\x %-- should be ->Test \textbf{Z}

\def\d{{Test \textbf{Z}}}
\TrimBraces\x\d
\show\x %-- should be ->Test \textbf{Z}

\def\e{{foo}bar}
\TrimBraces\x\e
\show\x %-- should be ->{foo}bar

\stop


Here's the log

> \x=macro:
->Test.
l.10 \show\x
%-- should be ->Test
?
> \x=macro:
->Test.
l.14 \show\x
%-- should be ->Test
?
> \x=macro:
->Test \textbf {Z}.
l.18 \show\x
%-- should be ->Test \textbf{Z}
?
> \x=macro:
->Test \textbf {Z}.
l.22 \show\x
%-- should be ->Test \textbf{Z}
?
> \x=macro:
->{foo}bar.
l.26 \show\x
%-- should be ->{foo}bar
?
)
No pages of output.

-