我正在寻找一个可以自动创建斜体物种名称的宏。我发现这个很好的答案。不幸的是,我必须在每个文档部分中写入完整形式。我必须向此宏添加什么内容才能在每个部分中第一次调用它时写入全名?
\documentclass{article}
\usepackage{xspace}
\newcommand{\species}[3]{%
\newcommand{#1}{\gdef#1{\textit{#3}\xspace}\textit{#2}\xspace}}
\species{\ecoli}{Escherichia coli}{E.~coli}
%%
\begin{document}
\section{Section 1}
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli.\par
\section{Section 2}
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli.
\end{document}
答案1
我的答案的修订版本https://tex.stackexchange.com/a/71094/4427
以下代码允许重置长格式。该命令\resetspecies
可以在任何地方调用;如果没有给出可选参数,则所有物种都将被重置,否则仅重置以下示例中指定的物种。
使用\xpreto{\section}{\resetspecies}
,所有物种将在新的部分重置(\chapter
如果要在章节中重置则使用)。
与以前版本的显著区别是,宏名不应带有反斜杠(为了便于实现)。
\documentclass{article}
\usepackage{xspace}
\usepackage{xparse,xpatch}
\ExplSyntaxOn
%% Meta-Command for defining new species macros
\NewDocumentCommand{\species}{mmm}
{
\daniel_species_define:nnn { #1 } { #2 } { #3 }
}
\prop_new:N \g_daniel_species_names_prop
\prop_new:N \g_daniel_species_used_prop
\prop_new:N \l__daniel_species_temp_prop
\cs_new_protected:Nn \daniel_species_define:nnn
{% save the long and short forms in a property list
\prop_gput:Nnn \g_daniel_species_names_prop { long#1 } { #2 }
\prop_gput:Nnn \g_daniel_species_names_prop { short#1 } { #3 }
% the long form is initially chosen
\prop_gput:Nnn \g_daniel_species_used_prop { #1 } { 0 }
\cs_new_protected:cpn { #1 }
{% if the value in the "used" property list is 0 use the long form
\int_compare:nNnTF { \prop_item:Nn \g_daniel_species_used_prop { #1 } } = { 0 }
{
\textit { \prop_item:Nn \g_daniel_species_names_prop { long#1 } }
}
{
\textit { \prop_item:Nn \g_daniel_species_names_prop { short#1 } }
}
% mark the species as used
\prop_gput:Nnn \g_daniel_species_used_prop { #1 } { 1 }
\xspace
}
}
\NewDocumentCommand{\resetspecies}{ o }
{
\IfNoValueTF { #1 }
{% reset all species
\prop_clear:N \l__daniel_species_temp_prop
\prop_map_inline:Nn \g_daniel_species_used_prop
{
\prop_put:Nnn \l__daniel_species_temp_prop { ##1 } { 0 }
}
\prop_gset_eq:NN \g_daniel_species_used_prop \l__daniel_species_temp_prop
}
{% reset only the specified ones
\clist_map_inline:nn { #1 }
{
\prop_gput:Nnn \g_daniel_species_used_prop { ##1 } { 0 }
}
}
}
\ExplSyntaxOff
\xpreto{\section}{\resetspecies}
%% Defining new species
% The first argument is the name of the macro you will call in the document.
% The second argument is what is written the first time the macro is called
% The third argument is what is written every subsequent time the macro is called.
\species{ecoli}{Escherichia coli}{E.~coli}
\species{rsphaeroides}{Rhodobacter sphaeroides}{R.~sphaeroides}
\species{abrasilense}{Azospirillum brasilense}{A.~brasilense}
\species{celegans}{Caenorhabditis elegans}{C.~elegans}
\species{pseudomonads}{Pseudomonads}{Pseudomonads}
%%
\begin{document}
\section{First}
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli. Also \abrasilense.
\resetspecies
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli. Also \abrasilense and \celegans.
\resetspecies[ecoli,celegans]
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli. Also \abrasilense and \celegans.
\section{Second}
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli. Also \abrasilense and \celegans.
\end{document}
答案2
以下是供您实施的一个选项:
\documentclass{article}
\usepackage{xspace,etoolbox}
\newcommand{\setspecies}[4]{%
#1{#2}{\renewcommand{#2}{\textit{#4}\xspace}\textit{#3}\xspace}}%
\newcommand{\species}[3]{%
\setspecies{\newcommand}{#1}{#2}{#3}%
\pretocmd{\section}{\setspecies{\renewcommand}{#1}{#2}{#3}}{}{}}
\species{\ecoli}{Escherichia coli}{E.~coli}
\begin{document}
\section{First section}
\ecoli is an example of a model species. People study \ecoli because people have studied \ecoli.
\section{Second section}
\ecoli is an example of a model species. People study \ecoli because people have studied \ecoli.
\end{document}
\setspecies
可以调用宏来关于定义如何设置物种。它在调用时定义它\species
,并且关于用 every 来定义它\section
。