TransWikia.com

auto arrangement of subfields in postal address fields of letters

TeX - LaTeX Asked by user1850133 on March 7, 2021

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 deffirstname(myname), …, deflandlinephone{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 ‘.*phoned’ and ‘.emaild’, where ‘.‘ could be a random string and d is a digit, should be taken as an electronic contact detail.

usepackage{marvosym}
deflandlinephone{1023455669}
defmobilephone{9876543212}
deflandlinephone2{1231234567}
defmobilephone2{8765423444}
defemailaddr{[email protected]}
defemailaddr2{[email protected]}

newcommand{contactdetails}{%
begin{tabular}{ l l }
Telefon~landlinephone & Telefon~landlinephone2
Mobilefone~defmobilephone & defmobilephone2
Letter~defemailaddr & defemailaddr2
end{tabular}

2 Answers

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

defseparator{quaddefseparator{}}

and then write:

ifthenelse{equal{landlinephone}{}}{}%
   {landlinephsymbol~landlinephoneseparator}%
ifthenelse{equal{mobilephone}{}}{}%
   {mobilephsymbol~landlinephoneseparator}%
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}{}}{defLANDLINE{}}%
   {defLANDLINE{landlinephsymbol~landlinephoneseparator}%
    stepcounter{electronicitems}}%
ifthenelse{equal{mobilephone}{}}{}%
   {defMOBILE{mobilephsymbol~landlinephoneseparator}%
    stepcounter{electronicitems}}%
ifthenelse{equal{email}{}}{defEMAIL{}}%
   {defEMAIL{emailsymbol~email}%
    stepcounter{electronicitems}}%
ifnumvalue{electronicitems}>2defseparator{}%
   elsedefseparator{quad}%
fi
LANDLINEMOBILEEMAIL

Answered by Don Hosek on March 7, 2021

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:

enter image description here

documentclass{article}

usepackage{fontawesome}

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

newcounter{einfo}
newcommand{seteinfo}{%
  ifnumvalue{einfo}=3
    % 3 elements provided; choose preference (mobile + email; ignore landline)
    faMobile~mobilephone~~faAt~email
  else% einfo < 3
    ifnumpdfstrcmp{csname landlinephoneendcsname}{}=0else
      faPhone~csname landlinephoneendcsname~~% Landline supplied
    fi
    ifnumpdfstrcmp{csname mobilephoneendcsname}{}=0else
      faMobile~csname mobilephoneendcsname~~% Mobile phone supplied
    fi
    ifnumpdfstrcmp{csname emailendcsname}{}=0else
      faAt~csname emailendcsname% Email supplied
    fi
  fi
}

begin{document}


newcommand{showeinfo}[3]{% Just for this example
  parmedskip
  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{}{}{[email protected]}% Only email
showeinfo{123-456-7890}{987-654-3210}{}% Landline and mobile only
showeinfo{123-456-7890}{}{[email protected]}% Landline and email only
showeinfo{}{987-654-3210}{[email protected]}% Mobile and email only
showeinfo{123-456-7890}{987-654-3210}{[email protected]}% 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.

Answered by Werner on March 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP