[Tex/LaTex] How to pass variables to \renewcommand from within another command (\DeclareRobustCommand)

macros

Situation

I need to set a few commands and variables based on what language a document is written in. To do this, I pass an ISO 639-1 language code to a command (\DeclareRobustCommand{\setlanguage}[1]. This variable is then sent to other subcommands contained by this e.g. \renewcommand{\dq}[1]. I do not want this to occur. How can I make the subcommands receive their inputs from where they are called in the document?

Problem

The problem is that the renewal of the \dq command (a subcommand of \setlanguage) receives the input of its containing command (e.g. "en") and not of the text (e.g. "black hole").

Minimal Working Example

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

Best Answer

The # sign followed by a single digit (1-9) is used to indicate an argument number to a macro. Thus, the way the OP's MWE was structured, an invocation of #1 inside the macro \setlanguage is interpreted as the first argument of the \setlanguage macro, which was specified without arguments. Thus, it throws an error.

LaTeX provides the means of "doubling" the # character (##) as the means to convey a single # through the expansion/replacement. Thus, ##1 is not perceived by \setlanguage as its first argument, but is instead replaced by #1 for the interpretation by the embedded macro \dq.

One can determine the rule for recursion here: for each layer of \def or \(re)newcommand inside of a macro definition, the number of # characters need doubling:

Layer  Argument
  1      #1
  2      ##1
  3     ####1

etc. At the end of the revised MWE, I whow an example with triple-nested arguments.

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

enter image description here

Related Question