Einführung

Einführung

Einführung

Es tut mir leid, dass der MWE so lang war, aber ich konnte ihn nicht kürzer machen und trotzdem mein Problem veranschaulichen. Ich arbeite wieder daran, die Erstellung von Prüfungen für meine Universität zu rationalisieren. Im folgenden Beispiel kann der Benutzer weitere Vorlesungen hinzufügen, indem er

\defineInfo{jane}{
  name=Jane Doe, 
  phone=75123123,
}

(Wo ich Hilfe bei der Erstellung der Syntax bekam inKontaktliste als Wörterbuch) Fügen Sie Informationen zur Prüfung hinzu mit

\FrontpageUiTsetup{
  contact = jane,
}

(Jetzt mit Hilfe vonBeschriftete Eingabeparameter) Wobei der letzte Befehl natürlich noch viel mehr Parameter hat, die für meine Frage irrelevant sind. Nun möchte ich diese beiden Antworten kombinieren, damit ich mich über eine ID auf einen Dozenten beziehen und dann die relevanten Teile in ausdrucken kann \FrontpageUiT.

Ich habe zuerst versucht zu schreiben

\ifx\@contact\@empty\else
  \contactname & \contactInfo{\@contact}{name}\\%
\fi

Es scheint jedoch, dass der \contactInfoBefehl vorher erweitert wird, \@contactsodass er nichts ausgibt. Nach ein wenig Googeln konnte ich Folgendes verwenden, um \@contactzuerst zu erweitern

\newcommand{\expandMacro}[1]{
  \begingroup\edef\x{\endgroup
    \noexpand#1%
  }\x
}

Problem

Hier kommt mein Problem:

  • Ich möchte, dass der Standardwert von @phone und @mobile durch die Zeilen \contactInfo{\@contact}{mobile}bzw.\contactInfo{\@contact}{phone}

Das Problem tritt auf, wenn ich die Leitung

\contactmobile & \@mobile 

Wird leer gelassen, wenn keine Handynummer angegeben ist. Beispiel:

    mobile        .tl_set:N  = \@mobile,
    mobile        .initial:n = {\expandMacro{\contactInfo{\@contact}{mobile}}},
    phone         .tl_set:N  = \@phone,
    phone         .initial:n = {\expandMacro{\contactInfo{\@contact}{phone}}},
.
.
.
  \ifx\@contact\@empty\else
    \contactname & \expandMacro{\contactInfo{\@contact}{name}}\\%
  \fi

  \ifx\@mobile\@empty\else
    \contactmobile & \@mobile\\%
  \fi

  \ifx\@phone\@empty\else
    \contactphone & \@phone\\%
.
.
.

\defineInfo{jane}{
  name=Jane Doe, 
  phone=75123123,
}

\FrontpageUiTsetup{
  contact = jane,
}

\begin{document}
\FrontpageUiT
\end{document}

Da Jane keine Handynummer hat, würde ich erwarten, dass die Ausgabe lautet

Contact    Jane Doe
Phone      75 12 31 23

Stattdessen stehe ich vor

Contact    Jane Doe
Mobile
Phone      75 12 31 23

Das Problem ist, dass der Code nicht \contactInfo{\@contact}{mobile}schnell genug expandieren kann, um zu erkennen, dass er leer ist. Nach etwas mehr Googeln fand ich

