我正在尝试一种食谱格式。对于输入,该方法的每个步骤都附有该步骤的成分。输出将所有成分放在一起,将方法的所有步骤放在一起,通常称为“标准”格式。方法步骤上附加的数字与成分的数字相匹配,从而实现两者之间的方便对应。我尝试了各种涉及和的咒语\xdef
,\xappto
但似乎遗漏了拼图中的一个重要部分。有这但是它有用collect.sty
,但我不知道如何在这里应用它。澄清:我并不是说我想collect.sty
在这里使用,而是我不知道该解决方案是否适用于这个问题。
\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}
给出以下输出:
最终格式将大不相同。这只是一个米我们。
答案1
在调用环境后,计数器增加后,您立即全局地分配计数器\xstepnum
的值。但只有当环境关闭时,最终要打印的行才会放在一起(并展开相关的宏)。然而,此时保存的是最后一步的编号。因此,这个数字将被放入每个条目或行中。stepnum
step
recipe
\xstepnum
因此,您需要将当前步骤传递给整个宏链,直到将最后的几行组合在一起,如下所示:
\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}{mu{ }u{ }u{~}}{% #1=stepnum; #2=measure; #3=unit; #4=ingr. name
\xappto{\ingrbody}{\ing{#1}{#2}{#3}{#4}}~
}%
\NewDocumentCommand{\methline}{mu{~}}{% #1=stepnum; #2 rest
\xappto{\methbody}{\mth{#1}{#2}}~%
}%
}
%% |=====8><-----| %%
\NewDocumentCommand{\ing}{mmmm}{% Format: #1=stepnum; #2=measure; #3=unit; #4=ingr. name
\textbf{#1}:\ \textit{#2~#3}\ \emph{\textbf{#4}}\par%
}
\NewDocumentCommand{\mth}{mm}{% Format: #1=stepnum, #2=Line of method text
\textbf{#1:\ }#2\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}
%% Assemble ingredients
\begingroup %% write ingredients to \ingrbody
\offinterlineskip
\obeylines
\everypar={\ingrline{\thestepnum}}
}{%
\endgroup %% End method
}
\NewDocumentCommand{\method}{}{%
\endgroup %% end ingredients
\begingroup %% begin method write method to \methbody
\offinterlineskip
\obeylines
\everypar={\methline{\thestepnum}}
}
%% |=====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}
PS:在的定义中\ing
我将\textit{#1-#2}
其改为\textit{#1~#2}
,因为我相信这才是您的真正意思。