# auto arrangement of subfields in postal address fields of letters

recipients and addresses (in letters) have a set of common fields like first name, surname, street, town, phone number... I made a template in which all elements of an address are given through pre-defined fields like \def\firstname(myname), ..., \def\landlinephone{1234}.

For the moment i'm using \ifthenelse statements to avoid problems with empty fields.

\firstname~\surname
\street
\town~\zipcode
\ifthenelse{\equal{\landlinephone}{}}{}%
{\landlinephsymbol~\landlinephone}
\ifthenelse{\equal{\mobilephone}{}}{\\}%
{ \mobilephsymbol~\landlinephone}
\ifthenelse{\equal{\email}{}}{\\}%
{\emailsymbol~\email\\}


and so on. Here if the recipient does not have any land line phone number, the mobile number and email would still consume two lines. I'd like in this case that latex arranges both to be on the same line. I'd accept maximum two electronic contact info per line. I could do that with a lot of \ifthenelse but the code would be too big.

Update

below i show a case in which a total of 6 contact numbers/addresses are given. I displayed these contact details with a tabular. The way it is done here with tabular is only fine for the case all of the six fields of the list are given. The order in which the fields are defined in the .tex file is fine.

What i need is that \contactdetails fills with this list in the given order, the table from left to right and add lines as per the need. A new row should be created only if the two fields of the previous are full. If only one mobile number and an email are given, then only one row must be created. The first field will have the mobile num. and next the email address.

I don't want \contactdetails to be limited to six fields. I might use the code later to fill bigger tables with totally different type of data. whatever matches '.*phone\d' and '.email\d', where '.' could be a random string and \d is a digit, should be taken as an electronic contact detail.

\usepackage{marvosym}
\def\landlinephone{1023455669}
\def\mobilephone{9876543212}
\def\landlinephone2{1231234567}
\def\mobilephone2{8765423444}

\newcommand{\contactdetails}{%
\begin{tabular}{ l l }
\Telefon~\landlinephone & \Telefon~\landlinephone2
\Mobilefone~\def\mobilephone & \def\mobilephone2
\end{tabular}


So, if I understand correctly, if there's just two items for electronic content, we put them on the same line, but if there's three each on their own line (let's call this possibility A). The other possibility is that if there's three, the first two go together and the third goes on its own line. We'll call this possibility B.

Possibility B is the easier one. We'll can define a command, \separator to be

\def\separator{\quad\def\separator{\\}}


and then write:

\ifthenelse{\equal{\landlinephone}{}}{}%
{\landlinephsymbol~\landlinephone\separator}%
\ifthenelse{\equal{\mobilephone}{}}{}%
{\mobilephsymbol~\landlinephone\separator}%
\ifthenelse{\equal{\email}{}}{}%
{\emailsymbol~\email}%


to get the expected result.

Possibility A is a bit trickier. We'll want to know whether we have more than two electronic contact items and then define \separator appropriately. The approach for this will be to store the contact items for later expansion.

\newcounter{electronicitems}

\setcounter{electronicitems}{0}
\ifthenelse{\equal{\landlinephone}{}}{\def\LANDLINE{}}%
{\def\LANDLINE{\landlinephsymbol~\landlinephone\separator}%
\stepcounter{electronicitems}}%
\ifthenelse{\equal{\mobilephone}{}}{}%
{\def\MOBILE{\mobilephsymbol~\landlinephone\separator}%
\stepcounter{electronicitems}}%
\ifthenelse{\equal{\email}{}}{\def\EMAIL{}}%
{\def\EMAIL{\emailsymbol~\email}%
\stepcounter{electronicitems}}%
\ifnum\value{electronicitems}>2\def\separator{\\}%
\fi
\LANDLINE\MOBILE\EMAIL

• never i said each on their own line for 3. Using a counter sounds good. If the counter is odd, put a long white space, a newline otherwise. And in both case only if there remains at least one phone number or email address to put. – user1850133 Mar 4 at 6:38

You can set up the electronic info storing to use a counter. This way you can keep track of how many items have been defined. Here's a possibly implementation:

\documentclass{article}

\usepackage{fontawesome}

\newcommand{\setlandlinephone}[1]{%
\def\landlinephone{#1}% Store landline phone number
% Check if user supplied anything (could be empty)
\ifnum\pdfstrcmp{#1}{}=0\else% User did supply a non-empty landline phone number
\stepcounter{einfo}% Step einfo counter
\fi}
\newcommand{\setmobilephone}[1]{%
\def\mobilephone{#1}% Store mobile phone number
% Check if user supplied anything (could be empty)
\ifnum\pdfstrcmp{#1}{}=0\else% User did supply a non-empty mobile phone number
\stepcounter{einfo}% Step einfo counter
\fi}
\newcommand{\setemail}[1]{%
% Check if user supplied anything (could be empty)
\ifnum\pdfstrcmp{#1}{}=0\else% User did supply a non-empty email address
\stepcounter{einfo}% Step einfo counter
\fi}

\newcounter{einfo}
\newcommand{\seteinfo}{%
\ifnum\value{einfo}=3
% 3 elements provided; choose preference (mobile + email; ignore landline)
\faMobile~\mobilephone~~\faAt~\email
\else% einfo < 3
\ifnum\pdfstrcmp{\csname landlinephone\endcsname}{}=0\else
\faPhone~\csname landlinephone\endcsname~~% Landline supplied
\fi
\ifnum\pdfstrcmp{\csname mobilephone\endcsname}{}=0\else
\faMobile~\csname mobilephone\endcsname~~% Mobile phone supplied
\fi
\ifnum\pdfstrcmp{\csname email\endcsname}{}=0\else
\faAt~\csname email\endcsname% Email supplied
\fi
\fi
}

\begin{document}

\newcommand{\showeinfo}[3]{% Just for this example
\par\medskip
\setcounter{einfo}{0}% Reset electronic information counter
\setlandlinephone{#1}% Landline
\setmobilephone{#2}% Mobile
\setemail{#3}% Email
\seteinfo
}

\showeinfo{123-456-7890}{}{}% Only landline
\showeinfo{}{987-654-3210}{}% Only mobile
\showeinfo{}{}{who@cares.com}% Only email
\showeinfo{123-456-7890}{987-654-3210}{}% Landline and mobile only
\showeinfo{123-456-7890}{}{who@cares.com}% Landline and email only
\showeinfo{}{987-654-3210}{who@cares.com}% Mobile and email only
\showeinfo{123-456-7890}{987-654-3210}{who@cares.com}% Landline, mobile and email

\end{document}


In the above code, a text comparison between elements uses \pdfstrcmp{<stringA>}{<stringB>}. This returns a result in the form of a number. Specifically, it returns -1 if <stringA> < <stringB> lexicographically, 0 if they're equal, or 1 if <stringA> > <stringB> lexicographically. So, with the expected outcome of this string comparison being a number, we use \ifnum<string comparison>=0 to check whether the two strings are the same or not. It's specifically used to check whether the user supplied a blank entry or not, which may require expansion of the arguments - something \pdfstrcmp does naturally.

• I'd probably suggest using a different (key-value) interface though: \setinfo{firstname={first name}, surname={surname}, landline={123-456-7890}, ...} so things can be set up succinctly and clearly where you do all the back-end details within \setinfo. Then, when it comes time to set content, it's a bit more clear what goes where. – Werner Mar 2 at 17:04
• can you add a lot of comments in your code? – user1850133 Mar 3 at 16:18
• @user1850133: I've added some comments and more detail on \pdfstrcmp. – Werner Mar 3 at 16:27
• what if there are 2 or 3 landline or mobile numbers? 3 is not the limit. Only 2 per line is a limit. – user1850133 Mar 4 at 6:30
• @user1850133: Oh, that wasn't clear from the post. I thought you're only interested in listing 2 elements, and you'll only supply a single one for each. How would you supply multiple landline/mobile/email numbers? Perhaps it would help if you provide some examples of use-cases and the anticipated output. – Werner Mar 4 at 6:46