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.

How can I define a macro, say \address, so that I can use it like this:

\address
27/7 Montague Street
Edinburgh

and it would put the lines following it into another macro, say \addrinfo ? What I'm ultimately trying to do, is modify Knuth's letter macros so that I can specify the address once in the letter, and then produce an address in a different font. Currently I've got

\newbox\theiraddressbox
\def\address{\beginlinemode%                                                               
 \global\setbox\theiraddressbox=\vtop\bgroup\raggedright
  \hsize=\longindentation \everypar{\hangindent1cm}
  \def\endmode{\egroup\endgroup\bigskip\copy\theiraddressbox}}

but this means the address label is set in the same font.

share|improve this question
1  
Is there a reason you want to use plain tex instead of, say, koma-script’s nice letter (scrlttr2) class? –  Caramdir Oct 6 '10 at 22:31
1  
Personal preference for plain TeX, born from a frustration with LaTeX not giving me enough freedom. I'm also aware that my understanding of plain TeX is less that I want it to be. –  Harry Newton Oct 10 '10 at 11:02

2 Answers 2

What you can do is to put each line into a separate definition as part of the definition of \address. In Knuth's letter format, he puts the first line of the address in the definition of \addressee.

The idea is to do this for the whole address. Then you'll have the definitions of \ToName, \ToStreet etc to use afterwards in any way you like.

A minimal example to illustrate the idea is as follows:

\def\beginlinemode{\endmode\begingroup\obeylines\def\endmode{\par\endgroup}}
\let\endmode=\par
\def\address{\beginlinemode\getToAddress}

% Populates the \To* definitions.
{\obeylines\gdef\getToAddress#1
  #2
  #3
  #4
  {\gdef\ToName{#2\unskip}%
   \gdef\ToStreet{#3\unskip}%
   \gdef\ToCity{#4\unskip}%
   \def\beginletter{\endgroup\bigskip}}}


\address
Some Name
27/7 Montague Street
Edinburgh


\beginletter

And now you can use the different fields as you wish. For example:

\bigskip
\moveright2cm\vbox{\hbox{\ToName}\hbox{\ToStreet}\hbox{\ToCity}}
\bigskip

This letter is addressed to {\bf \ToName}.

\bye

You can also have a \FromAddress macro similar to \address, but whose ending markup is \address instead of \beginletter. So you'd start your letter like:

\FromAddress
My Name
My Street
My City

\address
To Name
To Street
To City

\beginletter

etc
share|improve this answer

In effect, your \address macro is not undelimited: Somehow, the \endmode macro has to get called afterwards. In Knuth's letter macros, this job is done by the \body macro. So, what you can do is delimit the \address macro like this:

\def\address{\beginlinemode\getaddress}
\def\getaddress#1\body{\gdef\theaddress{#1}\body}

Then the macro \theaddress will contain the unformatted address. Hope you can work out the missing details.

share|improve this answer
    
Thanks. My original version put the desired text in a box which was specified as \vtop\bgroup. The desired text was delimited by a \egroup which got called with one of several macros e.g. \date or \body or \yourref - the definitions of which included a \egroup. Am I right in saying it isn't possible to do a similar trick with macro definitions ? e.g \def\addr{\bgroup} and \def\date{\egroup <rest of \date definition> } ? –  Harry Newton Oct 10 '10 at 11:13
    
@Harry Newton: Unfortunately, you can't use \bgroup and \egroup in this way. As you can read in the TeXbook in the bottom of page 275, \def needs explicit left and right braces. –  Hendrik Vogt Oct 16 '10 at 12:38

Your Answer

 
discard

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