我有这个命令,它是更大命令的一部分
\newcommand{\customParagraph}[2][1]{% Title, Name
\ifthenelse{\isempty{#1}}
{\def \customParTitle{}}
{\edef\customParTitle{ -- #1}}
\edef\customParTitle{#2\customParTitle} % This can be used by other commands to populate ToC
\subparagraph*{\customParTitle}
}
在我的文档中,我将其称为\customParagraph{Proof}
或\customParagraph[Proof of something]{Proof}
来获取我的子段落。如果我在第一个参数中放入一些内联数学运算,那就没问题,但是只要我在其中放入命令,例如\customParagraph[Proof of something in $\R$]{Proof}
,其中\R
定义为\newcommand*{\R}{\mathbb{R}}
,我就会得到一个未定义控制序列相应行处出现错误。\mathbb{R}
直接输入会得到相同的结果。
我该如何修复此命令以获得预期结果?请记住,的定义\customParTitle
可以按如下方式使用
\addcontentsline{toc}{...}{\customParTitle}
答案1
由于没有完整的 MWE,我猜测这是由的多次扩展引起的\customParTitle
,因此尝试将定义更改为以下内容(这样,在的定义期间参数就不会扩展\customParTitle
):
\newcommand{\customParagraph}[2][1]{% Title, Name
\ifthenelse{\isempty{#1}}
{\def\customParTitle{}}
{\def\customParTitle{ -- #1}}%
\edef\customParTitle{\unexpanded{#2}\unexpanded\expandafter{\customParTitle}}% This can be used by other commands to populate ToC
\subparagraph*{\customParTitle}%
}
嵌入自创的 MWE:
\documentclass[]{article}
\usepackage{amssymb}
\newcommand*\R{\mathbb{R}}
\newcommand\customParagraph[2][1]
{% Title, Name
\if\relax\detokenize{#1}\relax
\def\customParTitle{}%
\else
\def\customParTitle{ -- #1}%
\fi
\edef\customParTitle
{\unexpanded{#2}\unexpanded\expandafter{\customParTitle}}%
\subparagraph*{\customParTitle}%
}
\newcommand\YourCustomParagraph[2][1]
{% Title, Name
\if\relax\detokenize{#1}\relax
\def \customParTitle{}%
\else
\edef\customParTitle{ -- #1}%
\fi
\edef\customParTitle{#2\customParTitle}%
\subparagraph*{\customParTitle}%
}
\begin{document}
\customParagraph[Proof of something in $\R$]{Proof}%
%\YourCustomParagraph[Proof of something in $\R$]{Proof}% doesn't work
\end{document}