[Tex/LaTex] Using an adr-file, how to change format of the address

addresseslettersscrlttr2

I'm up to use LaTeX for writing my letters, invoices, other stuff and I'm nearly finished with setting up my templates using the KOMA class scrlttr2. One thing remains…

I got an address-file where I put my contacts/clients like this:

\adrentry{name}{surname}{address}{phone}{F1}{F2}{comment}{LABELNAME}

Then I use it in my main .tex file:

...
\input{contacts.adr}
\begin{letter}{\LABELNAME}
....

This is working pretty well, but I want to have a special formatting. Depending on F1, where I put the gender of my contact, I want to have "Mr." or "Mrs." or if not present "Company" right before the name. And I can't figure out how that could be done. I've read a lot of examples showing how to generate a bulk letter but can't transform this to my case of just formatting one address instead of them all.

But maybe that points someone else in the right direction so he can help me out?

\renewcommand*{\adrentry}[8]{
  \begin{letter}{#2 #1\\#3}
    \if #5m \opening{Dear Mr. #1,} \fi
    \if #5w \opening{Dear Mrs. #1,} \fi
    Some text in which the arguments can be used as well.
    \closing{Bye,}
  \end{letter}
}

So what I want to achieve is something that might look like this (It's not working, I know):

\renewcommand*{\adrentry}[8]{
  \if #5m Mr. #2 #1\\ \fi
  \if #5w Mrs. #2 #1\\ \fi
  \if #5c #2\\#6\\ \fi % It's a company, #6 states the contact person
  #3
}

Any ideas, links, etc.?

Oh boy, I hope you can follow and thanks in advance!


EDIT:

It follows a minimal working example showing the state that I have, not including the feature described above (since I o not know how to do that 😉

The address file (contacts.adr) including a person and a company:

\adrentry{Cool Company Inc.}{}{Companystreet 21\\12345 Elsewhere}{}{c}{Mr. Tim Tailor}{}{CCI}
\adrentry{Meier}{Michael}{Meierstreet 34\\45636 Nowhere}{}{m}{}{}{MEIER}

The tex file using scrlttr2:

\documentclass[
    paper=a4,
    version=last,
    enlargefirstpage,
    fontsize=11pt]{scrlttr2}
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{fromphone}{+49\,123\,45678910}
\setkomavar{fromemail}{mymail@gmail.com}
\setkomavar{backaddress}{Max Myname, Some Street 12, 012345 Somewhere}
\setkomavar{fromaddress}{Some Street 12\\012345 Somewhere}

\begin{document}

\setkomavar{subject}{The subject}
\setkomavar{date}{\today}
\setkomavar{place}{Dresden}
\setkomavar{yourmail}{1.1.2013}
\setkomavar{yourref}{76/54/321}
\setkomavar{myref}{123/456/789}

\input{contacts.adr}
\begin{letter}{\MEIER}
\opening{Dear Mr. Meier}
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}

The above produces

Michael Meier 
Meierstreet 34 
45636 Nowhere

and

Cool Company Inc. 
Companystreet 21 
12345 Elsewhere

But I'd like it to produce

Mr.
Michael Meier 
Meierstreet 34 
45636 Nowhere

and

Cool Company Inc.
Mr. Tim Tailor 
Companystreet 21 
12345 Elsewhere

Best Answer

Nice question; I solved it by changing \addrentry and adding something innocuous to \begin{letter}, that will do nothing if a subsequent \makeopening command doesn't follow.

In the usual \addentry command, the last argument is used to build a control sequence that stores the address lines. I add also the building of an opening line, which for MEIER is stored in \opening@MEIER. This command is called by \makeopening (using the hook added before).

\documentclass[
    paper=a4,
    version=last,
    enlargefirstpage,
    fontsize=11pt]{scrlttr2}
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

\usepackage{xpatch}
%%% Patch the relevant scrlttr macros
\makeatletter
% at the end of \begin{letter}{<name>} store the <name>
\xapptocmd\letter{\gdef\this@letter@to{#2}}{}{}

% this seems complicated, but is needed to strip off
% the backslash from the argument to `\begin{letter}`
% Don't use `\makeopening` if the argument to `\begin{letter}`
% is not obtained via the `.adr` file

% we need to get \opening@MEIER from the fact that
% \this@letter@to expands to \MEIER so we first expand
% \this@letter@to, then \string, so that \@gobble will
% remove the backslash
\newcommand\makeopening{%
  \@nameuse{%
    opening@%
    \expandafter\expandafter\expandafter
    \@gobble\expandafter\string\this@letter@to}}

% modify \addrentry to define an opening and
% the address lines as desired
\newif\if@company
\renewcommand\addrentry[9]{%
  \def\@tempa{#1}%
  \ifx\@tempa\@empty
  \else
    \@companyfalse
    \if#5m%
      \@namedef{opening@#9}{\opening{Dear Mr.\ #1,}}%
    \else
      \if#5w%
        \@namedef{opening@#9}{\opening{Dear Mrs.\ #1,}}%
      \else
        \@namedef{opening@#9}{\opening{Sirs,}}%
        \@companytrue
      \fi
    \fi
    \def\@tempa{#2}%
    \ifx\@tempa\@empty
      \if@company
        \@namedef{#9}{#1\\#6\\#3}%
      \else
        \@namedef{#9}{#1\\#3}%
      \fi
    \else
      \if@company
        \@namedef{#9}{#2\\#6\\#3}%
      \else
        \@namedef{#9}{#2 #1\\#3}%
      \fi
    \fi
  \fi
}
\makeatother



% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{fromphone}{+49\,123\,45678910}
\setkomavar{fromemail}{mymail@gmail.com}
\setkomavar{backaddress}{Max Myname, Some Street 12, 012345 Somewhere}
\setkomavar{fromaddress}{Some Street 12\\012345 Somewhere}

\begin{document}

\setkomavar{subject}{The subject}
\setkomavar{date}{\today}
\setkomavar{place}{Dresden}
\setkomavar{yourmail}{1.1.2013}
\setkomavar{yourref}{76/54/321}
\setkomavar{myref}{123/456/789}

\input{meier.adr}
\begin{letter}{\MEIER}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}

\begin{letter}{\CCI}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}

Here are the two letters (relevant parts only)

enter image description here

enter image description here