
私はまだ文体の選択をすべて行っていない論文を書いています。つまり、どの長い単語を省略するか、また、論文の著者が「私」、「私たち」、「著者」、あるいは「著者ら」などの場合にどのように省略するかを決めていません。そこで、何度も再利用されるキーワードをいくつか定義したいと思います。
ドキュメントの先頭で、次のようなことをしたいのです\newcommand{\auth}{we}
が、ドキュメントに書き込むたびに\auth{}
「私たち」という単語が表示される必要があります。経験豊富な方は、ここで説明していることが実際に私の希望どおりに機能することに気付くでしょう。それでは、実際の質問に移りましょう。
私が本当にやりたいのは、英語の辞書の単語をコマンドにリンクし、そのコマンドに引数を渡すときに出力を少し変更できるようにすることです。たとえば、\auth{}
「we」を出力するように定義した場合\auth{upper}
、代わりに「We」を出力したいのです。また、 などを呼び出すことで、「Ours」や「ours」などの所有格を取得できるようにしたいと思います。これを実現する最善の方法は何でしょうか。辞書のようなものを作成できますか\auth{upper, possessive}
、\auth{possessive}
それとも何かお勧めはありますか。
どうもありがとう!
答え1
まあ、試してみました。ちなみに、これは LuaLaTeX でのみ動作します:
%!TEX program = lualatex
\documentclass{article}
\usepackage{polyglossia}
\usepackage{luacode}
\begin{luacode*}
dofile(kpse.find_file("l-lpeg.lua"))
dofile(kpse.find_file("util-sto.lua"))
dofile(kpse.find_file("util-prs.lua"))
Pronouns = {
["formal"] = {
["possessive"] = "ours",
["nominative"] = "we",
["oblique"] = "us"
},
["informal"] = {
["possessive"] = "mine",
["nominative"] = "I",
["oblique"] = "me"
}
}
function Auth(keywords)
local dummy = utilities.parsers.settings_to_array(keywords)
for i,v in ipairs(dummy) do
if Pronouns[dummy[i]] ~= nil then result = Pronouns[dummy[i]] end
end
for i,v in ipairs(dummy) do
if result[dummy[i]] ~= nil then result = result[dummy[i]] end
end
return result
end
function Upper(string)
return unicode.utf8.upper(unicode.utf8.sub(string,1,1))..unicode.utf8.sub(string,2,utf8.len(string))
end
\end{luacode*}
\def\auth#1{\directlua{tex.print(Auth("#1"))}}
\def\Auth#1{\directlua{tex.print(Upper(Auth("#1")))}}
\begin{document}
\auth{oblique,informal} \Auth{formal,possessive}
\end{document}
答え2
これは驚くほど複雑でしたが、TeX*で動作するソリューションがあります。
\begingroup
\catcode`!=11 % for private macros
\endlinechar=-1
\catcode`\^^M=12 \catcode`\^^?=12\relax
\gdef\!stop{^^?}
\gdef\!fi{\fi}
\newtoks\!before \newtoks\!after
% some convenient shorthands
\gdef\!csnm#1{\csname#1\endcsname}
\gdef\!ecsnm#1#2{\expandafter#1\csname#2\endcsname}
% the main command sets up the catcodes for reading in
% the definitions of the second argument
\gdef\newvarcommand#1{
\begingroup\catcode`\^^M=12 \catcode`\^^?=12\relax
{\escapechar=-1 \xdef\!name{\string#1}}
\!ecsnm\newtoks{toks:\!name}
\gdef#1##1{
{\escapechar=-1 \xdef\!name{\string#1}}
\begingroup
\!processnextkey##1,^^?,
\!ecsnm\the{toks:\!name}
\!result
\endgroup
}
\!newvarcommand
}
% for each modifier in the argument, set the corresponding
% conditional true
\gdef\!processnextkey#1,{\def\arg{#1}
\ifx\!stop\arg\else
\ifx\empty\arg\def\!key{default}\else\def\!key{#1}\fi
\!ecsnm\ifx{ifkey:\!name:\!key}\relax
\errmessage{Unknown key \!key\space for command \!name}
\else\!csnm{key:\!name:\!key true}\fi
\expandafter\!processnextkey\fi
}
% here we read the argument line by line
\gdef\!newvarcommand#1{\!getnext#1^^M^^?^^M}
\gdef\!getnext#1^^M{\def\arg{#1}
\ifx\!stop\arg\endgroup\else
\ifx\empty\arg\else\!parse#1^^M\fi
\expandafter\!getnext\fi
}
% for each entry, new conditionals are created to test whether a
% modifier is present or not, and a token list containing the
% conditionals and the word to be printed is appended to the
% token list read by the command to be defined
\gdef\!parse#1:#2^^M{\!before={}\!after={}\def\arg{#1}
\ifx\empty\arg
{\globaldefs=1 \!ecsnm\newif{ifkey:\!name:default}}
\!before=\expandafter{\csname ifkey:\!name:default\endcsname}
\!after=\expandafter{\!fi}
\else\!setupnextkey#1,^^?,\fi
\edef\!addtoks{\!ecsnm\the{toks:\!name}
\the\!before\def\noexpand\!result{#2}\the\!after}
\global\!csnm{toks:\!name}=\expandafter{\!addtoks}
}
% creating \newifs for each modifier
\gdef\!setupnextkey#1,{\def\arg{#1}
\ifx\!stop\arg\else
{\globaldefs=1 \!ecsnm\newif{ifkey:\!name:#1}}
\!before=\expandafter{\the\expandafter\expandafter
\expandafter\!before\!csnm{ifkey:\!name:#1}}
\!after=\expandafter{\the\expandafter\!after\!fi}
\expandafter\!setupnextkey\fi
}
\endgroup
次に、「辞書」を次のように定義します。
\newvarcommand\auth{
:we
upper:We
possessive:ours
upper,possessive:Ours
}
各行には、引数として使用する単語をコンマで区切って記述し、コロンの後に表示される単語を記述します。デフォルト値は または のいずれかで指定でき:val
ますdefault:val
。重要:エントリは指定子の数が増える順に出現する必要があります (つまり、最初に 1 単語のエントリをすべて出現させ、次にコンマで区切られた 2 つの単語のエントリ、その次に 3 つの単語のエントリ、というように出現させる必要があります)。そうしないと、誤った結果が返される可能性があります。
\auth{} \auth{default} % these two are the same, "we"
\auth{upper} % "We"
\auth{possessive} % "ours"
\auth{upper,possessive} \auth{possessive,upper} % both "Ours"
\auth{lower} % this gives an "Unknown key" error
行のコードがどのように動作するかを説明しますupper,possessive:Ours
。基本的に行が読み込まれると、まず条件文が作成されます\ifkey:auth:upper
。\ifkey:auth:possessive
次にトークンリストが作成されます。
\ifkey:auth:upper
\ifkey:auth:possessive
\def\!result{Ours}
\fi
\fi
が構築され、 という名前のトークン リストに追加されます\toks:auth
。 を使用すると\auth{upper,possessive}
、引数が読み取られ、対応する条件\ifkey:auth:upper
とがifkey:auth:possessive
true に設定され、toks:auth
トークン リストが展開されて が\!result
印刷されます。
*ただし、LaTeX のように非外部を前提としています
\newif
が、プレーン TeX の場合はいくつかの適応が必要になります。