I'm writing my own letter class and I was wondering how I could deal with several attachments. Currently, I made myself a macro accepting one parameter that should be set in the preamble. The content - if there is any - is printed automatically by my letter-environment.
If there are several attachments, I would like to call the macro twice which would normally overwrite the first declaration (see my MWE). But as LaTeX reads the code from top to bottom, it first sees my first declaration and processes it. If I could store that value in an array it would be safe. If the second declaration comes along, I could append it's content to the first one. Then with a for loop one could print all the attachments in the list. This is very easy in other programming languages like PHP or Python, so I think LaTeX will also be able to handle this.
MWE
Class
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{customLetterClass}[2017/10/20 Custom Letter Class]
\LoadClass{article}
% %%%%%%%%%%%%%%%%
% Require Packages
% %%%%%%%%%%%%%%%%
\usepackage[english]{babel}
\usepackage{%
parskip,%
tabularx,%
environ,%
etoolbox%
}
% %%%%%%%%%%%%%%%%%
% Declare Variables
% %%%%%%%%%%%%%%%%%
% Attachment
\def\attachment#1{\gdef\@attachment{#1}}
\def\@attachment{}
% %%%%%%%%%%%%%%%%%%
% Define environment
% %%%%%%%%%%%%%%%%%%
\NewEnviron{letter}%
{%
% Letter text
{\BODY}%
%
% Attachment (if set)
\ifdefempty{\@attachment}%
{}%
{
\\[5\baselineskip]
\begin{tabularx}{\textwidth}{@{}lX@{}}
Attachment: & \@attachment
\end{tabularx}
}%
}%
Main
% %%%%%%%%%%%%%%
% Document Setup
% %%%%%%%%%%%%%%
\documentclass{customLetterClass}
% %%%%%%%%%%%%%%%%%%%
% Set Variable Values
% %%%%%%%%%%%%%%%%%%%
% Attachment
\attachment{Foo}
\attachment{Bar}
\begin{document}
\begin{letter}
Hallo
\end{letter}
\end{document}
Sorry, to not deliver this in a single file, but if I change the documentclass to article and paste the content of the class inside my preamble, suddenly the testing \ifdefempty does not work correctly. I didn't find the issue, so here I am posting the two files.
Problem with Werner's approach
If I include the code supposed by Werner into the letter environment, I get a weird error message. This does not happen if I use the MWE provided by him. I checked the code multiple times, but I'm unable to find the issue. That's why I'm providing this MWE with the error-producing code, so everyone knows where we are at. Maybe we can also solve this last bit. :-)
Class
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{customLetterClass}[2017/10/20 Custom Letter Class]
\LoadClass{article}
% %%%%%%%%%%%%%%%%
% Require Packages
% %%%%%%%%%%%%%%%%
\RequirePackage[english]{babel}
\RequirePackage{%
parskip,%
tabularx,%
environ,%
etoolbox%
}
% %%%%%%%%%%%%%%%%%
% Declare Variables
% %%%%%%%%%%%%%%%%%
% Attachment
\newcounter{attachment}
\newcommand{\attachmentpar}{%
\renewcommand{\attachmentpar}{\par}%
}
\newcommand{\attachment}[1]{%
\stepcounter{attachment}%
\listadd{\attachments}{#1}%
}
% %%%%%%%%%%%%%%%%%%
% Define environment
% %%%%%%%%%%%%%%%%%%
\NewEnviron{letter}%
{%
% Letter text
{\BODY}%
%
% Attachment (if set)
\ifnum\value{attachment}>0 % No \attachment
\\[5\baselineskip]
\begin{tabularx}{\linewidth}{ @{} l X @{} }
\ifnum\value{attachment}=1 % Only one \attachment
Attachment
\else % Multiple \attachments
Attachments
\fi &
\renewcommand{\do}[1]{\attachmentpar #1}% How each item should be processed
\dolistloop{\attachments}% Process list of \attachments
\end{tabularx}
\fi
}%
Main
same as above
Error
\c@attachment=\count89
! Illegal parameter number in definition of \env@letter@process.



\RequirePackage, not\usepackagereally. You could use thecsvlistfacilities ofetoolboxas well – Oct 24 '17 at 19:35\RequirePackage, seems that I forgot to modify it back when trying to make it work in a single file. – Sam Oct 24 '17 at 19:42