I am attempting a format for recipes. For input, each step of the method is accompanied by the ingredients for that step. The output places all of the ingredients together, and all of the steps of the method together, in what is commonly termed the “standard” format. Numbers are attached to the method steps that match the numbers for the ingredients, enabling a convenient correspondence between the two. I have tried various incantations involving \xdef
and \xappto
but seem to be missing an important piece of the puzzle. There is this but it uses collect.sty
and I don't know how to apply that here. Clarification: I don't mean to imply that I want to use collect.sty
here, but that I don't know that solution applies to this problem.
\documentclass[]{article}
\usepackage[papersize={5.5in,8.5in},margin=0.6in,bottom=0.7in]{geometry}
\usepackage{fontspec,xparse,etoolbox}
\newcounter{stepnum}
%% |=====8><-----| %%
\begingroup
\lccode`~=`\^^M
\lowercase{%
\endgroup
\NewDocumentCommand{\ingrline}{u{ }u{ }u{~}}{% #1=measure; #2=unit; #3=ingr. name
\xappto{\ingrbody}{\noexpand\ing{#1}{#2}{#3}}~
}%
\def\methline#1~{% Line of method text
\xappto{\methbody}{\unexpanded{\mth{#1}}}~%
}%
}
%% |=====8><-----| %%
\NewDocumentCommand{\ing}{mmm}{% Format: #1=measure; #2=unit; #3=ingr. name
\textbf{\xstepnum}:\ \textit{#1-#2}\ \emph{\textbf{#3}}\par%
}
\NewDocumentCommand{\mth}{m}{% Format: Line of method text
\textbf{\xstepnum:\ }#1\par
}
\NewDocumentEnvironment{recipe}{s O{} m}{% *=?; #2=keyval; #3=title
\setcounter{stepnum}{0}%% stepnum attaches both to group of ingredients and each method step
\gdef\ingrbody{}%
\gdef\methbody{}%
}{%
%% output \ingrbody and \methbody
\ingrbody
\par
\methbody
}
\NewDocumentEnvironment{step}{}{%
\stepcounter{stepnum}
\xdef\xstepnum{\thestepnum}
%% Assemble ingredients
\begingroup %% write ingredients to \ingrbody
\offinterlineskip
\obeylines
\everypar={\ingrline}
}{%
\endgroup %% End method
}
\NewDocumentCommand{\method}{}{%
\endgroup %% end ingredients
\begingroup %% begin method write method to \methbody
\offinterlineskip
\obeylines
\everypar={\methline}
}
%% |=====8><-----| %%
\setmainfont{STIX Two Text}
\pagestyle{empty}
\begin{document}
\begin{recipe}{A test}
\begin{step}% 1
1 cup butter (should be ``1:'')
½ cup sugar (should be ``1:'')
\method
Beat butter and sugar together until fluffy. (should be 1:)
\end{step}
\begin{step}% 2
2 cups flour
1 tablespoon baking powder
\method
Add to the butter mixture and combine.
\end{step}
\end{recipe}
\end{document}
gives this output:
The final format will be very different. This is just an MWE.