3

I'm working on a recipe book. Some of my recipes are in British Imperial units, some in American, some in metric. I want to avoid manual conversion as that can produce errors, but I want the book to use one type of unit throughout.

I also want to specify which, at build time, so I want a switch I can set to which of the three to use. So I have one text, no conditionals, but a function that handles the standardization and conversion.

I know I can do this in LuaLaTeX, but I want to avoid external dependencies.

Question: Can I do this within LaTeX' macro language, within the LaTeX environment as a whole (including new LaTeX3 utilities), or am I stuck with external programming?

(And if there's a choice, what is the preferred approach?)

  • 1
    tex can easily do the conversions (just multiplying by a constant), You could use lua or expl3 or just simply use primitive tex arithmetic but (presumably?) you don't want exact conversions, but rather a coherent recipe in each set of units, and that might take manual adjustment – David Carlisle Dec 10 '17 at 10:31
  • Welcome! Why would you print only one unit? Things are much easier for users if you include the different options in every recipe. Although auto-conversion would make life extremely tricky, for the reasons @DavidCarlisle suggests. You've tagged this lua although you explicitly don't want to use it. (I don't know what 'external dependencies' this avoids, but still.) And it has nothing to do with latex3 as far as I can tell. – cfr Dec 11 '17 at 1:17
5

Here is what I would recommend. You manually define the amounts for each component in both units. For example:

\SetAmount{meat}{\SI{2.2}{\lb}}{\SI{1.0}{\kilo\gram}}
\SetAmount{exercise}{\SI{1}{\mile}}{\SI{1.6}{\kilo\meter}}

sets up two items: The amount of meat and the amount of exercise in both units. Then to use these you use \Amount{meat} or \Amount{exercise}. If \def\UseMetricUnits{} is defined the metric paramater is output:

enter image description here

Otherwise, the imperial amount is output.

enter image description here

The advantage of this is the amounts do not need to be direct conversions which can get messy as this is not a scientific document which would need more accurate amounts.

Code:

\def\UseMetricUnits{}%
\documentclass{article}
\usepackage{siunitx}

\newcommand{\SetAmount}[3]{%
    \ifdefined\UseMetricUnits
        \expandafter\newcommand\csname Amount #1\endcsname{#3}%
    \else
        \expandafter\newcommand\csname Amount #1\endcsname{#2}%
    \fi
}
\newcommand*{\Amount}[1]{\expandafter\csname Amount #1\endcsname}


%% As SI does not have imperial units, set those up here
\DeclareSIUnit\lb{\text{lb}}%
\DeclareSIUnit\mile{\text{mile}}%

%% Declare the equivalent units
\SetAmount{meat}{\SI{2.2}{\lb}}{\SI{1.0}{\kilo\gram}}
\SetAmount{exercise}{\SI{1}{\mile}}{\SI{1.6}{\kilo\meter}}

\begin{document}
For this recipe we use 
\Amount{meat} of meat.
After you will need to walk
\Amount{exercise} to burn of these calories.
\end{document}
| improve this answer | |
  • In my opinion, it makes more sense to type For this recipe we use \Amount{1} of meat. to specify how much of ingredient we need. – Sigur Dec 30 '17 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.