
背景: 妻が家族向けの料理本を書いています。主に私自身の学習として、このプロジェクト用の LaTex コードをまとめています。レシピ データの入力をできるだけ簡単にし、(ほぼ) すべての処理コードをバックエンドに隠したいと考えています。レシピの材料を入力するための最初のパスを以下に示します。可変数の引数を取る \IngredientList コマンドを使用します。ご覧のとおり、各材料「項目」は [;] 文字で区切られています。理想的には、妻が材料を入力し、ENTER キーを材料項目間の区切り文字として使用すればよいでしょう。\SplitList 区切り文字として \cr と \newline を試しましたが、うまくいきませんでした。
私の質問: レシピの材料項目の可変長リスト間の区切りとして 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
すでにいくつかの解決策があるようですが、空白行を使用して材料を区切るのではなく、何らかの文字を使用するべきだと思います。そうしないと、材料の行が長くなったり、改行が間違っていたりして混乱が生じます。以下では、-
行の先頭に使用しました。さらに、ラテックス3a/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}