4

I am writing a multi-language document in latex.

I am using siunitx package in conjonction with babel to handle every numerical and units related numeric values.

My goal was to declare a new unit (for instance \fps) which would output differently depending on the locale selected.

I was thus thinking of something like this :

\documentclass[french,english]{book}

\usepackage[utf8]{inputenc}
\usepackage{babel} 
\usepackage{siunitx}

\sisetup{range-units = single}

\addto\extrasfrench{%
\sisetup{locale = FR, group-separator = {\,}, detect-all}%
\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{images\text{~}par\text{~}seconde}}

\addto\extrasenglish{\sisetup{locale = US, group-separator = {,}, detect-all}%
\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{frames\text{~}per\text{~}second}}

\begin{document}

    English: \SIrange{5}{6}{\fps}
    \begin{otherlanguage}{french}

        Français: \SIrange{5}{6}{\fps}

    \end{otherlanguage}

\end{document}

Unfortunately this idea cannot work since the

\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{frames\text{~}per\text{~}second}

macro is then executed within the document and thus resulting in an error ?

Any idea ?

Thank you.

8
  • 1
    Unrelated. What is the deal with those \text{~} ?
    – daleif
    Commented Aug 31, 2019 at 21:33
  • 1
    According to siunitx documentation, it corresponds to a "text-mode full space"
    – Flof
    Commented Aug 31, 2019 at 23:49
  • 3
    I don't think declaring a unit for this is the right approach, since you are really setting text and it should behave like text: Line breaks should be allowed, font changes should apply, etc. You should rather define something like \newcommand*\fpsname{frames per second}\newcommand*\fps[1]{\num{#1}~\fpsname} and then change \fpsname in the usual way when switching languages.
    – schtandard
    Commented Sep 1, 2019 at 12:37
  • 1
    As @schtandard says, really this is not actually a unit at all: it's textual, not 'symbolic' (unit symbols like m are symbols not name or abbreviations).
    – Joseph Wright
    Commented Sep 9, 2019 at 8:29
  • 1
    @schtandard While the question is not completely unclear, IMHO it still lacks some more explanation from the OP regarding the issues raised in the comments. Is the question actually how to define a new local SI unit, and the given textual example is just an unfortunate one? Or is the aim to define different textual units, and the approach to use siunitx just wasn't the right one?
    – siracusa
    Commented Sep 9, 2019 at 9:32

1 Answer 1

4

You can use the facilities of the translator package to do this. If the package is in your system, then siunitx loads it automatically. You can then set up the unit with

\DeclareSIUnit{\fps}{ \translate{frames per second} }

and declare translations with

\newtranslation[to = French]{frames per second}{images par seconde}

In your document this gives

Sample output

\documentclass[french,english]{book}

\usepackage[utf8]{inputenc}
\usepackage{babel} 
\usepackage{siunitx}

\sisetup{range-units = single}

\DeclareSIUnit{\fps}{ \translate{frames per second} }

\newtranslation[to = French]{frames per second}{images par seconde}

\addto\extrasfrench{%
\sisetup{locale = FR, group-separator = {\,}, detect-all}}

\addto\extrasenglish{\sisetup{locale = US, group-separator = {,}, detect-all}}

\begin{document}

English: \SIrange{5}{6}{\fps}

\begin{otherlanguage}{french}
  Français: \SIrange{5}{6}{\fps}
\end{otherlanguage}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .