![使用 ENTER 键作为分项列表中的分隔符](https://linux22.com/image/370773/%E4%BD%BF%E7%94%A8%20ENTER%20%E9%94%AE%E4%BD%9C%E4%B8%BA%E5%88%86%E9%A1%B9%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%88%86%E9%9A%94%E7%AC%A6.png)
背景:我的妻子正在写一本家庭食谱。我主要将其作为学习练习,为该项目编写 LaTex 代码。我希望使食谱数据输入尽可能简单,并将(几乎)所有处理代码隐藏在后端。下面显示了我输入食谱成分的第一步。我使用 \IngredientList 命令,该命令接受可变数量的参数。如您所见,每个成分“项”都由 [;] 字符分隔。在理想情况下,我的妻子只需输入成分并使用 ENTER 键作为成分项之间的分隔符。我尝试使用 \cr 和 \newline 作为 \SplitList 分隔符,但没有成功。
我的问题:有没有办法将 ENTER 键设置为食谱成分项的可变长度列表之间的分隔符?(也欢迎其他解决方案)。
\documentclass[11pt]{book}
\usepackage{enumitem, xfrac, xparse}
\NewDocumentCommand \IngredientList { >{\SplitList{;}} m }
{
\begin{description} [noitemsep,leftmargin=!,labelindent=8pt,itemindent=-15pt]
\ProcessList{#1}{\ProcessIngredients}
\end{description}
}
\newcommand\ProcessIngredients[1]{\item #1}
\begin{document}
\IngredientList{
4 ounces unsalted butter, room temperature ;
2 large eggs, room temperature ;
1 cup granulated sugar ;
1 tsp. vanilla extract ;
1 Tbsp. instant espresso powder ;
\sfrac{1}{2} cup cocoa powder, sifted if necessary ;
1\sfrac{1}{2} cups flour ;
\sfrac{1}{2} cup almond flour ;
1 tsp. baking soda ;
\sfrac{1}{2} tsp. salt ;
1 cup toasted hazelnuts, roughly chopped (or pistachios or almonds) ;
3 ounces bittersweet chocolate, coarsely chopped
}
\end{document}
答案1
列表处理似乎并没有真正起到帮助作用,以下输出或多或少是相同的。如果您需要特定的布局,可以在本地环境中调整段落设置。
\documentclass[11pt]{book}
\usepackage{xfrac}
\newcommand\f[2]{\sfrac{#1}{#2}}
\begin{document}
4 ounces unsalted butter, room temperature
2 large eggs, room temperature
1 cup granulated sugar
1 tsp. vanilla extract
1 Tbsp. instant espresso powder
\f12 cup cocoa powder, sifted if necessary
1\f12 cups flour
\f12 cup almond flour
1 tsp. baking soda
\f12 tsp. salt
1 cup toasted hazelnuts, roughly chopped (or pistachios or almonds)
3 ounces bittersweet chocolate, coarsely chopped
\end{document}
答案2
我花了很多时间在食谱的格式上。我更喜欢尽可能轻量的标记。我知道 的危险\obeylines
(而且\everypar
就此而言),尽管这些危险很容易避免——只要不对使用它们的环境做任何事情。我认为这种使用使输入更干净。如果要换行,\obeylines
较长的配料应始终用 处理。我还习惯于 使换行更合理。\hangindent
~
\documentclass{book}
%% Narrow measure to show wrapped lines
\usepackage[textwidth=2.5in]{geometry}
\usepackage{xfrac}
\newenvironment{ingredients}{%
\begingroup
\frenchspacing %% no extra space after periods
\parindent0pt
\obeylines
\everypar={\hangindent1.25em} %% long lines should hang
\raggedright %% no need for justified text for ingredients
}{%
\endgroup
}
%% Transfrom horizontal fractions to slashed with
%% minimal markup. Not perfect but handles 99% of instances.
\def\fr#1/#2 {\sfrac{#1}{#2} }
\begin{document}
\begin{ingredients}
4 ounces unsalted butter, room~temperature
2 large eggs, room temperature
1 cup granulated sugar
1 tsp. vanilla extract
1 Tbsp. instant espresso powder
\fr1/2 cup cocoa powder, sifted if necessary
1\fr1/2 cups flour
\fr1/2 cup almond flour
1 tsp. baking soda
\fr1/2 tsp. salt
1 cup toasted hazelnuts, roughly chopped (or pistachios or almonds)
3 ounces bittersweet chocolate, coarsely~chopped %% note ~
\end{ingredients}
\end{document}
更新
分数的处理让我很困扰——我添加了一个定义,允许保持水平分数原样,并简单地添加命令将它们变成斜线分数。输出是相同的。
答案3
我看到已经有几个解决方案,但我认为你应该使用一些字符,而不是使用空行来划分成分,因为否则过长的成分行或错误的换行符会造成严重破坏。下面我在行-
首使用了。此外,使用LaTeX3a/b
正则表达式,我自动用替换分数,\sfrac{a}{b}
以便成分列表
\IngredientList{
- 4 ounces unsalted butter, room temperature
- 2 large eggs, room temperature
- 1 cup granulated sugar
- 1 tsp. vanilla extract
- 1 Tbsp. instant espresso powder
- 1/2 cup cocoa powder, sifted if necessary
- 1 1/2 cups flour
- 1/2 cup almond flour
- 1 tsp. baking soda
- 1/2 tsp. salt
- 1 cup toasted hazelnuts, roughly chopped (or pistachios or almonds)
- 3 ounces bittersweet chocolate, coarsely chopped
}
生产
当然,你可以将我的“自动分数”与其他解决方案之一结合起来,以删除-
分隔符
完整代码如下:
\documentclass[11pt]{book}
\usepackage{enumitem, xfrac, xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \regex_split:nnN { nVN }
\tl_new:N \l_ingredient_tl
\seq_new:N \l_ingredients_seq
\NewDocumentCommand \IngredientList { m }
{
% first replace fractions
\tl_set:Nn \l_ingredient_tl {#1}
\regex_replace_all:nnN { \b(\d+)/(\d+) } {\c{sfrac}\cB{\1\cE}\cB{\2\cE}} \l_ingredient_tl
% clear the ingredients sequence and split #1 on integers and/or \sfrac
\seq_clear:N \l_ingredients_seq
\regex_split:nVN { -\s } \l_ingredient_tl \l_ingredients_seq
% remove the first (empty) item from the sequence
\seq_pop_left:NN \l_ingredients_seq \l_ingredient_tl
% now run through the list and print the ingredients inside a description environment
\begin{description} [noitemsep,leftmargin=!,labelindent=8pt,itemindent=-15pt]
\item \seq_use:Nn \l_ingredients_seq {\item }
\end{description}
}
\ExplSyntaxOff
\begin{document}
\IngredientList{
- 4 ounces unsalted butter, room temperature
- 2 large eggs, room temperature
- 1 cup granulated sugar
- 1 tsp. vanilla extract
- 1 Tbsp. instant espresso powder
- 1/2 cup cocoa powder, sifted if necessary
- 1 1/2 cups flour
- 1/2 cup almond flour
- 1 tsp. baking soda
- 1/2 tsp. salt
- 1 cup toasted hazelnuts, roughly chopped (or pistachios or almonds)
- 3 ounces bittersweet chocolate, coarsely chopped
}
\end{document}