\label: 转义或删除 LaTeX 命令

\label: 转义或删除 LaTeX 命令

我公司的 CI 需要一个具有自定义布局的部分分隔页面。
其中一部分是一个彩色框,右上角包含分区编号,而分区标题则对齐左中

我创建了自己的命令:

\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}

问题在于,由于章节标题被用作唯一标签,当章节标题包含 LaTeX 命令时,我会出现编译错误。

有没有办法在将 LaTeX 命令传递给 时转义反斜杠和花括号或删除它们\label
或者是否有另一种方法可以在 中创建唯一标签\newcommand

仅供记录:
引发的错误是:
! Argument of \@gobble has an extra }.<inserted text>\par \beamersection{UnitTest \textit{in} Java}

答案1

我不确定您的预期输出是什么,但从您的代码中我可以想象到以下内容:

\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}

在此处输入图片描述

答案2

\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}

相关内容