5

How do I write a macro that, given the input \length(100) would interpret the input value as length (mm) and output something like \SI{100}{\mm}/4'', In other words, I need something that behind the scenes makes a mm-to-in conversion ($1'' \approx 25mm$) and inserts it into my output.

0
8

Using just siunitx (v3) and the underlying expl3 support:

\documentclass{article}
\usepackage{siunitx}
\NewDocumentCommand\length{O{}m}{%
  \begingroup
    \sisetup{#1}%
    \qty{#2}{\milli\metre}%
    \ensuremath{/}%
    \qty[
      expression = ##1/2.54,
      evaluate-expression,
      round-mode = figures,
      round-precision = 3,
      quantity-product = 
      ]%
        {#2}{''}%
  \endgroup
}
\begin{document}
\length{100}, \length{101.6}, $\length{103.5}$
\end{document}

If you want to cover v2, you need slightly different set-up: a demo that works for both versions

\documentclass{article}
\usepackage{siunitx}
\ifx\qty\undefined
  \usepackage{xfp}
  \NewDocumentCommand\length{O{}m}{%
    \begingroup
      \sisetup{#1}%
      \SI{#2}{\milli\metre}%
      \ensuremath{/}%
      \edef\x{%
        \SI[
          number-unit-product = ,
          round-mode = figures,
          round-precision = 3,
        ]
          {\fpeval{#2/25.4}}{''}%
      }\x
    \endgroup
  }
\else
  \NewDocumentCommand\length{O{}m}{%
    \begingroup
      \sisetup{#1}%
      \qty{#2}{\milli\metre}%
      \ensuremath{/}%
      \qty[
        expression = ##1/25.4,
        evaluate-expression,
        round-mode = figures,
        round-precision = 3,
        quantity-product = 
        ]%
          {#2}{''}%
    \endgroup
  }
\fi
\begin{document}
\length{100}, \length{101.6}, $\length{103.5}$
\end{document}
3
  • A separate, genuine question: To denote feet and inches typographically, is one supposed to use text-mode or math-mode versions of ' and ''? (I thought one is supposed to show the text-mode versions, i.e., single and double text-mode apostrophes, but my impression may very well be wrong.)
    – Mico
    Sep 28 at 11:57
  • 2
    @Mico I'd say it's the same symbol as minutes of a plane angle, so primes, but there are not really codified rules for the typography here: I think this is very much a personal choice.
    – Joseph Wright
    Sep 28 at 12:00
  • @Mico Didn't I change that?
    – Joseph Wright
    Sep 28 at 20:48
6

If you're willing and able to compile your document with LuaLaTeX, the following solution may be of interest to you.

  • If a millimeter-based length divides evenly into an integer-based inch-length, no decimal part is shown. You can control the number of decimal digits shown by modifying the string "\%.3g".

  • The macro \length is set up to permit a line break after the "slash" character that separates the two length terms. If you don't want to permit a line break, just change \\slash to /.

enter image description here

\documentclass{article}
\usepackage{siunitx} % for '\qty' macro (formerly '\SI')
\usepackage{luacode} % for '\luaexec' macro

%% Lua-side code
\luaexec{
function length ( len_mm ) 
   len_in = string.format ( "\%.3g" , len_mm / 25.4 )
   tex.sprint ( "\\qty{"..len_mm.."}{\\milli\\meter}\\slash"..len_in.."\\mbox{''}" )
end 
}

%% LaTeX-side code
\newcommand\length[1]{\directlua{length(#1)}}

\begin{document}
\length{100}, \length{101.6}, $\length{103.5}$
\end{document}
2
  • I just tried to compile it in Overleaf and it gave me an error using \qty instead of \SI (which I read is the depracated version kept for backwards compatibilty). When I change \qty to \SI your example compiles without a problem, is there any possibilty to have Overleaf accept this new version? And I definitely have to learn Lua(LaTeX), as it seems to be very powerful and useful in many applications!
    – Lukas
    Sep 28 at 11:36
  • 1
    @Lukas - Until Overleaf gets around to providing access to version 3.0 of the siunitx package, you'll have to make do with \\SI instead of \\qty. (It didn't occur to me earlier to mention this factoid as I wasn't aware that you were using Overleaf, which is still based on TeXLive2020.) Indeed, because LuaTeX embeds Lua, LuaLaTeX can access Lua's powerful string library, including string.format. If you're familiar with C or one of its descendants: Lua's string.format function essentially implements C's versatile printf function.
    – Mico
    Sep 28 at 12:01

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.