Einführung
Ich versuche, eine Vorlage zu erstellen, die das Eintippen von Aufgabenstellungen und Prüfungen für die Universität, an der ich arbeite, erleichtert. In diesem Zusammenhang ist es oft notwendig, die Kontaktdaten der Fakultätsmitglieder aufzuschreiben.
Was ich suche ist einbenutzerfreundlichMöglichkeit, Personen in ein „Wörterbuch“ einzufügen und sie anhand einer ID abzurufen.
Etwas wie das Folgende wäre ideal
Beispiel
\updateContactInfo{
\addMember{
id = joe,
name = Joe Doe,
mobile = 12345678,
phone = 87654321
}
\addMember{
id = jane,
name = Jane Doe,
mobile = 12345678,
phone = 87654321
}
}
\begin{document}
\getContactInfo[jane][name]
\end{document}
Hier ist, was ich bisher habe, aber der Code ist nicht ideal. Die Syntax ist für den normalen Benutzer schwer zu lesen und zu aktualisieren
Code
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse,xstring}
\NewDocumentCommand\contactInfo{m m}{%
\IfStrEqCase{#1}{%
{jane}{%
\IfStrEqCase{#2}{%
{name}{Jane Doe}
{mobile}{32132132}
{phone}{123123123}
}[]
}
{Joe}{%
\IfStrEqCase{#2}{%
{name}{Joe Doe}
{mobile}{32132132}
{phone}{123123123}
}[]
}
{richard}{%
\IfStrEqCase{#2}{%
{name}{Richard Roe}
{mobile}{32132132}
{phone}{123123123}
}[]
}%
}[]
}
\begin{document}
\contactInfo{jane}{mobile}
\end{document}
Antwort1
Sie müssen das Speichern und Abrufen von Kontaktinformationen trennen.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\defineInfo}{mm}
{
\prop_new:c { g_nebu_contact_#1_prop }
\prop_gset_from_keyval:cn { g_nebu_contact_#1_prop } { #2 }
}
\NewDocumentCommand{\contactInfo}{mm}
{
\str_case:nnF { #2 }
{
{phone} { \nebu_phone:e { \prop_item:cn { g_nebu_contact_#1_prop } { #2 } } }
{mobile} { \nebu_phone:e { \prop_item:cn { g_nebu_contact_#1_prop } { #2 } } }
}
{ \prop_item:cn { g_nebu_contact_#1_prop } { #2 } }
}
%%% formatting phone numbers
\NewDocumentCommand{\phone}{m}
{
\nebu_phone:n { #1 }
}
\tl_new:N \l__nebu_phone_tl
\cs_new_protected:Nn \nebu_phone:n
{
\tl_set:Nn \l__nebu_phone_tl { #1 }
\tl_remove_all:Nn \l__nebu_phone_tl { ~ }
\int_compare:nTF { \tl_count:N \l__nebu_phone_tl = 8 }
{
\__nebu_phone_eight:V \l__nebu_phone_tl
}
{
\tl_use:N \l__nebu_phone_tl
}
}
\cs_generate_variant:Nn \nebu_phone:n { e }
\cs_new:Nn \__nebu_phone_eight:n
{
\str_case_e:nnF { \tl_head:n { #1 } }
{
{4}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{8}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{9}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
}
{
\__nebu_phone_ii:nnnnnnnn #1
}
}
\cs_generate_variant:Nn \__nebu_phone_eight:n { V }
\cs_new:Nn \__nebu_phone_iii_ii_iii:nnnnnnnn { #1#2#3\nobreakspace#4#5\nobreakspace#6#7#8 }
\cs_new:Nn \__nebu_phone_ii:nnnnnnnn { #1#2\nobreakspace#3#4\nobreakspace#5#6\nobreakspace#7#8 }
\ExplSyntaxOff
\defineInfo{jane}{
name=Jane Doe,
mobile=32132132,
phone=45123123,
}
\defineInfo{joe}{
name=Joe Doe,
mobile=32132132,
phone=93123123,
}
\defineInfo{richard}{
name=Richard Roe,
mobile=32132132,
phone=83123123,
}
\begin{document}
\contactInfo{jane}{name} has \contactInfo{jane}{mobile} mobile phone.
\contactInfo{richard}{phone} is \contactInfo{richard}{name}'s phone.
\end{document}
Ich habe auch die Unterstützung zum Drucken von Telefonnummern hinzugefügt, wie in einer meiner vorherigen Antworten beschrieben.
Die Idee besteht darin, für jeden Kontakt eine Eigenschaftsliste zu verwenden. Damit \prop_gset_from_keyval:Nn
ist eine einfache Schnittstelle für die Eingabe möglich.
Mit der folgenden Version wird beim Kontaktschlüssel die Groß-/Kleinschreibung nicht mehr beachtet:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\defineInfo}{mm}
{
\prop_new:c { \__nebu_contact_prop:n { #1 } }
\prop_gset_from_keyval:cn { \__nebu_contact_prop:n { #1 } } { #2 }
}
\NewDocumentCommand{\contactInfo}{mm}
{
\str_case:nnF { #2 }
{
{phone} { \nebu_phone:e { \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } } }
{mobile} { \nebu_phone:e { \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } } }
}
{ \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } }
}
% syntactic sugar
\cs_new:Nn \__nebu_contact_prop:n
{
g_nebu_contact_\str_lowercase:n { #1 }_prop
}
%%% formatting phone numbers
\NewDocumentCommand{\phone}{m}
{
\nebu_phone:n { #1 }
}
\tl_new:N \l__nebu_phone_tl
\cs_new_protected:Nn \nebu_phone:n
{
\tl_set:Nn \l__nebu_phone_tl { #1 }
\tl_remove_all:Nn \l__nebu_phone_tl { ~ }
\int_compare:nTF { \tl_count:N \l__nebu_phone_tl = 8 }
{
\__nebu_phone_eight:V \l__nebu_phone_tl
}
{
\tl_use:N \l__nebu_phone_tl
}
}
\cs_generate_variant:Nn \nebu_phone:n { e }
\cs_new:Nn \__nebu_phone_eight:n
{
\str_case_e:nnF { \tl_head:n { #1 } }
{
{4}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{8}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{9}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
}
{
\__nebu_phone_ii:nnnnnnnn #1
}
}
\cs_generate_variant:Nn \__nebu_phone_eight:n { V }
\cs_new:Nn \__nebu_phone_iii_ii_iii:nnnnnnnn { #1#2#3\nobreakspace#4#5\nobreakspace#6#7#8 }
\cs_new:Nn \__nebu_phone_ii:nnnnnnnn { #1#2\nobreakspace#3#4\nobreakspace#5#6\nobreakspace#7#8 }
\ExplSyntaxOff
\defineInfo{jane}{
name=Jane Doe,
mobile=32132132,
phone=45123123,
}
\defineInfo{joe}{
name=Joe Doe,
mobile=32132132,
phone=93123123,
}
\defineInfo{richard}{
name=Richard Roe,
mobile=32132132,
phone=83123123,
}
\begin{document}
\contactInfo{Jane}{name} has \contactInfo{jane}{mobile} mobile phone.
\contactInfo{riCHard}{phone} is \contactInfo{Richard}{name}'s phone.
\end{document}
Eine Version, die möglicherweise mit TeX Live 2017 kompatibel ist; die Verwendung von Overleaf kann ich nicht empfehlen, da es sehr hinterherhinkt.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\defineInfo}{mm}
{
\prop_new:c { \__nebu_contact_prop:n { #1 } }
\prop_clear:N \l__nebu_contact_tmp_prop
\keys_set:nn { nebu/contact } { #2 }
\prop_gset_eq:cN { \__nebu_contact_prop:n { #1 } } \l__nebu_contact_tmp_prop
}
\keys_define:nn { nebu/contact }
{
unknown .code:n = \prop_put:NVn \l__nebu_contact_tmp_prop \l_keys_key_tl { #1 }
}
\prop_new:N \l__nebu_contact_tmp_prop
\cs_generate_variant:Nn \prop_put:Nnn { NV }
\NewDocumentCommand{\contactInfo}{mm}
{
\str_case:nnF { #2 }
{
{phone} { \nebu_phone:e { \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } } }
{mobile} { \nebu_phone:e { \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } } }
}
{ \prop_item:cn { \__nebu_contact_prop:n { #1 } } { #2 } }
}
% syntactic sugar
\cs_new:Nn \__nebu_contact_prop:n
{
g_nebu_contact_\str_lowercase:n { #1 }_prop
}
%%% formatting phone numbers
\NewDocumentCommand{\phone}{m}
{
\nebu_phone:n { #1 }
}
\tl_new:N \l__nebu_phone_tl
\cs_new_protected:Nn \nebu_phone:n
{
\tl_set:Nn \l__nebu_phone_tl { #1 }
\tl_remove_all:Nn \l__nebu_phone_tl { ~ }
\int_compare:nTF { \tl_count:N \l__nebu_phone_tl = 8 }
{
\__nebu_phone_eight:V \l__nebu_phone_tl
}
{
\tl_use:N \l__nebu_phone_tl
}
}
\cs_generate_variant:Nn \nebu_phone:n { e }
\cs_new:Nn \__nebu_phone_eight:n
{
\str_case_e:nnF { \tl_head:n { #1 } }
{
{4}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{8}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
{9}{ \__nebu_phone_iii_ii_iii:nnnnnnnn #1 }
}
{
\__nebu_phone_ii:nnnnnnnn #1
}
}
\cs_generate_variant:Nn \__nebu_phone_eight:n { V }
\cs_new:Nn \__nebu_phone_iii_ii_iii:nnnnnnnn { #1#2#3\nobreakspace#4#5\nobreakspace#6#7#8 }
\cs_new:Nn \__nebu_phone_ii:nnnnnnnn { #1#2\nobreakspace#3#4\nobreakspace#5#6\nobreakspace#7#8 }
\ExplSyntaxOff
\defineInfo{jane}{
name=Jane Doe,
mobile=32132132,
phone=45123123,
}
\defineInfo{joe}{
name=Joe Doe,
mobile=32132132,
phone=93123123,
}
\defineInfo{richard}{
name=Richard Roe,
mobile=32132132,
phone=83123123,
}
\begin{document}
\contactInfo{Jane}{name} has \contactInfo{jane}{mobile} mobile phone.
\contactInfo{riCHard}{phone} is \contactInfo{Richard}{name}'s phone.
\end{document}