Optionally show/enable or hide/disable text

1,342

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={[email protected]}, 
    phone={+00 00 012345},
    hidePhone=True
    ];
}
\newcommand{\janedoe}{
    \contact[
    name={Ms Jane Doe}, 
    email={[email protected]}, 
    phone={+00 00 012345},
    hidePhone=False
    ];
}


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

results

Share:
1,342

Related videos on Youtube

Matthias
Author by

Matthias

Instrumental in successful development of GUI components for client-server multi-user systems. Deployed advanced technologies such as UML, Microsoft .NET, OpenGL/DirectX and Microsoft SQL Server.

Updated on August 01, 2022

Comments

  • Matthias
    Matthias over 1 year

    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={[email protected]}, 
        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={[email protected]}, 
        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.

    • Steven B. Segletes
      Steven B. Segletes over 6 years
      How about this recent question: tex.stackexchange.com/questions/359387/…
    • Matthias
      Matthias over 6 years
      No, that one is redefining the command. This would indicate that I have all different combinations of my contact command. With/Without phone number; With/Without email address and so on. Of course, that works, but it's bad design.
    • Dr. Manuel Kuehner
      Dr. Manuel Kuehner over 6 years
    • Matthias
      Matthias over 6 years
      No, because it's again throughout the whole document. The answer below works perfectly.
  • Matthias
    Matthias over 6 years
    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
    Matthias over 6 years
    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?
  • Skillmon
    Skillmon over 6 years
    @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).
  • Matthias
    Matthias over 6 years
    Ok thanks. The docs of the package aren't easy to read.