
我正在使用 glosaries-extra 套件作為我的符號列表。由於某種與此處無關的原因,我定義了一個新命令
\newcommand{\symb}[3][]{%
\glsxtrnewsymbol[#1]{#2}{#3}%
}
這樣我的詞彙表條目的形式如下:
\symb[description={Set of Smooth Vector Fields}]{vfs}{\ensuremath{\mathfrak{X}}}
我想將這些「轉移」到我大學的論文模板,其中他們不使用詞彙表包,而是定義一個應輸入符號的環境:
\begin{symbols}
\sym{\ensuremath{\mathfrak{X}}}{Set of Smooth Vector Fields}
\end{symbols}
這只是一個選項卡環境,它直接列印您在命令 \sym 的參數中寫下的任何內容。所以我的計畫是定義類似的東西
\renewcommand{\symb}[3][]{%
\glsxtrnewsymbol[#1]{#2}{#3}%
\sym{#3}{\glsdesc{#2}}%
}
除了使用詞彙表包會以某種方式與大學樣式文件的另一部分混淆之外,這是有效的。因此我想定義
\renewcommand{\symb}[3][]{%
\sym{#3}{#1}%
}
但是,我不想為第二個參數提供“description={平滑向量場集}”,我想要“平滑向量場集”。有沒有辦法從 \symb 的第二個參數中僅提取「平滑向量場集」部分?
解決方案:@Schrödinger 的貓解決方案的簡化版本對我有用:
\newcommand{\symb}[3][]{%
\def\mysplit##1=##2{\sym{#3}{##2}}%
\expandafter\mysplit#1%
}
答案1
使用以下程式碼,重新定義的例程的\symb
行為如下:
\glsxtrnewsymbol
參數按原樣傳遞給原樣。
\sym
從 的可選參數中提取描述後,參數將傳遞給\symb
,如下所示:
如果該參數不包含前導短語“description=”,則按原樣傳遞。 (考慮整個短語周圍的空格標記和/或“描述”和“=”之間的空格標記。)
如果該參數確實包含前導短語“description=”,則該短語將被刪除。
- 如果剩餘部分僅包含空格令牌或不包含任何內容,則提取結果將為空/完全不包含任何令牌。
- 如果餘數由單一非分隔參數組成,即單個非大括號標記或嵌套在大括號中的一組標記,則圍繞整個餘數的一層大括號將被刪除(如果存在),然後是空格- 圍繞整個剩餘部分的令牌將被刪除。
- 如果餘數包含單一非分隔參數以外的其他內容,則不會刪除大括號,但會刪除整個餘數周圍的空格。
由於\sym
和\glsxtrnewsymbol
不適用於我,我提供了“虛擬定義”,它們什麼也不做,只是以去標記化的方式傳遞它們的論點,嵌套在尖括號中。
這一切都是我即興完成的,所以沒有任何保證。 ;-)
另請注意我對您的非重新定義\symb
例程的評論以及關於在花括號中嵌套任意材料{
以及}
將其傳遞到另一個宏時形成可選參數的評論。
整個「機制」是在不使用任何東西的情況下實現的\if..
… \else
。\fi
因此,該機制不會被包含不匹配的\if
or\else
或 的宏觀參數所混淆\fi
。
\documentclass{article}
\makeatletter
%%=============================================================================
%% Check whether argument is empty:
%%=============================================================================
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%%
%% Due to \romannumeral0-expansion the result is delivered after two
%% expansion-steps/after two "hits" by \expandafter.
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\@firstoftwo\expandafter{} \@secondoftwo}%
{\@firstoftwo\expandafter{} \@firstoftwo}%
}%
%%=============================================================================
%% Check whether argument is blank (empty or only spaces):
%%=============================================================================
%% -- Take advantage of the fact that TeX discards space tokens when
%% "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that
%% argument which is to be checked is blank>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not blank}%
\newcommand\UD@CheckWhetherBlank[1]{%
\romannumeral\expandafter\expandafter\expandafter\@secondoftwo
\expandafter\UD@CheckWhetherNull\expandafter{\@firstoftwo#1{}.}%
}%
%%=============================================================================
%% Exchange two arguments. (From each argument an outermost level of
%% surrounding braces will be removed if present.)
%%=============================================================================
\newcommand\UD@Exchange[2]{#2#1}%
%%=============================================================================
%% Check whether argument's leading tokens form a specific
%% token-sequence that does not contain explicit character tokens of
%% category code 1 or 2:
%%=============================================================================
%% \UD@CheckWhetherLeadingTokens{<argument which is to be checked>}%
%% {<a <token sequence> without explicit
%% character tokens of category code
%% 1 or 2>}%
%% {a <single non-space token> that does
%% _not_ occur in <token sequence> >}%
%% {<internal token-check-macro>}%
%% {<tokens to be delivered in case
%% <argument which is to be checked> has
%% <token sequence> as leading tokens>}%
%% {<tokens to be delivered in case
%% <argument which is to be checked>
%% does not have <token sequence> as
%% leading tokens>}%
\newcommand\UD@CheckWhetherLeadingTokens[4]{%
\romannumeral0\UD@CheckWhetherNull{#1}%
{\UD@Exchange{ }\expandafter\@secondoftwo}%
{\expandafter\@secondoftwo\string{\expandafter
\UD@@CheckWhetherLeadingTokens#4#3#1#2}{}}%
}%
\newcommand\UD@@CheckWhetherLeadingTokens[1]{%
\expandafter\UD@CheckWhetherNull\expandafter{\@firstoftwo{}#1}%
{\UD@Exchange{\@firstoftwo}}{\UD@Exchange{\@secondoftwo}}%
{\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter}\expandafter\expandafter
\expandafter}\expandafter\@secondoftwo\expandafter{\string}%
}%
%%=============================================================================
%% \UD@internaltokencheckdefiner{<internal token-check-macro>}%
%% {<token sequence>}%
%% Defines <internal token-check-macro> to snap everything
%% until reaching <token sequence>-sequence and spit that out
%% nested in braces.
%%=============================================================================
\newcommand\UD@internaltokencheckdefiner[2]{%
\@ifdefinable#1{\long\def#1##1#2{{##1}}}%
}%
%%=============================================================================
\UD@internaltokencheckdefiner{\UD@ExtractDescriptionEqual}{description=}%
\UD@internaltokencheckdefiner{\UD@ExtractDescriptionSpaceEqual}{description =}%
\UD@internaltokencheckdefiner{\UD@ExtractDescriptionEqualSpace}{description= }%
\UD@internaltokencheckdefiner{\UD@ExtractDescriptionSpaceEqualSpace}{description = }%
\UD@internaltokencheckdefiner{\UD@ExtractSpaceDescriptionEqual}{ description=}%
\UD@internaltokencheckdefiner{\UD@ExtractSpaceDescriptionSpaceEqual}{ description =}%
\UD@internaltokencheckdefiner{\UD@ExtractSpaceDescriptionEqualSpace}{ description= }%
\UD@internaltokencheckdefiner{\UD@ExtractSpaceDescriptionSpaceEqualSpace}{ description = }%
\UD@internaltokencheckdefiner{\UD@ExtractSpace}{ }%
%%=============================================================================
%% Trim all leading and trailing spaces:
%%=============================================================================
\newcommand\UD@RemoveSpaces[1]{%
\romannumeral0\@firstofone{\UD@TrimTrailSpaceLoop{#1}.#1\UD@Bizarre} \UD@Bizarre\relax\UD@Bizarre
}%
\@ifdefinable\UD@TrimTrailSpaceLoop{%
\long\def\UD@TrimTrailSpaceLoop#1#2 \UD@Bizarre#3\relax\UD@Bizarre{%
\UD@CheckWhetherNull{#3}{%
\UD@TrimLeadSpaceLoop{#1}%
}{%
\@firstofone{\expandafter\UD@TrimTrailSpaceLoop\expandafter{\@gobble#2}#2\UD@Bizarre} \UD@Bizarre\relax\UD@Bizarre
}%
}%
}%
\@ifdefinable\UD@gobblespace{%
\@firstofone{\def\UD@gobblespace} {}%
}%
\newcommand\UD@TrimLeadSpaceLoop[1]{%
\UD@CheckWhetherLeadingTokens{#1}{ }{.}{\UD@ExtractSpace}{\expandafter\UD@TrimLeadSpaceLoop\expandafter{\UD@gobblespace#1}}{ #1}%
}%
%%=============================================================================
%% Extract description:
%%=============================================================================
\newcommand\UD@ExtractDescription[1]{%
\romannumeral0%
\UD@CheckWhetherLeadingTokens{#1}{ description = }{.}{\UD@ExtractSpaceDescriptionSpaceEqualSpace}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractSpaceDescriptionSpaceEqualSpace#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{ description= }{.}{\UD@ExtractSpaceDescriptionEqualSpace}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractSpaceDescriptionEqualSpace#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{ description =}{.}{\UD@ExtractSpaceDescriptionSpaceEqual}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractSpaceDescriptionSpaceEqual#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{ description=}{.}{\UD@ExtractSpaceDescriptionEqual}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractSpaceDescriptionEqual#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{description = }{.}{\UD@ExtractDescriptionSpaceEqualSpace}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractDescriptionSpaceEqualSpace#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{description= }{.}{\UD@ExtractDescriptionEqualSpace}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractDescriptionEqualSpace#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{description =}{.}{\UD@ExtractDescriptionSpaceEqual}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractDescriptionSpaceEqual#1}%
}{%
\UD@CheckWhetherLeadingTokens{#1}{description=}{.}{\UD@ExtractDescriptionEqual}{%
\expandafter\UD@ExtractDescriptionCheckArgAmount\expandafter{\UD@ExtractDescriptionEqual#1}%
}{ #1}%
}%
}%
}%
}%
}%
}%
}%
}%
\newcommand\UD@ExtractDescriptionCheckArgAmount[1]{%
\expandafter\UD@CheckWhetherBlank\expandafter{\@gobble#1}{ }{%
\expandafter\UD@CheckWhetherBlank\expandafter{\@gobbletwo#1}{%
\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter}%
\expandafter\UD@RemoveSpaces\expandafter{\@secondoftwo#1}%
}{%
\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter}%
\expandafter\UD@RemoveSpaces\expandafter{\@firstoftwo{}#1}%
}%
}%
}%
%%=============================================================================
%% Your definition of \symb with a modification:
%% When passing arbitrary arguments as optional arguments, I strongly recommend
%% nesting them in braces for ensuring that nesting of square brackets won't
%% lead to problems:
%%=============================================================================
\newcommand{\symb}[3][]{%
\glsxtrnewsymbol[{#1}]{#2}{#3}% <- Here the content of #1 is arbitrary.
}% % Problems due to nested square brackets
% might occur in case #1 contains
% [ or ].
% Some day someone might do something like
% \symb[{description=optional\macro[macro's optional]}]{...}{...}.
% Therefore nest #1 in braces.
% These braces will be removed by LaTeX
% when processing \glsxtrnewsymbol's
% optional argument.
% They prevent confusion when it comes
% to nesting optional arguments within
% optional arguments.
%%=============================================================================
%% Redefinition of \symb:
%%=============================================================================
\renewcommand{\symb}[3][]{%
\romannumeral0%
\expandafter\expandafter\expandafter\UD@Exchange
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter{\UD@ExtractDescription{#1}}}{ \sym{#3}}%
\glsxtrnewsymbol[{#1}]{#2}{#3}%
}%
%%=============================================================================
%% Dummy-definition for \glsxtrnewsymbol which displays the arguments verbatim
%%=============================================================================
\newcommand\glsxtrnewsymbol[3][]{%
\par\noindent
\texttt{\string\glsxtrnewsymbol}'s optional argument: $\langle$\texttt{\detokenize{#1}}$\rangle$
\par\noindent
\texttt{\string\glsxtrnewsymbol}'s mandatory argument 1: $\langle$\texttt{\detokenize{#2}}$\rangle$
\par\noindent
\texttt{\string\glsxtrnewsymbol}'s mandatory argument 2: $\langle$\texttt{\detokenize{#3}}$\rangle$
\par
}%
%%=============================================================================
%% Dummy-definition for \sym which displaysthe arguments verbatim
%%=============================================================================
\newcommand\sym[2]{%
\par\noindent
\texttt{\string\sym}'s mandatory argument 1: $\langle$\texttt{\detokenize{#1}}$\rangle$
\par\noindent
\texttt{\string\sym}'s mandatory argument 2: $\langle$\texttt{\detokenize{#2}}$\rangle$
\par
}%
\makeatother
\begin{document}
\vspace*{-1in}%
\noindent
\texttt{\detokenize{\symb[description={Set of Smooth Vector Fields}]{vfs}{\ensuremath{\mathfrak{X}}}}}:
\smallskip
\symb[description={Set of Smooth Vector Fields}]{vfs}{\ensuremath{\mathfrak{X}}}
\bigskip
\noindent\null\hrulefill\null
\bigskip
\noindent
\texttt{\detokenize{\symb[ description = { Set of Smooth Vector Fields } ]{vfs}{\ensuremath{\mathfrak{X}}}}}:
\smallskip
\symb[ description = { Set of Smooth Vector Fields } ]{vfs}{\ensuremath{\mathfrak{X}}}
\bigskip
\noindent\null\hrulefill\null
\bigskip
\noindent
\texttt{\detokenize{\symb[description = Set of Smooth Vector Fields ]{vfs}{\ensuremath{\mathfrak{X}}}}}:
\smallskip
\symb[description = Set of Smooth Vector Fields ]{vfs}{\ensuremath{\mathfrak{X}}}
\bigskip
\noindent\null\hrulefill\null
\bigskip
\noindent
\texttt{\detokenize{\symb{vfs}{\ensuremath{\mathfrak{X}}}}}:
\smallskip
\symb{vfs}{\ensuremath{\mathfrak{X}}}
\bigskip
\noindent\null\hrulefill\null
\bigskip
\noindent
\texttt{\detokenize{\symb[whatsoever]{vfs}{\ensuremath{\mathfrak{X}}}}}:
\smallskip
\symb[whatsoever]{vfs}{\ensuremath{\mathfrak{X}}}
\end{document}
答案2
這就是這樣做的。顯然我沒有你的\sym
命令,所以我使用了一些東西來表明它的參數是正確的。
\documentclass{article}
\usepackage{amsfonts}
\def\forgetit{}
\newcommand{\symb}[3][]{%
\glsxtrnewsymbol[#1]{#2}{#3}%
}
\newcommand{\sym}[2]{first argument=\ensuremath{#1},second argument=#2}
\renewcommand{\symb}[3][\forgetit]{%
\def\mysplit##1=##2;{\def\mylast{##2}}%
\ifx#1\forgetit
\sym{#3}{empty}%
\else
\expandafter\mysplit#1;%
\sym{#3}{\mylast}%
\fi
}
\begin{document}
\symb[description={Set of Smooth Vector Fields}]{vfs}{\ensuremath{\mathfrak{X}}}
\symb{vfs}{\ensuremath{\mathfrak{X}}}
\end{document}
這假設只要第一個參數非空,它就包含一個=
符號。你可以讓它免受這種影響,但我會建議使用一些密鑰管理系統。例如,如果您已經加載,pgf
那麼就會非常簡單。