在逐項清單中使用 ENTER 鍵作為分隔符

在逐項清單中使用 ENTER 鍵作為分隔符

背景:我妻子正在寫一本家庭食譜。主要作為我的學習練習,我正在為該專案編寫 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

我看到已經有一些解決方案,但我認為您應該使用一些字符,而不是使用空行來劃分成分,因為否則長成分行或不正確的換行符會造成嚴重破壞。下面我用過-下面我在行的開頭。此外,使用乳膠3正規表示式,我自動替換一個分數a/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}

相關內容