Henning Hraban Ramm schrieb am 01.08.2020 um 20:49:
Am 01.08.2020 um 13:22 schrieb Henning Hraban Ramm
: Hi,
besides the CG journal I’m working on a scientific biography with huge person and locality indexes (named Person and Locality for the examples).
In many cases, the author wants additional information in the index, e.g.
\Locality{Altona (Hamburg)} \Locality{Breslau (pol. Wrocław)} or \Person{Arends, Katharina (née Schoemaker)} \Person{Wilhelm II. (Kaiser)}
Now, I need that consistent and I don’t want to type these complicated entries every time.
I’m looking for a good way to handle this – maybe a lookup table in Lua, so that \LookupPerson{Willy II} would call a Lua function that returns \Person{Wilhelm II. (Kaiser)} ?
Or is there already something in place that I overlooked, like \OverwriteIndexEntry{Hraban}{Ramm, Henning Hraban} ?
I came up with:
\startluacode user.Lookups = { ["Albano"] = "Albano (Provinz Rom)", ["Altona"] = "Altona (Hamburg)", ["Aurich"] = "Aurich (Ostfriesland)" }
function user.Lookup(Name) local Res = user.Lookups[Name] if Res then return context(Res) else return context(Name) end end \stopluacode
\def\Ort#1{\index{\ctxlua{user.Lookup("#1")}}}
You have to expand the \index argument: \define[1]\Ort{\expanded{\index{...}}}
\starttext
\Ort{Albano} \Ort{Altona} \Ort{Aurich} \strut\page
\placeindex
\stoptext
The lookup works so far, but all the entries get sorted unter C (because of \ctxlua).
I remember I had the same problem with other macros (like \index{\emph{bla}}), but can’t find a solution in my usual sources.
When you use formatting commands etc. you have to use the optional argument for sorting. You can avoid a few problems when you move the \index command to Lua and use context.index or you use a pure TeX solution. %%%% begin lua example \startluacode userdata = userdata or { } userdata.lookup = { ["Albano"] = "Albano (Provinz Rom)", -- ["Altona"] = "Altona (Hamburg)", ["Aurich"] = "Aurich (Ostfriesland)" } function userdata.index(name) local indexentry = userdata.lookup[name] or name context.index(indexentry) end \stopluacode \define[1]\Ort{\ctxlua{userdata.index("#1")}} \starttext \Ort{Albano} \Ort{Altona} \Ort{Aurich} \dontleavehmode\page \placeindex \stoptext %%%% end lua example %%%% begin tex example \setvariables [index] [Albano={Albano (Provinz Rom)}, %Altona={Altona (Hamburg)}, Aurich={Aurich (Ostfriesland)}] \define[1]\Ort {\doifelsevariable{index}{#1} {\expanded{\index{\getvariable{index}{#1}}}} {\index{#1}}} \starttext \Ort{Albano} \Ort{Altona} \Ort{Aurich} \dontleavehmode\page \placeindex \stoptext %%%% end tex example Wolfgang