![情况](https://linux22.com/image/307923/%E6%83%85%E5%86%B5.png)
情况
我需要根据文档所用的语言设置一些命令和变量。为此,我将 ISO 639-1 语言代码传递给命令(\DeclareRobustCommand{\setlanguage}[1]
。然后将此变量发送给由此包含的其他子命令,例如\renewcommand{\dq}[1]
。我不希望发生这种情况。如何让子命令从文档中调用它们的位置接收输入?
问题
问题在于,\dq
命令(的子命令\setlanguage
)的更新接收其包含命令(例如“en”)的输入,而不是文本(例如“black hole”)的输入。
最小工作示例
\documentclass{article}
\usepackage{fontspec}
\usepackage{xstring}
\newcommand{\dq}[1]{\char"201C{}#1\char"201D{}} % Sets default double quotes to English (overridden with \renewcommand{\dq}[1])
\newcommand{\sq}[1]{\char"2018{}#1\char"2019{}} % Sets default single quotes in English (overridden with \renewcommand{\sq}[1])
\DeclareRobustCommand{\setlanguage}[1]{%
% \IfEq{#1}{en}{%
\protect\renewcommand{\dq}[1]{\char"201C{}#1\char"201D{}} % Set double quotes to standard in respective language
\protect\renewcommand{\sq}[1]{\char"2018{}#1\char"2019{}} % Set single quotes to standard in respective language
% }{}%
}%
\begin{document}
\setlanguage{en}
Although the term \dq{black hole} was not coined until 1967 by Princeton physicist John Wheeler, the idea of an object in space so massive and dense that light could not escape it has been around for centuries. Most famously, black holes were predicted by Einstein's theory of general relativity, which showed that when a massive star dies, it leaves behind a small, dense remnant core. If the core's mass is more than about three times the mass of the Sun, the equations showed, the force of gravity overwhelms all other forces and produces a black hole.
\end{document}
答案1
符号#
后跟一个数字(1-9)用于指示宏的参数编号。因此,根据 OP 的 MWE 结构,#1
宏内部的调用\setlanguage
被解释为宏的第一个参数\setlanguage
,该参数没有指定参数。因此,它会抛出一个错误。
#
LaTeX 提供了“双倍”字符 ( )的方法,作为通过扩展/替换##
传达单个字符的方法。因此,不会被视为其第一个参数,而是被替换为嵌入宏的解释。#
##1
\setlanguage
#1
\dq
这里可以确定递归的规则:对于宏定义的每一层\def
或\(re)newcommand
内部,字符数#
需要加倍:
Layer Argument
1 #1
2 ##1
3 ####1
等等。在修订后的 MWE 的最后,我展示了一个具有三重嵌套参数的例子。
\documentclass{article}
\usepackage{fontspec}
\usepackage{xstring}
\newcommand{\dq}[1]{\char"201C{}#1\char"201D{}} % Sets default double quotes to English (overridden with \renewcommand{\dq}[1])
\newcommand{\sq}[1]{\char"2018{}#1\char"2019{}} % Sets default single quotes in English (overridden with \renewcommand{\sq}[1])
\DeclareRobustCommand{\setlanguage}[1]{%
% \IfEq{#1}{en}{%
\protect\renewcommand{\dq}[1]{\char"201C{}##1\char"201D{}} % Set double quotes to standard in respective language
\protect\renewcommand{\sq}[1]{\char"2018{}##1\char"2019{}} % Set single quotes to standard in respective language
% }{}%
}%
\begin{document}
\setlanguage{en}
Although the term \dq{black hole} was not coined until 1967 by Princeton physicist John Wheeler, the idea of an object in space so massive and dense that light could not escape it has been around for centuries. Most famously, black holes were predicted by Einstein's theory of general relativity, which showed that when a massive star dies, it leaves behind a small, dense remnant core. If the core's mass is more than about three times the mass of the Sun, the equations showed, the force of gravity overwhelms all other forces and produces a black hole.
\def\first{F\def\second##1{(S##1)\def\third####1{[T####1]}}}
\first, \second{x}, \third{y}
\end{document}