\newcommand{\IfNoText}[3]{%
  \sbox0{#1}%
  \ifdim\wd0=0pt %
    {#2}% if #1 is empty
  \else%
    \ifdim0pt=\dimexpr\ht0+\dp0\relax
      {#2}% if #1 is empty
    \else
      {#3}% if #1 is not empty
    \fi
  \fi%
}

Damit kann man erkennen, ob die Ausgabe leer ist. Allerdings unterstützt es kein Et-Zeichen (&), daher kann ich es in meiner Tabelle nicht verwenden. EG

\IfNoText{\@mobile}{}{Mobile & \@mobile \\}

gibt mir einen Fehler aus. Ich habe auch versucht, \IfNoTextfrüher in meinem Dokument Umschalter zu definieren, die sich basierend auf dem Inhalt von auf true oder false erweitern \@mobile. Diese wurden jedoch ebenfalls nicht schnell genug erweitert.

  • Gibt es eine Möglichkeit, dies schneller zu überprüfen \@mobileund @phoneleer zu sein, wenn sie in den Bedingungen von angegeben sind \contactInfo?

Code

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{xparse}
\usepackage{xfp}

\newcommand{\IfNoText}[3]{%
  \sbox0{#1}%
  \ifdim\wd0=0pt %
    {#2}% if #1 is empty
  \else%
    \ifdim0pt=\dimexpr\ht0+\dp0\relax
      {#2}% if #1 is empty
    \else
      {#3}% if #1 is not empty
    \fi
  \fi%
}

\newcommand{\expandMacro}[1]{
  \begingroup\edef\x{\endgroup
    \noexpand#1%
  }\x
}

\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\hspace{1ex}#4#5\hspace{1ex}#6#7#8 }
\cs_new:Nn \__nebu_phone_ii:nnnnnnnn { #1#2\hspace{0.6666ex}#3#4\hspace{0.6666ex}#5#6\hspace{0.6666ex}#7#8 }

\ExplSyntaxOff

% https://tex.stackexchange.com/a/504129/8306
%
% This creates the backbone for the frontpage. One can add new keys here
\makeatletter
  % Provide commands.
  \ExplSyntaxOn
    % Provide keys.
    \keys_define:nn { babylonia }
      {
        contact       .tl_set:N  = \@contact,
        mobile        .tl_set:N  = \@mobile,
        mobile        .initial:n = {\expandMacro{\contactInfo{\@contact}{mobile}}},
        phone         .tl_set:N  = \@phone,
        phone         .initial:n = {\expandMacro{\contactInfo{\@contact}{phone}}},
      }
    % Provide key setting command.
    \NewDocumentCommand\FrontpageUiTsetup{ m }{
      \keys_set:nn { babylonia } { #1 }
    }
  \ExplSyntaxOff
  % Provide names.
  \newcommand*\contactname{Contact}
  \newcommand*\contactmobile{Mobile}
  \newcommand*\contactphone{Phone}
  % The typesetting command.
  \newcommand\FrontpageUiT{%

    \noindent
    \begin{tabular}{p{0.2\textwidth} p{\dimexpr 0.8\textwidth - 4\tabcolsep}}

      \ifx\@contact\@empty\else
        \contactname & \expandMacro{\contactInfo{\@contact}{name}}\\%
      \fi

      \ifx\@mobile\@empty\else
        \contactmobile & \@mobile\\%
      \fi

      \ifx\@phone\@empty\else
        \contactphone & \@phone\\%
      \fi

    \end{tabular}%
  }
\makeatother

% ==============================================================================
%   User interface below here
% ==============================================================================

% This adds contacts to be used in the UiT frontpage
% Example:
%
% \defineInfo{joe}{
%   name=Joe Doe, mobile=32132132, phone=93123123,
% }

\defineInfo{jane}{
  name=Jane Doe, 
  phone=75123123,
}

\FrontpageUiTsetup{
  contact = jane,
}

\begin{document}

\FrontpageUiT

\end{document}

Antwort1

Fügen Sie zwei Befehle hinzu, einen zum Testen des Vorhandenseins einer Eigenschaft und den anderen zum Setzen der Informationen.

\NewExpandableDocumentCommand{\contactTestTF}{mmmm}
 {% #1 = contact, #2 = property to test, #3 = code for true, #4 = code for false
  \prop_if_in:cnTF { \__nebu_contact_prop:n { #1 } } { #2 } { #3 } { #4 }
 }
\NewDocumentCommand{\FrontpageUiTsetup}{m}
 {
  \begin{tabular}{@{} l l @{}}
  \contactname & \contactInfo{#1}{name} \\
  \contactTestTF{#1}{mobile}{\mobilename & \contactInfo{#1}{mobile} \\}{}
  \contactTestTF{#1}{phone}{\phonename & \contactInfo{#1}{phone} \\}{}
  \end{tabular}
 }

Vollständiges Beispiel:

\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 }
 }
\NewExpandableDocumentCommand{\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 } }
 }
\NewExpandableDocumentCommand{\contactTestTF}{mmmm}
 {% #1 = contact, #2 = property to test, #3 = code for true, #4 = code for false
  \prop_if_in:cnTF { \__nebu_contact_prop:n { #1 } } { #2 } { #3 } { #4 }
 }
\NewDocumentCommand{\FrontpageUiTsetup}{m}
 {
  \begin{tabular}{@{} l l @{}}
  \contactname & \contactInfo{#1}{name} \\
  \contactTestTF{#1}{mobile}{\mobilename & \contactInfo{#1}{mobile} \\}{}
  \contactTestTF{#1}{phone}{\phonename & \contactInfo{#1}{phone} \\}{}
  \end{tabular}
 }

% 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

\newcommand{\contactname}{Contact}
\newcommand{\mobilename}{Mobile}
\newcommand{\phonename}{Phone}

\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.

\bigskip

\FrontpageUiTsetup{jane}

\bigskip

\FrontpageUiTsetup{joe}

\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen