0

My background is software-development and object-oriented programming. Therefore I got the following question about LaTeX.

I got the following command, which defines the properties of a contact person.

\usepackage{tabularx}
\usepackage{xkeyval}
\usepackage{hyperref}

% define the key (arguments)
\makeatletter
\define@key{personkeys}{name}{%
  \def\personname{#1}
}
\define@key{personkeys}{phone}{%
  \def\personphone{#1}
}
\define@key{personkeys}{email}{%
  \def\personemail{#1}
}
\makeatother
% end of key definition

\newcommand{\contact}[2][]{%
    \setkeys{personkeys}{#1}%

    \personname \newline
    \href{mailto:\personemail}{\nolinkurl{\personemail}} \newline
    \personphone \bigskip
}

The command is then used to specify persons that are used often throughout the document. Such person is then accessed by the command \johndoe.

\newcommand{\johndoe}{
    \contact[
    name={Mr John Doe}, 
    email={john@doe.co}, 
    phone={+00 00 012345}
    ];
}

The problem is, that some persons would like to include their phone number and others don't. Nevertheless I want the number filled out in the command and just set a flag like hidePhone that is then interpreted by the command definition.

\newcommand{\johndoe}{
    \contact[
    name={Mr John Doe}, 
    email={john@doe.co}, 
    phone={+00 00 012345},
    hidePhone=True
    ];
}

Question: Is there a way to have a command show or hide a text depending on the flag? Otherwise I have to simply leave the phone number and/or Email address blank.

Remark: I had a look into this posting and also this one, but both care about document wide enabling/disabling of text fragments. I need it per command.

1

You might do it like this (further utilizing the xkeyval-package):

\documentclass{article}

\usepackage{tabularx}
\usepackage{xkeyval}
\usepackage{hyperref}

% define the key (arguments)
\makeatletter
\define@key{personkeys}{name}{%
  \def\personname{#1}
}
\define@key{personkeys}{phone}{%
  \def\personphone{#1}
}
\define@key{personkeys}{email}{%
  \def\personemail{#1}
}
\define@boolkey{personkeys}[my]{hidePhone}{}
\makeatother
% end of key definition

\newcommand{\contact}[2][]{%
    \setkeys{personkeys}{#1}%

    \personname \newline
    \href{mailto:\personemail}{\nolinkurl{\personemail}} \ifmyhidePhone\else\newline
    \personphone \fi\bigskip
}

\newcommand{\johndoe}{
    \contact[
    name={Mr John Doe}, 
    email={john@doe.co}, 
    phone={+00 00 012345},
    hidePhone=True
    ];
}
\newcommand{\janedoe}{
    \contact[
    name={Ms Jane Doe}, 
    email={jane@doe.co}, 
    phone={+00 00 012345},
    hidePhone=False
    ];
}


\begin{document}
\johndoe
\janedoe
\end{document}

results

  • Very nice! Can you explain the "my" argument on the hidePhone keyval definition? Is there a way to have a default value, so that the hidePhone flag doesn't need to be used in all contacts? Imagine default value is false. Then you could delete the flag from Jane Doe but keep it on John Doe. – Matthias Mar 22 '17 at 14:44
  • Just realized that False is the default value and that you may skip the flag on those contacts that have the default value. How would you define True as default value? – Matthias Mar 22 '17 at 14:59
  • @Matthias The package lets you define a default value like this: \define@boolkey{personkeys}[my]{hidePhone}[<default>]{} (didn't test that, just believed the package documentation of xkeyval). The [my] is just to prefix the boolean key's name (I used it because I didn't find out the default naming). – Skillmon Mar 22 '17 at 15:08
  • Ok thanks. The docs of the package aren't easy to read. – Matthias Mar 22 '17 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.