Hi Hraban,

On Sat, 2025-11-29 at 16:26 +0100, Henning Hraban Ramm wrote:
> Can I apply a format code, e.g. for rounding, to use such a dimension
> with \unit?

I'm sure that this is possible from TeX, but as soon as you're doing
something complicated like this, it's easiest to use Lua:

    \startluacode
        interfaces.implement {
            name = "FormatUnit",
            arguments = {
                "dimension",
                "string",
            },
            actions = { function(from, to)
                return number.todimen(from, to, [[%M\thinspace %s]])
            end, context },
        }
    \stopluacode

    \unprotect
        \protected\def\FormatUnit[#S#1]#*#2{\clf_FormatUnit{#1}{#2}}
    \protect

    \startTEXpage[offset=1ex]
        \cdef\Units{pt,mm,cm,in}
        \bTABLE[
            leftframe=off,
            rightframe=off,
            loffset=0.5em,
            roffset=0.5em,
            align=flushright,
        ]
            % We can't nest \startprocesscommalist or \dowith, so we need to 
use one
            % of each.
            \dowith{\Units}{
                \bTR
                    \startprocesscommacommand[\Units]
                        \bTD \FormatUnit[1#1]{##1} \eTD
                    \stopprocesscommacommand
                \eTR
            }
        \eTABLE

        \FormatUnit[10pt]{in}
        \FormatUnit[\textwidth]{pc}
        \FormatUnit[\dimexpr 1cm + 1in]{mm}
    \stopTEXpage

Thanks,
-- Max
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : [email protected] / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to