Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
    
Welcome to TeX.sx! No need to add signature, your user name is automatically added to your post. If you can, please also package your code in a Minimal working Example (MWE) which can be compiled by anyone. –  Peter Jansson Feb 19 '13 at 11:32
    
Sure I'll do! Sorry... –  aaki Feb 19 '13 at 12:30
    
Nothing to be sorry about :-) We all learn from the experience of others on this site. –  Peter Jansson Feb 19 '13 at 12:39
    
Added. Yeah, hopefully I'll learn too! :) –  aaki Feb 19 '13 at 12:42

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
Damn it, I'm totally blown away! It's working like a charm! Even though it really looks complicated at least to me. But that is imo because the way of texing compared to programming in general is complete different but I like to think in those latter paradigms. Sorry for not voting your answer up, I'm not allowed to (yet). –  aaki Feb 19 '13 at 15:06
    
@aaki Keep up with the good questions and you'll be soon. And, who knows, also with good answers, too! –  egreg Feb 19 '13 at 15:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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