
我正在寫一篇論文,但我還沒有做出所有的文體選擇。也就是說,我還沒有決定要縮寫哪些長單字以及如何縮寫它們,如果論文的作者是「我」、「我們」、「作者」或「作者」等所以我想做的是定義一些會被多次重複使用的關鍵字。
我想在文件的頂部做一些類似的事情\newcommand{\auth}{we}
,每當我\auth{}
在文件中寫入時,「我們」這個字就應該出現。現在,經驗豐富的人會注意到,我在這裡描述的內容實際上會按照我想要的方式進行,等等,直到真正的問題。
我真正想做的是將英語詞典中的單字連結到命令,並且在將參數傳遞給該命令時能夠稍微更改其輸出。例如,如果我定義\auth{}
吐出“我們”,我想\auth{upper}
吐出“我們”。我還希望能夠透過呼叫 或類似的方式從中獲得所有格形式,例如“我們的”和“我們的\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
。重要的:這些條目應該按照說明符數量遞增的順序出現(即,您應該首先擁有所有單字條目,然後是具有兩個逗號分隔單字的條目,然後是三個,依此類推),否則您可能會得到不正確的結果。
\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
列印標記清單。
*雖然它假設是非外部的
\newif
,就像在 LaTeX 中一樣,但對於純 TeX 來說,一些調整是必要的。