\renewcommand\thesection 中出现意外的未定义控制序列

\renewcommand\thesection 中出现意外的未定义控制序列

我试图在文档中提供文档的编辑说明,这意味着有时必须有不属于我的文档且不应属于我的目录的章节和小节编号。通常使用的惯例是使用 unicode 替换字符符号(菱形中的 ?)代替未知章节编号。因为这会使 lualatex 感到困惑,所以它通常呈现为

\usepackage{amssymb,graphicx,stackengine,xcolor}
\def\ucr{\scalebox{2}{\stackinset{c}{}{c}{-.2pt}{%
      \textcolor{white}{\sffamily\bfseries\small ?}}{%
      \rotatebox{45}{$\blacksquare$}}}}

这可以作为文档的一部分。但是,当我尝试将其用作重新定义 \thesection 的一部分时,它会产生错误。即

\renewcommand\thesection{\ucr.\arabic{section}}

结果是:

! 未定义控制序列。\stackinset ...\ifstrequal {#2}{}{\def \stack@tmp {0pt}}{\def \stack@tmp {#2...

一个工作示例:

\documentclass[a4paper,10pt,oneside,openany,final,article]{memoir}

\usepackage{amssymb,graphicx,stackengine,xcolor}
\def\ucr{\scalebox{2}{\stackinset{c}{}{c}{-.2pt}{%
      \textcolor{white}{\sffamily\bfseries\small ?}}{%
      \rotatebox{45}{$\blacksquare$}}}}

\settocdepth{section}

\begin{document}
\tableofcontents*
\chapter{Intro}
An intro to my problem.
\section{A Section for the Table of Contents}
\chapter{Wording}
Please amend the standing document by inserting a new chapter between the current chapters 4 and 5.
\begin{@empty}
  \settocdepth{part}
  \renewcommand\thechapter{X}
  \renewcommand\thesection{X.\arabic{section}}

  \chapter{A New Chapter}
  Some contents for the new chapter
  \section{New Section}
  Some contents for the new section
\end{@empty}
\end{document}

但是,如果将 renewcommand 块中的 X 更改为 \ucr,则编译会失败。

\documentclass[a4paper,10pt,oneside,openany,final,article]{memoir}

\usepackage{amssymb,graphicx,stackengine,xcolor}
\def\ucr{\scalebox{2}{\stackinset{c}{}{c}{-.2pt}{%
      \textcolor{white}{\sffamily\bfseries\small ?}}{%
      \rotatebox{45}{$\blacksquare$}}}}

\settocdepth{section}

\begin{document}
\tableofcontents*
\chapter{Intro}
An intro to my problem.
\section{A Section for the Table of Contents}
\chapter{Wording}
Please amend the standing document by inserting a new chapter between the current chapters 4 and 5.
\begin{@empty}
  \settocdepth{part}
  \renewcommand\thechapter{\ucr}
  \renewcommand\thesection{\ucr.\arabic{section}}

  \chapter{A New Chapter}
  Some contents for the new chapter
  \section{New Section}
  Some contents for the new section
\end{@empty}
\end{document}

答案1

问题:stackinset是脆弱的,并且你的命令也是脆弱的。

脆弱命令和坚固命令之间有什么区别?什么时候以及为什么我们需要 \protect?更多细节。

可能的修复:使您的命令更加健壮:

\documentclass[a4paper,10pt,oneside,openany,final,article]{memoir}

\usepackage{amssymb,graphicx,stackengine,xcolor}
\protected\def\ucr{\scalebox{2}{\stackinset{c}{}{c}{-.2pt}{%
      \textcolor{white}{\sffamily\bfseries\small ?}}{%
      \rotatebox{45}{$\blacksquare$}}}}

\settocdepth{section}

\begin{document}
\tableofcontents*
\chapter{Intro}
An intro to my problem.
\section{A Section for the Table of Contents}
\chapter{Wording}
Please amend the standing document by inserting a new chapter between the current chapters 4 and 5.
\begin{@empty}
  \settocdepth{part}
  \renewcommand\thechapter{\ucr}
  \renewcommand\thesection{\ucr.\arabic{section}}

  \chapter{A New Chapter}
  Some contents for the new chapter
  \section{New Section}
  Some contents for the new section
\end{@empty}
\end{document}

这里唯一的变化是添加了\protected

或者使用xparse's NewDocumentCommand

\NewDocumentCommand\ucr{}{\scalebox{2}{\stackinset{c}{}{c}{-.2pt}{%
      \textcolor{white}{\sffamily\bfseries\small ?}}{%
      \rotatebox{45}{$\blacksquare$}}}}

它有许多额外的功能,请阅读texdoc xparse

相关内容