Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

After having plowed through (i.e. read and trimmed down the given examples in an attempt to apply them to my case) these documentations and guides:

I still can't figure out how to simply pass two numbers to a .sty-file.

The situation is as follows: there is a .sty-file that changes the behaviour of siunitx so that only very small and very large numbers are output in scientific notation, and the other numbers are displayed in the usual format. This file has the following contents:

\RequirePackage{expl3,siunitx}
\sisetup{scientific-notation=true}
\ExplSyntaxOn
    \cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\newcommand*{\ThresholdLow}{0.01}
\newcommand*{\ThresholdHigh}{100}
\let\OldNum\num%
\renewcommand*{\num}[2][]{%
    \fpcmpTF{abs(#2)<=\ThresholdLow}{%
        \OldNum[scientific-notation=true,#1]{#2}%
    }{%
        \fpcmpTF{abs(#2)>=\ThresholdHigh}{%
            \OldNum[scientific-notation=true,#1]{#2}%
        }{%
            \OldNum[scientific-notation=false,#1]{#2}%
        }%
    }%
}

and is for the time being saved as threshold.sty in the same folder as the (to be) compiled document. This document includes it using the usepackage-command, as such:

\documentclass{article}
\usepackage{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

In the .sty-file above, there are two lines defining the limits outside of which the numbers should be printed using scientific notation:

\newcommand*{\ThresholdLow}{0.01}
\newcommand*{\ThresholdHigh}{100}

Now ideally, these two thresholds would be given as an argument to the threshold.sty using for example

\usepackage[low=0.01,high=100]{threshold}

where the file would use 0.01 and 100 as default values if no arguments are given.

What is the preferred way to do this? And maybe more useful to other readers that are as new to these matters as I am: what would be the simplest way to do this?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

By rereading the documentation of kvoptions once more, I figured it out myself. This is the result:

\RequirePackage{expl3,kvoptions,siunitx}
\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.01]
\DeclareStringOption[1]{high}[100]
\ProcessKeyvalOptions*
\sisetup{scientific-notation=true}
\ExplSyntaxOn
    \cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\let\OldNum\num%
\renewcommand*{\num}[2][]{%
    \fpcmpTF{abs(#2)<=\threshold@low}{%
        \OldNum[scientific-notation=true,#1]{#2}%
    }{%
        \fpcmpTF{abs(#2)>=\threshold@high}{%
            \OldNum[scientific-notation=true,#1]{#2}%
        }{%
            \OldNum[scientific-notation=false,#1]{#2}%
        }%
    }%
}

First the options need to be set up using \SetupKeyvalOptions, then the keys (variables) are defined using \DeclareStringOption, and finally these keys are processed with \ProcessKeyvalOptions*. In my case this is done with the next piece of code.

\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.01]
\DeclareStringOption[1]{high}[100]
\ProcessKeyvalOptions*

Here \DeclareStringOption[1]{low}[0.01] says for example that the key low has as initial value 1, which changes to 0.01 if the key low is passed to the package without value (i.e. \usepackage[low]{threshold}), and otherwise the value that is assigned by calling the package (e.g. \usepackage[low=1e-3]{threshold}). We can now use \threshold@low and \threshold@high as variables.


Using the same .tex-file as described in the question, we can now for example only use a threshold for the lower bound:

\documentclass{article}
\usepackage[low]{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

enter image description here

Or using the default value for the lower threshold and using a different value for the higher threshold:

\documentclass{article}
\usepackage[low,high=1e3]{threshold}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}
\begin{document}
\begin{tabular}{l l l}
    Num & Old & New\\\hline\\[-0.7em]
    \Row{0.01}\\
    \Row{0.1}\\
    \Row{1}\\
    \Row{10}\\
    \Row{100}\\
\end{tabular}
\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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