%% nutritionfacts.sty
%% Typeset US-style "Nutrition Facts" panels and per-100 g nutrition tables.
%% Copyright (C) 2026 Alex Bell
%% This work may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, version 1.3c or later. http://www.latex-project.org/lppl.txt
%% Maintainer: Alex Bell <strawbelly.app at gmail.com>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nutritionfacts}[2026/11/21 v1.0 Nutrition Facts panels and nutrition tables]
\RequirePackage{pgfkeys}
\RequirePackage{array}
\RequirePackage{xcolor}

% ---------- keys ----------
\pgfkeys{/nf/.is family, /nf,
  width/.store in=\nf@width,             width=6.4cm,
  title/.store in=\nf@title,             title=Nutrition Facts,
  servings/.store in=\nf@servings,       servings=,
  serving size/.store in=\nf@servsize,   serving size=,
  calories/.store in=\nf@calories,       calories=0,
  dv note/.store in=\nf@dvnote,
  dv note={* The \% Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.},
  font/.store in=\nf@font,               font=\sffamily,
}

% ---------- rules ----------
\newcommand*\nf@rule[1]{\par\nointerlineskip\vskip1pt\hrule height #1\vskip1pt\nointerlineskip}
\newcommand*\nfthinrule{\nf@rule{0.3pt}}
\newcommand*\nfmidrule{\nf@rule{2.5pt}}
\newcommand*\nfthickrule{\nf@rule{6pt}}

% ---------- rows ----------
% \nfrow[<indent level 0/1/2>]{<label>}{<amount>}{<% DV or empty>}
\newcommand*\nf@line[3]{\hbox to\linewidth{#1\hfil\strut#2}\nfthinrule}
\newcommand\nfrow[4][0]{%
  \def\nf@ind{\ifcase#1\relax\or\hspace*{1em}\or\hspace*{2em}\fi}%
  \nf@line{\nf@ind\ifnum#1=0 \textbf{#2}\else #2\fi\ #3}{\textbf{\ifx\relax#4\relax\else#4\%\fi}}{}}
% Nutrients that are not bold (vitamins and minerals)
\newcommand\nfmineral[3]{\nf@line{#1\ #2}{#3\ifx\relax#3\relax\else\%\fi}{}}

% ---------- panel ----------
\newenvironment{nutritionfacts}[1][]{%
  \pgfkeys{/nf,#1}%
  \begin{minipage}{\nf@width}\nf@font\setlength\parindent{0pt}%
  \setlength\fboxsep{4pt}\setlength\fboxrule{0.6pt}%
  \begin{lrbox}{\nf@box}\begin{minipage}{\dimexpr\nf@width-2\fboxsep-2\fboxrule}\nf@font\setlength\parindent{0pt}%
  {\fontsize{22}{24}\selectfont\bfseries\nf@title\par}%
  \nfthinrule
  \ifx\nf@servings\@empty\else{\small\nf@servings\ \ifnum\nf@servings=1 serving\else servings\fi\ per container\par}\fi
  \ifx\nf@servsize\@empty\else\hbox to\linewidth{\textbf{Serving size}\hfil\textbf{\nf@servsize}}\fi
  \nfthickrule
  {\footnotesize\textbf{Amount per serving}\par}%
  \hbox to\linewidth{{\fontsize{16}{18}\selectfont\bfseries Calories}\hfil{\fontsize{26}{28}\selectfont\bfseries\nf@calories}}%
  \nfmidrule
  \hbox to\linewidth{\hfil\footnotesize\textbf{\% Daily Value*}}\nfthinrule
  \small
}{%
  \nfthickrule
  {\scriptsize\nf@dvnote\par}%
  \end{minipage}\end{lrbox}%
  \fbox{\usebox{\nf@box}}%
  \end{minipage}%
}
\newsavebox\nf@box

% ---------- per-100 g table (EU / international style) ----------
% \begin{nutritiontable}[per=100 g, portion=1 medium (118 g)] ... \nfitem{Energy}{89 kcal}{105 kcal} ... \end{nutritiontable}
\pgfkeys{/nf/per/.store in=\nf@per, /nf/per=100 g, /nf/portion/.store in=\nf@portion, /nf/portion=}
\newenvironment{nutritiontable}[1][]{%
  \pgfkeys{/nf,#1}\nf@font\small
  \def\nfitem##1##2##3{##1 & ##2\ifx\nf@portion\@empty\else & ##3\fi\\}%
  \def\nfsubitem##1##2##3{\hspace*{1em}##1 & ##2\ifx\nf@portion\@empty\else & ##3\fi\\}%
  \ifx\nf@portion\@empty\def\nf@cols{l r}\else\def\nf@cols{l r r}\fi
  \expandafter\tabular\expandafter{\nf@cols}\hline
  \textbf{Nutrition} & \textbf{per \nf@per}\ifx\nf@portion\@empty\else & \textbf{per \nf@portion}\fi\\\hline
}{\hline\endtabular}
\endinput
