# \label: escape or remove LaTeX commands

The CI of my company requires a section separation Page with a custom layout.
Part of this is a color box containig the section number on the upper right corner while the section title is aligned left center.

I created my own command:

\documentclass[aspectratio=169]{beamer}

%% does not reflect the target layout, just the principle
\newcommand{\beamersection}[1]{\section{#1}\label{#1}
\begin{frame}{}
\colorbox{gray}{\Huge\ref{#1}}\hspace{2em}\huge\nameref{#1}
\end{frame}}

\begin{document}
\beamersection{a \textit{styled} section} %% gives error
\end{document}


the problem with this is, since the section title is used as unique label I get compilation errors, wen the section title contains LaTeX commands.

Is there a way to escape backslashes and curly braces or remove the LaTeX commands while passing them to \label?
Or is there another way of createing a unique label within \newcommand?

Just for the records:
the error thrown is:
! Argument of \@gobble has an extra }.<inserted text>\par \beamersection{UnitTest \textit{in} Java}

• There surely are better ways. Perhaps, instead of showing only what you have, explain what you're after. Of course \nameref will complain when you pass it formatting commands like \textit. Moreover, why would you set a \label with all the formatting commands. I'd suggest an alternative interface. – Werner Oct 24 '16 at 16:08
• I did explain what I need: the section number in a color box and the vertically centered, left aligned name of the section. And what I postesd does what I want except when I want to "style" the section name. – Timothy Truckle Oct 24 '16 at 16:11
• Have a look at the beamer manual, paying attention to \AtBeginSection – user36296 Oct 24 '16 at 16:20
• Using the section title as an automatic label is a very bad idea, just my personal opinion. – user36296 Oct 24 '16 at 16:24
• why do \label{#1} rather than \label{#2} and have a second argument for the label? having the section title as a label is strange, having formatting like \textit in \label is just wrong. – David Carlisle Oct 24 '16 at 16:26

I am not sure what your expected output is, but from your code I image something like this:

\documentclass[aspectratio=169]{beamer}

\setbeamercolor{section page}{bg=gray}
\setbeamerfont{section title}{size=\huge}
\setbeamerfont{section name}{size=\Huge}

\setbeamertemplate{section page}{%
\begin{frame}[plain]
\hfill%
\colorbox{section page.bg}{%
\usebeamerfont{section name}%
\sectionname~\insertsectionnumber%
}\par
\vfill
\usebeamerfont{section title}
\insertsection\par
\vfill
\end{frame}
}

\AtBeginSection{\sectionpage}

\begin{document}
\section{a \texorpdfstring{\itshape styled\upshape}{styled} section}
\frame{test}
\end{document}


• This is the right direction bit still ugly to use... – Timothy Truckle Oct 24 '16 at 16:43
• Using style commands in arguments is ugly, not this :) – user36296 Oct 24 '16 at 16:44
• \texorpdfstring{\itshape styled\upshape}{styled} has duplicated text, so I'd like to change it to \textit{styled}, but then I get the same compile error as with my original solution. – Timothy Truckle Oct 24 '16 at 17:30
• @TimothyTruckle \protect helps: \protect\textit{styled} – Heiko Oberdiek Oct 24 '16 at 18:28
\documentclass[aspectratio=169]{beamer}
\usepackage{color}

\makeatletter

\newif\ifobeystylecommands\obeystylecommandsfalse
%%
\newcommand\disobeystylecommands{%
\ifx\protect\@unexpandable@protect
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{}%
{\texorpdfstring{\obeystylecommandsfalse}{}}%
}%
%%
\newcommand\obeystylecommands{}%
\let\obeystylecommands=\disobeystylecommands
%%
\newcommand\savedobeystylecommands{%
\ifx\protect\@unexpandable@protect
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{}%
{\texorpdfstring{\obeystylecommandstrue}{}}%
}%
%%
\newcommand\CallStyleCommand[1]{%
\ifobeystylecommands
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\texorpdfstring{#1}{\@firstofone}}%
{\@firstofone}%
}%
%%
\newcommand\StyledNameref[1]{%
{%
\let\obeystylecommands=\savedobeystylecommands
\disobeystylecommands
\nameref{#1}%
}%
}%
%%
%% does not reflect the target layout, just the principle
\newcommand{\beamersection}[1]{%
\section{{\obeystylecommands#1}}%
{\disobeystylecommands\label{#1}}%
\begin{frame}{}%
\colorbox{gray}{%
\Huge{\disobeystylecommands\ref{#1}}%
}%
\hspace{2em}%
\huge
\StyledNameref{#1}%
\end{frame}%
}%

\makeatother

\begin{document}

\beamersection{a \CallStyleCommand{\textit}{styled} section}

\beamersection{another \CallStyleCommand{\textbf}{styled} section}

\beamersection{yet another \CallStyleCommand{\textit}{\CallStyleCommand{\footnotesize}{styled}} section}

\beamersection{one more \CallStyleCommand{\textbf}{\CallStyleCommand{\textcolor{green}}{styled}} section}

\nameref{a styled section}

\nameref{another styled section}

\nameref{yet another styled section}

\nameref{one more styled section}

\StyledNameref{a styled section}

\StyledNameref{another styled section}

\StyledNameref{yet another styled section}

\StyledNameref{one more styled section}

\ref{a styled section}

\ref{another styled section}

\ref{yet another styled section}

\ref{one more styled section}

\end{document}