使用命令设置标签时出现多个错误

使用命令设置标签时出现多个错误

我定义了一个命令来创建带有标签的部分。根据只使用一个参数还是同时使用两个参数,部分名称和标签可以不同。我创建了以下 MWE:

\documentclass{article}
\usepackage{xifthen}

\newcommand{\labeledsection}[2][]{%
    \section{#2}%
    \label{\ifthenelse{\isempty{#1}}{#2}{#1}}%
}   

\begin{document}
    \labeledsection{Section 1 name}
    Section should be: 'Section 1 name'\\
    Label should be: 'Section 1 name'

    \labeledsection[Section 2 label]{Section 2 name}
    Section should be: 'Section 2 name'\\
    Label should be: 'Section 2 label'
\end{document}

当我尝试编译它时,收到以下错误消息:

Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Illegal parameter number in definition of \reserved@a. \labeledsection{Section 1 name}
Line 10: Extra \or. \labeledsection{Section 1 name}
Line 10: Illegal parameter number in definition of \reserved@a. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Illegal parameter number in definition of \reserved@a. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Paragraph ended before \@sect was complete. \labeledsection{Section 1 name}
Line 10: Illegal parameter number in definition of \reserved@a. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Paragraph ended before \@sect was complete. \labeledsection{Section 1 name}
Line 10: Extra \or. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Undefined control sequence. \labeledsection{Section 1 name}
Line 10: Illegal parameter number in definition of \reserved@a. \labeledsection{Section 1 name}
Line 10: Missing \endcsname inserted. \labeledsection{Section 1 name}
Line 10: Missing \endcsname inserted. \labeledsection{Section 1 name}
Line 10: Missing \endcsname inserted. \labeledsection{Section 1 name}
Line 10: Argument of \@sect has an extra }. \labeledsection{Section 1 name}
Line 10: Paragraph ended before \@sect was complete. \labeledsection{Section 1 name}
Line 10: Extra }, or forgotten \endgroup. \labeledsection{Section 1 name}
Line 10: Extra }, or forgotten \endgroup. \labeledsection{Section 1 name}
Line 10: Missing control sequence inserted. \labeledsection{Section 1 name}
Line 10: Extra \endcsname. \labeledsection{Section 1 name}
Line 10: Missing control sequence inserted. \labeledsection{Section 1 name}
Line 10: Missing control sequence inserted. \labeledsection{Section 1 name}
Line 17: Use of \par doesn't match its definition. \end{document}
Line 17: You can't use `\prevdepth' in horizontal mode. \end{document}
Line 17: Missing number, treated as zero. \end{document}
Line 17: Illegal unit of measure (pt inserted). \end{document}
Line 17: Use of \par doesn't match its definition. \end{document}
Line 17: Improper \prevdepth. \end{document}
Line 17: You can't use `\prevdepth' in horizontal mode. \end{document}
Line 17: Use of \par doesn't match its definition. \end{document}
Line 17: Missing \endcsname inserted. \end{document}
Line 17: Missing \endcsname inserted. \end{document}
Line 17: Argument of \@gobble has an extra }. \end{document}
Line 17: Paragraph ended before \@gobble was complete. \end{document}
Line 17: Use of \par doesn't match its definition. \end{document}
Line 17: Missing } inserted. \end{document}
Line 17: Use of \par doesn't match its definition. \end{document}
Line 17: Extra \endgroup. \end{document}
: Emergency stop.

我已经知道错误来自\ifthenelse{\isempty{#1}}{#2}{#1}命令\label,但我需要做什么才能使命令正常工作?

答案1

我删除了xifthen并使用了\ifx\relax#1\relax

\documentclass{article}

\newcommand{\labeledsection}[2][]{%
    \section{#2}%
    \ifx\relax#1\relax%
        \label{#2}%
    \else%
        \label{#1}%
    \fi%
}

\begin{document}
    \labeledsection{Section 1 name}
    Section should be: 'Section 1 name'\\
    Label should be: 'Section 1 name'\\
    Referencing: \ref{Section 1 name}

    \labeledsection[Section 2 label]{Section 2 name}
    Section should be: 'Section 2 name'\\
    Label should be: 'Section 2 label'\\
    Referencing: \ref{Section 2 label}
\end{document}

在此处输入图片描述

\ifthenelse您可以按以下方式使用您的方法:

\usepackage{xifthen}
\newcommand{\labeledsection}[2][]{%
    \section{#2}%
    \ifthenelse{\isempty{#1}}{%
        \label{#2}%
    }{%
        \label{#1}%
    }%
}   

答案2

参数应该\label完全扩展为字符串,但事实\ifthenelse并非如此。

我不推荐这种方法,原因如下:

  1. 一旦你改变一个章节的标题,你就会遇到麻烦;

  2. 使用标题作为标签会导致使用不便\ref

无论如何,这是一个巧妙的方法:

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\labeledsection}{O{#2}m}{%
  \section{#2}%
  \label{#1}%
}   

\begin{document}

\labeledsection{Section 1 name}
Section should be: 'Section 1 name'\\
Label should be: 'Section 1 name'

\labeledsection[Section 2 label]{Section 2 name}
Section should be: 'Section 2 name'\\
Label should be: 'Section 2 label'

\section{References}

\ref{Section 1 name} and \ref{Section 2 label}

\end{document}

参数列表指定一个可选参数,其默认值为强制参数。

在此处输入图片描述

不过,我可以想到一种可能有用的情况。假设您正在讨论星系,并且对它们的描述分散在文档中。出于某种原因,您想通过编号交叉引用它们。如果星系的名称中有特殊字符,则可选参数很有用。

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{xparse}
\usepackage{cleveref}

\NewDocumentCommand{\galaxy}{O{#2}m}{%
  \refstepcounter{galaxy}%
  \item[(\thegalaxy) #2]%
  \label{galaxy:#1}%
}
\newcounter{galaxy}
\crefname{galaxy}{galaxy}{galaxies}

\begin{document}

\begin{description}
\galaxy{Andromeda} is a nearby galaxy

\galaxy[Ooc]{Ööç} is a distant galaxy
\end{description}

Galaxy \cref{galaxy:Andromeda} is big, while \cref{galaxy:Ooc} is small.

\begin{description}
\galaxy{Aquarius} is a dwarf galaxy
\end{description}

Can you see \cref{galaxy:Aquarius} or \cref{galaxy:Andromeda,galaxy:Ooc}?

\end{document}

在此处输入图片描述

相关内容