Using the ENTER key as a delimiter in an itemized list

Using the ENTER key as a delimiter in an itemized list

Background: My wife is writing a family cookbook. Mostly as a learning exercise for me, I am putting together LaTex code for the project. I would like to make the recipe data entry as simple as possible, with (almost) all the processing code hidden in the back end. My first pass for entering recipe ingredients is shown below. I use an \IngredientList command that takes a variable number of arguments. As you see, each ingredient "item" is delimited by the [;] character. In an ideal world, my wife could just type the ingredients and use the ENTER key as the delimiter between ingredient items. I have tried \cr and \newline as the\SplitList delimiter with no luck.

My Question: Is there a way to set the ENTER key as the delimiter between a variable length list of recipe ingredient items? (Other solutions also welcome).

\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

The list handling doesn't really seem to be helping, the following is more or less the same output. If you need a particular layout the paragraph settings could be adjusted in a local environment.

enter image description here

\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

I have spent a good deal of time on the formatting of recipes. I prefer the lightest possible markup. I am aware of the perils of \obeylines (and \everypar for that matter), though these perils are easily avoided -- just don't do anything with the environment that uses them. I think this use of \obeylines makes the input cleaner. Long ingredients should always be treated with a \hangindent if they are going to wrap. I have also used ~ to make for more sensible line breaks.

\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}

Update

The handling of fractions bothered me -- I have added a definition that allows keeping horizontal fractions as-is, and simply adding the command to make them into slashed fractions. The output is identical.

ingredients example

답변3

I see that there are already a few solutions but rather than using blank lines to demarcate ingredients I think that you should use some character because otherwise a long ingredient line, or incorrect linebreak, will cause havoc. Below I have used - at the start of the line. In addition, using LaTeX3 regular expressions, I automatically replace a fraction a/b with \sfrac{a}{b} so that the ingredient list

\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
}

produces

enter image description here

Of course, you could combine my "automatic fractions" with one of the other solutions so as to remove the - seperator

Here is the full code:

\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}

관련 정보