如果您\bletter
使用\DeclareRobustCommand\bletter[1]{\textbf{#1}}
它進行定義,即使在部分命令中也可以正常運作。但是如果你想在\edef
or\xdef
命令中使用它,\bletter
將會被破壞(除非你定義了\let\protect\noexpand
)。這意味著:
\xdef\boldyletter{\bletter{y}}
會拋出錯誤訊息。
有沒有什麼命令可以定義永遠牢不可破的命令?
答案1
您可以使用
\NewDocumentCommand\bletter{m}{\textbf{#1}}
這使得 e-tex \protected
定義不會在 edef 中擴展。
但你不應該\xdef
在未知的乳膠輸入上使用,使用\protected@xdef
乳膠健壯的命令將受到保護。
答案2
只需使用\protected\def
原語來聲明巨集\bletter
:
\protected\def\bletter#1{\textbf{#1}}
在、、等期間巨集\protected
不會展開。\edef
\xdef
\write
\message
答案3
很抱歉回答我自己的問題,但我不知道如何與社區分享我的解決方案。
\def\RJDeclareRobustCommand#1% #1 name of new command, #2 same instructions as \newcommand
{%
\gdef#1%
{%
\begingroup%
\let\relax\aftergroup% this has to be redefined before processing \csname @\string#1\endcsname
\ifx\aftergroup\relax% this is false in expandations
\expandafter\aftergroup\csname RJ@\expandafter\@gobble\string#1\endcsname%
\else% \expandafter\aftergroup\expandafter\noexpand\csname\expandafter\@gobble\string#1@RJ\endcsname%
\fi%
\endgroup%
}%
\expandafter\gdef\csname\expandafter\@gobble\string#1@RJ\endcsname%
{%
\begingroup%
\let\relax\aftergroup% this has to be redefined before processing \csname @\string#1\endcsname
\ifx\aftergroup\relax% this is false in expandations
\expandafter\aftergroup\csname RJ@\expandafter\@gobble\string#1\endcsname%
\else%
\expandafter\aftergroup\expandafter\aftergroup\expandafter\aftergroup%
\expandafter\noexpand\csname\expandafter\@gobble\string#1@RJ\endcsname%
\fi%
\endgroup%
}%
% read the instructions (see \new@command)
\expandafter\new@command\csname RJ@\expandafter\@gobble\string#1\endcsname%
}%
%
\RJDeclareRobustCommand\bletter[1]{\textbf{#1}}
會很健壯。
\xdef\boldyletter{\bletter{y}}
即使在命令中也能正常運作\xdef\boldxyletter{x\boldyletter}}
。
答案4
非常感謝您提供的非常有幫助的答案。它們對我來說也是一個挑戰,我很高興地接受了。
為了在章節標題中使用新命令,除其他外,有必要在 \pdfstringdef 中新增和重新定義該命令。我的舊程式碼中沒有考慮到這一點。當添加這個需求時,我還能夠優化程式碼。
下面是使用新程式碼的測試範例。書籤中將忽略新指令,而不是其參數。
\documentclass[a4paper,12pt]{report}
\RequirePackage[pdftex,colorlinks,linkcolor=blue,%
urlcolor=blue,bookmarksnumbered]{hyperref}
%
\makeatletter
%
\let\RJ@aftergroup\aftergroup%
\let\RJ@begingroup\begingroup%
\let\RJ@endgroup\endgroup%
\let\RJ@let\let%
%
\@ifundefined{pdfstringdefPreHook}{}%
{%
\gdef\Addpdfstringdef#1%
{%
\global\@temptokena\expandafter%
{\pdfstringdefPreHook#1}%
\xdef\pdfstringdefPreHook{\the\@temptokena}%
}%
%
\Addpdfstringdef%
{%
\let\RJ@aftergroup\@empty%
\let\RJ@begingroup\@empty%
\let\RJ@endgroup\@empty%
\let\RJ@let\@empty%%
}%
}%
%
\def\RJ@DRCentry#1%
{%
\RJ@begingroup%
\RJ@let\relax\RJ@aftergroup%
\ifx\RJ@aftergroup\relax%
\expandafter\RJ@aftergroup\csname RJ@\expandafter\@gobble\string#1\endcsname%
\else%
\expandafter\noexpand\csname RJ\expandafter\@gobble\string#1JR\endcsname%
\fi%
\RJ@endgroup%
}%
%
\def\RJ@DeclareRobustCommand#1% #1 name of new command, #2 same instructions as \newcommand
{%
\gdef#1%
{%
\ifx\RJ@aftergroup\@empty%
\else%
\expandafter\RJ@DRCentry\expandafter#1%
\fi%
}%
%
\expandafter\gdef\csname RJ\expandafter\@gobble\string#1JR\endcsname%
{%
\ifx\RJ@aftergroup\relax%
\gdef\next{\RJ@DRCentry{#1}}%
\RJ@aftergroup\next%
\else%
\expandafter\noexpand\csname RJ\expandafter\@gobble\string#1JR\endcsname%
\fi%
}%
%
\@ifundefined{pdfstringdefPreHook}{}%
{%
\expandafter\Addpdfstringdef\expandafter%
{\expandafter\let\csname RJ\expandafter\@gobble\string#1JR\endcsname\@empty}%
}%
\expandafter\new@command\csname RJ@\expandafter\@gobble\string#1\endcsname%
}%
%
\RJ@DeclareRobustCommand\iletter[1]{\textit{\huge #1}}%
%
\xdef\yiletter{\iletter{y}}%
\xdef\xyiletter{\iletter{x}\yiletter}%
\xdef\xyziletter{\xyiletter\iletter{z}}%
\xdef\jiletter{\iletter{j\iletter{i}}}%
\begin{document}
\tableofcontents
\newpage
test \iletter{j\iletter{i}}, \jiletter{} test \iletter{a}, \yiletter, \xyiletter, \xyziletter{} test
\section{italic \iletter{y}, \xyziletter}
The following chapter heading is for comparison purposes only .
\section{italic \textit{\huge y}, \textit{\huge xyz}}
more text
\end{document}