1

I am trying to write a package that generates envelopes for scrlttr2. The idea is to collect a list of recipient addresses and write all the envelopes at the end. Here is what I have so far, distilled down to the relevant bits:

\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}

%%%%% Goes in a separate package %%%%%
\makeatletter
\def\@envelopes{}

\AtEndLetter{
    \g@addto@macro\@envelopes{\@elt{%
        ~ % So that the page isn't totally blank in this non-working example
        \usekomavar{toname}\\
        \usekomavar{toaddress}%
    }}
}

\AtEndDocument{
    \KOMAoptions{paper=4.125in:9.5in,paper=landscape}
    \recalctypearea
    \pagestyle{empty}
    \def\@elt#1{ % Some stuff
        #1
        \clearpage
    }
    \@envelopes
}
\makeatother
%%%%% End of package %%%%%

\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}

\begin{letter}{%
John Doe\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}

I look forward to hearing from you.

\closing{Sincerely,}
\end{letter}
\end{document}

The problem is that \usekomavar{toname} and \usekomavar{toaddress} are not getting expanded right away, and toname and toaddress are empty by the time they do expand (in the AtEndDocument hook). This question has a very similar sort of problem: Expanding an argument of code added by \g@addto@macro

I tried to use the method suggested there:

%...
\AtEndLetter{
    \edef\next{%
        \noexpand\g@addto@macro\noexpand\@envelopes{\noexpand\@elt{%
            \usekomavar{toname}\\
            \usekomavar{toaddress}%
        }}%
    }
    \next
}
%...

but then I get a very unexpected error message from pdflatex:

! Argument of \reserved@a has an extra }.
<inserted text> 
                \par 
l.48 \end{letter}

(and actually, this is different from my original file, which gives, ! Incomplete \iffalse; all text was ignored after line 59.).

Am I doing something wrong? Is there a way to fix this?

1 Answer 1

2

I don't see in the manual any way to use variables in an expandable way and you need the value, not the container.

\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}

%%%%% Goes in a separate package %%%%%
\makeatletter
\def\@envelopes{}

\AtEndLetter{%
  \begingroup\edef\x{\endgroup
    \noexpand\g@addto@macro\noexpand\@envelopes{%
      \noexpand\@elt{%
        \noindent % So that the page isn't totally blank
        \expandafter\unexpanded\expandafter{\scr@toname@var}\noexpand\\
        \expandafter\unexpanded\expandafter{\scr@toaddress@var}%
      }%
    }%
  }\x
}

\AtEndDocument{%
  \KOMAoptions{paper=4.125in:9.5in,paper=landscape}
  \recalctypearea
  \pagestyle{empty}
  \def\@elt#1{% Some stuff
    #1\clearpage
  }%
  \@envelopes
}
\makeatother
%%%%% End of package %%%%%

\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}

\begin{letter}{%
John Doe\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}

I look forward to hearing from you.

\closing{Sincerely,}
\end{letter}
\end{document}

The value is stored in \scr@<varname>@var.

An expl3 version:

\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g__pastore_envelopes_seq

\AtEndLetter
 {
  \seq_gput_right:Nx \g__pastore_envelopes_seq
   {
    \noindent
    \exp_not:v { scr@toname@var }
    \exp_not:N \\
    \exp_not:v { scr@toaddress@var }
   }
 }

\NewDocumentCommand{\printenvelopes}{}
 {
  \seq_use:Nn \g__pastore_envelopes_seq { \clearpage }
 }

\ExplSyntaxOff

\AtEndDocument{%
  \KOMAoptions{paper=4.125in:9.5in,paper=landscape}
  \recalctypearea
  \pagestyle{empty}
  \printenvelopes
}
%%%%% End of package %%%%%

\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}

\begin{letter}{%
John Doé\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}

I look forward to hearing from you.

\closing{Sincerely,}
\end{letter}
\end{document}

I used Doé in order to check that accents are not untimely expanded.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .